0

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);
6
  • 1
    It'll be much clearer if you include your dump as text rather than a picture, and also use var_export() instead so the data is re-usable easily in an answer. Also we don't necessarily need all of it, just a relevant sample will do. You can edit the question to update it. Commented Apr 1, 2022 at 15:41
  • Hi @ADyson I have tried to do a var_export() but it doesnt let me because the code isnt properly formatted? I hope you would still like to help me :) Commented Apr 1, 2022 at 15:48
  • 1
    That doesn't make any sense. var_export works fine. What is "it" and what's it complaining about exactly? At a guess, the SO question editor is complaining. You possibly need to read How do I format my posts?, idk. Commented Apr 1, 2022 at 15:50
  • @ADyson Ah yeah that was it sorry. Reformatted and added the var_export Commented Apr 1, 2022 at 15:52
  • So to be clear, that's the result of var_export($results_decoded), is that correct? Commented Apr 1, 2022 at 16:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Hi @ADyson, This is exactly what iam looking for!! Thank you. But when i try this i still seem to get the Undefined index: result. Could this be because the data is coming from an API? I'm confident your solution should work as shown in the online PHP editor. Thanks again for your time!
I don't know, maybe there are some other structures in the data which you didn't show in your sample.
I dont think so i have copy and pasted the full result in a pastebin: pastebin.com/0pZ3b72C I think every data structure was included?
I don't know, it's impossible to see properly when it's unformatted like that. But if you add some logging to the code (e.g. a counter inside the loop, and var_export($inner_val) each time) you can then see exactly which line it fails on.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.