脚本宝典收集整理的这篇文章主要介绍了php – 从给定数组中获取值,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
$result=$array
通过使用echo $result我得到以下数组
Array ( [success] => 1 [PRoduct] => Array ( [id] => 83 [SEO_h1] => [name] => Beer Week [manufacturer] => The Boxer Store [model] => WPEB/0413/74/BW [sku] => WPEB/0413/74/BW [reward] => 0 [points] => 0 [image] => asd [images] => Array ( [0] => asd ) [quantITy] => 4 [price] => Rs.599 [special] => [discounts] => Array ( ) [options] => Array ( [0] => Array ( [product_option_id] => 42 [option_id] => 25 [name] => Size Option [type] => select [option_value] => Array ( [0] => Array ( [product_option_value_id] => 165 [option_value_id] => 72 [name] => Large [option_sku] => [image] => asd [price] => [price_prefix] => + ) [1] => Array ( [product_option_value_id] => 166 [option_value_id] => 73 [name] => XL [option_sku] => [image] => asd [price] => [price_prefix] => + ) [2] => Array ( [product_option_value_id] => 163 [option_value_id] => 70 [name] => Small [option_sku] => [image] => asd [price] => [price_prefix] => + ) [3] => Array ( [product_option_value_id] => 164 [option_value_id] => 71 [name] => Medium [option_sku] => [image] => asd [price] => [price_prefix] => + ) ) [required] => 1 ) ) [minimum] => 1 [rating] => 0 [description] => as [attribute_groups] => Array ( ) [date_added] => 2014-09-30 12:35:12 [date_modified] => 2014-10-17 17:33:46 [currency] => INR [status] => 1 ) )
我可以像这样单独获取结果
$finalid = $array['product']['id']; echo $finalid;
但希望使用for循环获取每个数据,甚至是最内部的数据.为此我试过这个
$c=count($result); for ( $i=0; $i < $c; $i++) { echo $array[$i]['id']; echo $array[$i]['images'][0]; echo $array[$i]['options'][0]['product_option_id']; echo $array[$i]['images'][0]['option_value'][0]['product_option_value_id']; echo $array[$i]['images'][0]['option_value'][1]['product_option_value_id']; }
<?PHP foreach($result as $res){ echo '<pre>'; echo $res['id']; if(is_array($res['options']) && !empty($res['options'])) { foreach($res['options'] as $option) { echo '<pre>'; echo ($option['product_option_id']); if(is_array($option['option_value']) && !empty($option['option_value'])) { foreach($option['option_value'] as $option_value) { echo '<pre>'; echo $option_value['product_option_value_id']; } } } } ?>
以上是脚本宝典为你收集整理的php – 从给定数组中获取值全部内容,希望文章能够帮你解决php – 从给定数组中获取值所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。