Php 将数组值与输入值进行比较

Php 将数组值与输入值进行比较,php,forms,multidimensional-array,Php,Forms,Multidimensional Array,我有一个包含数组的PHP文件: $cards = array ( 1234=>array ( "type" => "Viza", "owner" => "Petar Petrovic", "balance" => 1000, ), 5678=>array

我有一个包含数组的PHP文件:

$cards = array
    (
      1234=>array
                ( 
                  "type" => "Viza",
                  "owner" => "Petar Petrovic",
                  "balance" => 1000,
                ),
      5678=>array
                (

                  "type" => "Master Card",
                  "owner" => "Ivan Ivanovic",
                  "balance" => 20000,
                ),
     91011=>array
                (

                  "type" => "American Express",
                  "owner" => "Marko Markovic",
                  "balance" => 300000,
                ),
    121314=>array
                (

                  "type" => "Diners Club",
                  "owner" => "Veljko Veljkovic",
                  "balance" => 1000000,
                )
这是一个信用卡信息的例子,关键是信用卡号。因此,我必须仔细研究,并将这些值与表单中的输入值进行比较。我的HTML表单:

  <form action="cards.php" method="post">
  <table align="center">
    <tr>
      <td>Enter your name</td>
      <td><input type="text" name="name"></td>
   </tr>

   <tr>
     <td>Enter your card number</td>
     <td><input type="text" name="cardno"></td>
   </tr>

   <tr>
     <td>Enter your sum</td>
     <td><input type="text" name="sum"></td>
   </tr>

   <tr>
     <td>Select card type</td>
     <td><select name="select">
        <option value="viza">Viza</option>
        <option value="master">Master Card</option>
        <option value="american">American Express</option>
        <option value="diners">Diners Club</option>
        </select></td>
   </tr>

   <tr>
      <td></td>
      <td><input type="submit" name="submit" value="SUBMIT"></td>
   </tr>

</table>
</form>

输入您的姓名
输入您的卡号
输入你的总数
选择卡片类型
维萨
万事达卡
美国运通
大来卡
首先,我需要检查输入的卡号是否存在于数组中,如果存在,则需要比较其余输入值(名称和卡类型)。我该怎么做?我是这一切的初学者,所以请善待我,帮帮我

我有一个想法,但它当然不起作用:

  $card_number=$_POST['cardno'];
  $name = $_POST['name'];
  $sum = $_POST['sum'];
  $type = $_POST['select'];

foreach($cards as $key=>$item){  
   if ($item['type']== $type){
     if($item['owner']== $name){
       if($item['balance'] > $sum){

            $newbalance= $item['balance'] - $sum;
            echo "Your new balance is:".$newbalance;
        }
         else if($item['balance'] < $sum){

            echo "You dont have enough sources on your account";

        }
       else if  ($item['owner']!== $name){

        echo "Ivalid name!";

        }
     else if($item['type']!== $type){

        echo "Invalid card type!";

      }
    else if($key !== $card_number){

        echo "Invalid card number!";

    }
     }
 }
 }
$card\u number=$\u POST['cardno'];
$name=$_POST['name'];
$sum=$_POST['sum'];
$type=$_POST['select'];
foreach($key=>$item的卡){
如果($item['type']=$type){
如果($item['owner']==$name){
如果($item['balance']>$sum){
$newbalance=$item['balance']-$sum;
echo“您的新余额为:”.$newbalance;
}
如果有其他情况($item['balance']<$sum){
echo“你的账户上没有足够的信息来源”;
}
else if($item['owner']!==$name){
呼应“我的名字!”;
}
else if($item['type']!=$type){
回显“无效卡类型!”;
}
否则如果($key!==$card\u编号){
回显“无效卡号!”;
}
}
}
}
首先,我需要检查输入的卡号是否存在于数组中,如果存在,则需要比较其余输入值(名称和卡类型)

使用函数检查
$cards
数组中是否存在卡号。然后按照以下方式重构整个业务逻辑

// First check if the card number exists or not
if(array_key_exists($_POST['cardno'], $cards)){
    // Check rest of the details
    if($cards[$_POST['cardno']]['name'] == $_POST['name']){
        if($cards[$_POST['cardno']]['type'] == $_POST['select']){
            if($cards[$_POST['cardno']]['balance'] >= $_POST['sum']){
                $newbalance= $cards[$_POST['cardno']]['balance'] - $_POST['sum'];
                echo "Your new balance is:".$newbalance;
            }else{
                echo "You dont have enough sources on your account";
            }
        }else{
            echo "Invalid card type";
        }
    }else{
        echo "Invalid name";
    }
}else{
    echo "Invalid card number";
}
首先,我需要检查输入的卡号是否存在于数组中,如果存在,则需要比较其余输入值(名称和卡类型)

使用函数检查
$cards
数组中是否存在卡号。然后按照以下方式重构整个业务逻辑

// First check if the card number exists or not
if(array_key_exists($_POST['cardno'], $cards)){
    // Check rest of the details
    if($cards[$_POST['cardno']]['name'] == $_POST['name']){
        if($cards[$_POST['cardno']]['type'] == $_POST['select']){
            if($cards[$_POST['cardno']]['balance'] >= $_POST['sum']){
                $newbalance= $cards[$_POST['cardno']]['balance'] - $_POST['sum'];
                echo "Your new balance is:".$newbalance;
            }else{
                echo "You dont have enough sources on your account";
            }
        }else{
            echo "Invalid card type";
        }
    }else{
        echo "Invalid name";
    }
}else{
    echo "Invalid card number";
}

试试这个,正如@Rajdeep Paul告诉您的,您可以使用array_key_exists(),我已经创建了一个函数,并向余额中添加了新值

function checkCreditCard($cards, $cardno, $name, $sum, $type) {
  $bResult = (array_key_exists($cardno, $cards)) ? true : false;
  if ($bResult) {
    if ($cards[$cardno]['owner'] == $name && $cards[$cardno]['type'] == $type) {
        if ($cards[$cardno]['balance'] >= $sum) {
            $newBalance = ($cards[$cardno]['balance'] - $sum);
            $cards[$cardno]['balance'] = $newBalance;
            return "Your new balance is:" . $newBalance;
        } else {
            "You dont have enough sources on your account";
        }
     } else {
        return "Invalid name or card type";
     }
  }
  return "Invalid card number!";
}
输出:

$cards = array
(
1234 => array
    (
    "type" => "Viza",
    "owner" => "Petar Petrovic",
    "balance" => 1000,
),

echo checkCreditCard($cards, 1234, 'Petar Petrovic', '100', 'Viza');

Your new balance is:900

echo checkCreditCard($cards, 000000, 'Petar Petrovic', '100', 'Viza');

Invalid card number!

试试这个,正如@Rajdeep Paul告诉您的,您可以使用array_key_exists(),我已经创建了一个函数,并向余额中添加了新值

function checkCreditCard($cards, $cardno, $name, $sum, $type) {
  $bResult = (array_key_exists($cardno, $cards)) ? true : false;
  if ($bResult) {
    if ($cards[$cardno]['owner'] == $name && $cards[$cardno]['type'] == $type) {
        if ($cards[$cardno]['balance'] >= $sum) {
            $newBalance = ($cards[$cardno]['balance'] - $sum);
            $cards[$cardno]['balance'] = $newBalance;
            return "Your new balance is:" . $newBalance;
        } else {
            "You dont have enough sources on your account";
        }
     } else {
        return "Invalid name or card type";
     }
  }
  return "Invalid card number!";
}
输出:

$cards = array
(
1234 => array
    (
    "type" => "Viza",
    "owner" => "Petar Petrovic",
    "balance" => 1000,
),

echo checkCreditCard($cards, 1234, 'Petar Petrovic', '100', 'Viza');

Your new balance is:900

echo checkCreditCard($cards, 000000, 'Petar Petrovic', '100', 'Viza');

Invalid card number!

附带说明:在扣除并向用户显示余额金额后,您可能还需要更新
$cards
数组中的余额金额。非常感谢,我将尝试一下!附带说明:在扣除并向用户显示余额金额后,您可能还需要更新
$cards
数组中的余额金额。非常感谢,我将尝试一下!当我尝试这段代码时,所有功能都正常,但我总是在页面上出现“无效卡片类型!”的回音,即使它是正确的。是的,看这个:Viza Master card American Express Diners Club这是错误的,所有值都必须固定以匹配数组值。哦,是的,你是对的,真是个愚蠢的错误,谢谢你!我需要一直用我的思维方式理解这个计算机逻辑,我很容易犯错误……再次感谢你。当我尝试这段代码时,所有功能都正常,但我总是在页面上重复“无效卡片类型”,即使它是正确的。是的,看这个:Viza Master Card美国运通食客俱乐部这是错误的,所有值都必须固定以匹配数组值。哦,是的,你是对的,这是一个愚蠢的错误,谢谢!我需要一直用我的思维方式来理解计算机逻辑,我很容易犯错误……再次感谢你。