Heim  >  Artikel  >  Backend-Entwicklung  >  PHP一个二维数组的有关问题

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

WBOY
WBOYOriginal
2016-06-13 12:53:35722Durchsuche

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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn