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

时间:2016-03-14 09:06:01

标签: php html

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

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

我该怎么做?

<?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>

4 个答案:

答案 0 :(得分:2)

选项值应该是一个关键。因为产品价格可以相同。我的意思是,当您发布表单时,您的帖子变量需要是唯一的值&#34; apple &#34;。然后,您可以从阵列中获取产品节点。

<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>

答案 1 :(得分:2)

根据您的数组,我认为无需使用array_search(),您也可以将$key与产品值连接为:

示例:

<强> 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 .".";
}
?>

使用<?php echo $value['price']; ?>-<?php echo $key; ?>的连接

答案 2 :(得分:1)

当您拥有这种多维数组时,此array_search函数不会按预期工作。您可以尝试在页面顶部编写这样的自定义搜索功能:

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

然后改变:

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

   To

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

希望这有帮助。

和平!的xD

答案 3 :(得分:1)

试试这个..

<?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 .".";
        }
    ?>