多维数组响应。减少(最小化)嵌套循环

时间:2019-02-08 05:35:26

标签: php multidimensional-array laravel-5.7

facebook批处理请求api给了我响应。根据我的逻辑,这是我得到的最佳响应,我想访问根元素“ source_id ”和“ copied_id ”。 我只想以最少的循环访问它们。

我目前正在使用嵌套循环。

Array(
[0] => Array
    (
        [0] => stdClass Object
            (
                [code] => 200
                [body] => {"copied_adset_id":"15454","ad_object_ids":[{"ad_object_type":"ad_set","source_id":"545454","copied_id":"15454"}]}
            )
        [1] => stdClass Object
            (
                [code] => 200
                [body] => {"copied_adset_id":"1547754","ad_object_ids":[{"ad_object_type":"ad_set","source_id":"566454","copied_id":"1547754"}]}
            )
     )
 [1] => Array
    (
        [0] => stdClass Object
            (
                [code] => 200
                [body] => {"copied_adset_id":"1500454","ad_object_ids":[{"ad_object_type":"ad_set","source_id":"598754","copied_id":"1500454"}]}
            )
        [1] => stdClass Object
            (
                [code] => 200
                [body] => {"copied_adset_id":"78448","ad_object_ids":[{"ad_object_type":"ad_set","source_id":"541230","copied_id":"78448"}]}
            )
     ))

包含以下JSON响应的主体是已解码的json响应。

stdClass Object(
[copied_adset_id] => 14848
[ad_object_ids] => Array
    (
        [0] => stdClass Object
            (
                [ad_object_type] => ad_set
                [source_id] => 14848
                [copied_id] => 448486
            )

    ))

2 个答案:

答案 0 :(得分:1)

使用以下代码希望对您有帮助

foreach($arraya as $arrayaresponse) {    
    foreach($arrayaresponse as $copiedarray) {    
        $adsetdata = array();    
        $copiedBody = json_decode($copiedarray - > body);    
        $adata['source_id'] = $copiedBody - > ad_object_ids[0] - > source_id;   
        $adata['copied_id'] = $copiedBody - > ad_object_ids[0] - > copied_id;   
    }  
 }

答案 1 :(得分:0)

由于您正在使用Laravel,因此可以将其重构为使用Collections。

例如:

$response = [
    [
        (object) [
            'code' => 200, 
            'body' => '{"copied_adset_id":15454,"ad_object_ids": [{"ad_object_type":"ad_set","source_id": 545454,"copied_id": 545}]}'
        ],
        (object) [
            'code' => 200, 
            'body' => '{"copied_adset_id":15454,"ad_object_ids": [{"ad_object_type":"ad_set","source_id": 545454,"copied_id": 545}]}'
        ]  
    ],
    [
        (object) [
            'code' => 200, 
            'body' => '{"copied_adset_id":15454,"ad_object_ids": [{"ad_object_type":"ad_set","source_id": 545454,"copied_id": 545}]}'
        ],
        (object) [
            'code' => 200, 
            'body' => '{"copied_adset_id":15454,"ad_object_ids": [{"ad_object_type":"ad_set","source_id": 545454,"copied_id": 545}]}'
        ]  
    ]  
];

return collect($response)
    ->flatten()
    ->flatMap(function($item) {
        return json_decode($item->body, true)['ad_object_ids'];
    })
    ->map(function($item) {
        return array_only($item, ['source_id', 'copied_id']);
    })
    ->toArray();

上面的输出将是:

array:4 [▼
  0 => array:2 [▼
    "source_id" => 545454
    "copied_id" => 545
  ]
  1 => array:2 [▼
    "source_id" => 545454
    "copied_id" => 545
  ]
  2 => array:2 [▼
    "source_id" => 545454
    "copied_id" => 545
  ]
  3 => array:2 [▼
    "source_id" => 545454
    "copied_id" => 545
  ]
]

这是一个example,您可以在其中使用。