加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 从给定数组中获取值

发布时间:2020-12-13 16:55:34 所属栏目:PHP教程 来源:网络整理
导读:我有一个像这样存储在$result中的数组 $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/
我有一个像这样存储在$result中的数组

$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'];
            }


        }
    }
}
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读