Home > database >  Keep getting 'Undefined index' while index is there, Works perfectly in online editor but
Keep getting 'Undefined index' while index is there, Works perfectly in online editor but

Time:04-04

Iam creating a dashboard with php, laravel and Vue (&vuex). Another user of Stackoverflow has helped me tremendously and came up with the following code to extract the values that i want (only the 'Result' & 'result') from the dataset that i get back from the laravel API. His code seems to work perfect in every online PHP editor. But as soon as i want to try to use it in my project i get an ERROR back that says "Undefined index: result". What causes this and how would i be able to solve this?

I cant seem to get my head around the problem. Any help would be greatly appreciated!

The code:

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

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

        }

        return json_encode($result_array);

var_export() - The dataset:

$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', ), ), ));

The perfecty working example in a online PHP editor: live demo: https://3v4l.org/TEPab

The Desired array of result:

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

My code (using in project):

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

        $result_array = [];
        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'];

        }

CodePudding user response:

Explanation:

If I am getting right, and array keys are Result and result in your described array, then this code would be right.

$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', ), ), ));

foreach ($results_decoded as $inner_val){
    $result_array[] = $inner_val["Result"][0]["ERRORS"] ?? $inner_val['result'] ?? '';    
}

$result_array = array_values($result_array);

echo json_encode($result_array);

Demo Link: https://3v4l.org/nRCQS

  • Related