无法从数组中获取项目

时间:2015-11-18 08:55:37

标签: php arrays

嗨我刚接触api调用,我似乎遇到了从我的数组中获取一个名为price的项目的问题。以下是我应该从中提取价格的数组。

Array
(
    [prodId] => ROC-PRD-2
    [prodName] => iphone 6
    [projectId] => 8
    [categoryIds] => Array
        (
            [0] => ROC-CAT-1
        )

    [prodParentSku] => iph6a1
    [prodMetaTitle] => iphone 6
    [visible] => 1
    [prodStatus] => 1
    [modifiedDate] => 1443472415
    [createDate] => 1443472193
    [productImages] => Array
        (
            [0] => Array
                (
                    [id] => 89
                    [imageName] => iphone-ipad hi res.png
                    [imagePath] => http://tos-staging-web-server-s3.s3.amazonaws.com/8/products/ROC-PRD-2/iphone_ipad_hi_res.png
                    [visible] => 1
                    [featured] => 
                    [modifiedDate] => 1443472390
                    [createDate] => 1443472390
                )

            [1] => Array
                (
                    [id] => 90
                    [imageName] => ipad 2.jpg
                    [imagePath] => http://tos-staging-web-server-s3.s3.amazonaws.com/8/products/ROC-PRD-2/ipad_2.jpg
                    [visible] => 1
                    [featured] => 
                    [modifiedDate] => 1443472397
                    [createDate] => 1443472397
                )

        )

    [pricing] => Array
        (
            [price] => 1000
            [memberGroupPrices] => Array
                (
                )

        )

)

我可以使用以下for循环

获取产品图像信息,例如id,imagepath
foreach ( $product['productImages'] as $key => $data){
    foreach ($data as $key => $eachImage){
 }
}

但是对于价格,我的代码如下:

foreach ( $product['pricing'] as $key => $price){

}

如果我回应我会得到的价格" 1000Array" 如果我回应价格['价格'],则不会出现任何问题。

3 个答案:

答案 0 :(得分:1)

你不需要寻找$pricing作为它的单身;

$price = $product["pricing"]["price"];
$memberGroupPrices = $product["pricing"]["memberGroupPrices"];

答案 1 :(得分:1)

请查看

foreach ( $product['pricing'] as $key => $price){ 
        if($key=='price')
          $price_val=$price;
}

答案 2 :(得分:1)

只需打印价格而不用循环

 $price = $array["pricing"]["price"];