Home  >  Article  >  Backend Development  >  PHP一个二维数组的有关问题

PHP一个二维数组的有关问题

WBOY
WBOYOriginal
2016-06-13 12:53:35759browse

PHP一个二维数组的问题
我有一个二维数组,想通过一个元素goods_id的值得到对应的其他元素的值
数组如下,知道goods_id = 123,如何得到goods_name,goods_price。。。。
最简单的方法是?


$goods = array(
            0 => array('goods_id'    => 123, //goods_id
                'goods_name'  => 测试商品,    //商品名称
                'goods_price' => '12.10',       //商品价格
                'max_number'  => '5',           //限购数量
                'thumb_url'   => 'http://.....'  //缩略图
            ),
            1 => array('goods_id'    => 124,
                'goods_name'  => 测试商品2,
                'goods_price' => '12.10',
                'max_number'  => '',
                'thumb_url'   => 'http://.....'
            ),
            2 => array('goods_id'    => 125,
                'goods_name'  => 测试商品3,
                'goods_price' => '12.10',
                'max_number'  => '',
                'thumb_url'   => 'http://.....'
            )
        );


------解决方案--------------------
$result = array();
foreach($goods as $vals){
   if($vals['goods_id']==123){
     $result[] = $vals;
     break;
   }
}
------解决方案--------------------
变形数组为
$goods = array(<br>
            123 => array('goods_id'    => 123, //goods_id<br>
                'goods_name'  => 测试商品,    //商品名称<br>
                'goods_price' => '12.10',       //商品价格<br>
                'max_number'  => '5',           //限购数量 <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn