skip to Main Content

facebook Batch request api gives me the response.and as according to my logic this is the best response i get i want to access the root element “source_id” and “copied_id” inside the [BODY].
i just want to access them in minimum loops.

i am currently using nested loops.

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"}]}
            )
     ))

The body contain the JSON response below is the decoded json response.

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

Answers


  1. You could refactor it to use Collections since you are using Laravel.

    For example:

    $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();
    

    The output of the above will be:

    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
      ]
    ]
    

    Here’s an example you can play around with.

    Login or Signup to reply.
  2. Use following code hope this will help you

    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;   
        }  
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search