多维数组,找到所选值的关键

多维数组,找到所选值的关键

问题描述:

I have a code with an array containing a bunch of fruits with product id and price as keys for the different values.

I get the correct price, but how do I get the name of chosen product? For example if you choose the "apple 1", I want the answer to be "The product apple costs 5.95" where it now just says "The product costs 5.95". As you may see, I've tried to do an array_search without good results.

How should I do this?

<?php
$a3 = array('apple' => array('productid' => '1',
                            'price' => '5.95'),
            'banana' => array('productid' => '2',
                            'price' => '122'),
            'squash' => array('productid' => '3',
                            'price' => '47.2'),
            'watermelon' => array('productid' => '4',
                            'price' => '1.2'),
            'potato' => array('productid' => '5',
                            'price' => '6.04')
            );
?>
<!DOCTYPE html>
<html>
 <body>
  <form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
    <select name="products">
        <?php
         foreach ($a3 as $key => $value){ ?>
            <option value="<?php echo $value['price']; ?>"><?php echo $key, " ", $value['productid']; ?></option>
        <?php
        } ?>
    </select>
    <input type="submit" name="showprice" value="Show the price">

    <?php
        if (isset($_POST['showprice'])){
                $price = $_POST['products'];
                $product = array_search("$price", $a3);

                echo "The product ". $product ." costs ". $price .".";
        }
    ?>

</form>

我有一个包含一堆水果的数组的代码,产品ID和价格作为不同值的键。 p>

我得到了正确的价格,但我如何获得所选产品的名称? 例如,如果您选择“苹果1”,我希望答案是“产品苹果成本5.95”,现在它只是说“产品成本5.95”。 正如您所看到的,我尝试过一个没有良好结果的array_search。 p>

我该怎么做? p>

 &lt;  ?php 
 $ a3 = array('apple'=&gt; array('productid'=&gt;'1',
'price'=&gt;'5.95'),
'banana'=&gt; array(  'productid'=&gt;'2',
'price'=&gt;'122'),
'squash'=&gt; array('productid'=&gt;'3',
'price'=&gt;  ; '47 .2'),
'西瓜'=&gt;数组('productid'=&gt;'4',
'价格'=&gt;'1.2'),
'土豆'=&gt;数组('  productid'=&gt;'5',
'price'=&gt;'6.04')
); 
?&gt; 
&lt;!DOCTYPE html&gt; 
&lt; html&gt; 
&lt; body&gt; 
  &lt; form action =“&lt;?php $ _SERVER ['PHP_SELF']?&gt;”  method =“POST”&gt; 
&lt; select name =“products”&gt; 
&lt;?php 
 foreach($ a3 as $ key =&gt; $ value){?&gt; 
&lt; option value  =“&lt;?php echo $ value ['price'];?&gt;”&gt;&lt;?php echo $ key,“”,$ value ['productid'];  ?&gt;&lt; / option&gt; 
&lt;?php 
}?&gt; 
&lt; / select&gt; 
&lt; input type =“submit”name =“showprice”value =“显示价格”&gt  ; 
 
&lt;?php 
 if(isset($ _ POST ['showprice'])){
 $ price = $ _POST ['products']; 
 $ product = array_search(“$ price”,  $ a3); 
 
 echo“The product”。  $ product。“成本”。  $ price。“。”; 
} 
?&gt; 
 
&lt; / form&gt; 
  code>  pre> 
 
 

p> DIV>

The option value shoud be a key. Because products price can be same then eachother. I mean when you post the form, your post variable need to be unique value "apple". Then you can get the product node from the array.

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
    <select name="products">
        <?php
        foreach ($a3 as $key => $value){ ?>
            <option value="<?php echo $key; ?>"><?php echo $key, " ", $value['productid']; ?></option>
            <?php
        } ?>
    </select>
    <input type="submit" name="showprice" value="Show the price">

    <?php
    if (isset($_POST['showprice'])){
        $name       = $_POST['products'];
        $product    = $a3[$name];
        $price      = $product['price'];
        $productid = $product['productid'];
       echo "The product ". strtoupper($name) ." costs ". $price .".";
    }
    ?>
</form>

This array_search function doesn't work as expected when you have this kind of a multi dimensional array. You could try and write a custom search function like this on top of the page:

function multiDimSearch($price, $arr){
    foreach ($arr as $fruit => $val) {
        if ($val['price'] == $price) {
            return $fruit;
        }
    }
    return false;
}       

And then change:

 $product = array_search("$price", $a3);

   To

 $product = multiDimSearch($price, $a3);

Hope this helps.

Peace! xD

Try this..

<?php
        if (isset($_POST['showprice'])){
                $price = $_POST['products'];
                $product = "";
                foreach($a3 as $k=>$a) {
                    if($a['price'] == $price) {
                        $product = $k;
                        break;
                    }
                }

                echo "The product ". $product ." costs ". $price .".";
        }
    ?>

As per your array, i think there is no need to use array_search(), you can also concatenate the $key with product value as:

Example:

HTML:

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
  <select name="products">
    <?php
    foreach ($a3 as $key => $value){ ?>
    <option value="<?php echo $value['price']; ?>-<?php echo $key; ?>"><?php echo $key. " ". $value['productid']; ?></option>
    <?php
    } ?>
  </select>
  <input type="submit" name="showprice" value="Show the price">
</form> 

PHP:

<?php    
if (isset($_POST['showprice'])){
  $data = explode('-',$_POST['products']);
  $price = $data[0];
  $product = $data[1];
  echo "The product ". $product ." costs ". $price .".";
}
?>

Using concatenation for <?php echo $value['price']; ?>-<?php echo $key; ?>