DeckChain
DeckChain

Reputation: 85

How to get : for each json object in array get nested value php

I'am trying to extract all the String value's from the key: ERRORS. This is my code so far and i have been able to 'un-nest' 2 layers but i cant seem to get any further then this. could someone show me how to access the value of the key: ERRORS.

I would like to store all the string values in an array

I have the following data structure.

var_export($results_decoded):

array ( 
0 => array ( 'Result' => array ( 0 => array ( 'ERRORS' => '99012: Weborder number already exists : 20211049', ), ), ),
1 => array ( 'Result' => array ( 0 => array ( 'ERRORS' => '99012: Weborder number already exists : 20211048', ), ), ), 
2 => array ( 'Result' => array ( 0 => array ( 'ERRORS' => '99012: Weborder number already exists : 20211050', ), ), ), 
3 => array ( 'Result' => array ( 0 => array ( 'ERRORS' => '99012: Weborder number already exists : 20211050', ), ), ), 
4 => array ( 'Result' => array ( 0 => array ( 'ERRORS' => '99012: Weborder number already exists : 20211049', ), ), ), 
5 => array ( 'Result' => array ( 0 => array ( 'ERRORS' => '99012: Weborder number already exists : 20211046', ), ), ), 
6 => array ( 'Result' => array ( 0 => array ( 'ERRORS' => '99012: Weborder number already exists : 20211045', ), ), ), 
7 => array ( 'result' => '2007: New Web Order created successfully', ), 
8 => array ( 'Result' => array ( 0 => array ( 'ERRORS' => '99012: Weborder number already exists : 20211046', ), ), ),

a return() Statement:

[{"Result":[{"ERRORS":"99012: Weborder number already exists : 20211049"}]},{"Result":[{"ERRORS":"99012: Weborder number already exists : 20211048"}]},{"Result":[{"ERRORS":"99012: Weborder number already exists : 20211050"}]}]

a datadump of $results_array

array:2432 [▼
  0 => array:1 [▼
    "Result" => array:1 [▼
      0 => array:1 [ …1]
    ]
  ]
  1 => array:1 [▼
    "Result" => array:1 [▼
      0 => array:1 [ …1]
    ]
  ]
  2 => array:1 [▼
    "Result" => array:1 [▼
      0 => array:1 [ …1]
    ]
  ]
  3 => array:1 [▶]
  4 => array:1 [▶]
  5 => array:1 [▶]
  6 => array:1 [▶]
  7 => array:1 [▶]
  8 => array:1 [▶]
  9 => array:1 [▶]

My code:

    $data = Log::all('RESPONSE');
    $results_decoded = json_decode($data, true);

    foreach ($results_decoded as $inner_array){
        foreach ($inner_array as $value){
            $result_array[] = $value;
        }
    }
    return($result_array);

Upvotes: 0

Views: 98

Answers (1)

ADyson
ADyson

Reputation: 61839

It seems your raw data (awkwardly) contains two possible data structures within the outer array. Mostly there's a nested array leading to the errors data, but occasionally there's a simpler structure just containing a success message.

This code will check each item to see if there's an ERRORS element. If there is, it uses that. If not, it assumes the simpler structure instead and uses that.

foreach ($results_decoded as $inner_val){
  if (isset($inner_val["Result"][0]["ERRORS"])) $result_array[] = $inner_val["Result"][0]["ERRORS"];
  else $result_array[] = $inner_val["result"];
}

Based on the sample data in your question, this produces the following output (in JSON format):

[
 "99012: Weborder number already exists : 20211049",
 "99012: Weborder number already exists : 20211048",
 "99012: Weborder number already exists : 20211050",
 "99012: Weborder number already exists : 20211050",
 "99012: Weborder number already exists : 20211049",
 "99012: Weborder number already exists : 20211046",
 "99012: Weborder number already exists : 20211045",
 "2007: New Web Order created successfully",
 "99012: Weborder number already exists : 20211046"
]

which I think is what you were looking for, if I've understood correctly.

Live demo: https://3v4l.org/TEPab

Upvotes: 1

Related Questions