在索引存在的情况下继续获取 'Undefined index',在在线编辑器中完美运行但在项目中不运行?
Keep getting 'Undefined index' while index is there, Works perfectly in online editor but not in project?
我正在使用 php、laravel 和 Vue (&vuex) 创建仪表板。 Whosebug 的另一个用户给了我很大的帮助,并提出了以下代码来从我从 [=47= 返回的数据集中提取我想要的值(仅 'Result' 和 'result') ] API。
他的代码似乎在每个在线 PHP 编辑器中都能完美运行。但是一旦我想尝试在我的项目中使用它,我就会收到一条错误消息,上面写着“未定义的索引:结果”。是什么原因造成的,我该如何解决?
我似乎无法解决这个问题。任何帮助将不胜感激!
代码:
$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() - 数据集:
$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', ), ), ));
在线PHP编辑器中的完美工作示例:
现场演示:https://3v4l.org/TEPab
期望的结果数组:
["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"]
我的代码(在项目中使用):
$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'];
}
解释:
如果我做对了,并且数组键在您描述的数组中是 Result
和 result
,那么这段代码就是正确的。
$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);
演示 Link:https://3v4l.org/nRCQS
我正在使用 php、laravel 和 Vue (&vuex) 创建仪表板。 Whosebug 的另一个用户给了我很大的帮助,并提出了以下代码来从我从 [=47= 返回的数据集中提取我想要的值(仅 'Result' 和 'result') ] API。 他的代码似乎在每个在线 PHP 编辑器中都能完美运行。但是一旦我想尝试在我的项目中使用它,我就会收到一条错误消息,上面写着“未定义的索引:结果”。是什么原因造成的,我该如何解决?
我似乎无法解决这个问题。任何帮助将不胜感激!
代码:
$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() - 数据集:
$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', ), ), ));
在线PHP编辑器中的完美工作示例: 现场演示:https://3v4l.org/TEPab
期望的结果数组:
["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"]
我的代码(在项目中使用):
$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'];
}
解释:
如果我做对了,并且数组键在您描述的数组中是 Result
和 result
,那么这段代码就是正确的。
$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);
演示 Link:https://3v4l.org/nRCQS