PHP通过嵌套的JSON循环查找值
我正在尝试循环遍历嵌套的JSON数据,以找到具有特定字符串的名称,并提取与该名称相关联的字符串值。在本例中,名称字符串为“myKey”,值字符串为“12345678” 在看了几个类似的问题和文档之后,我尝试了不同的方法,例如使用对象或关联数组,但最终还是无法获得所需的信息或收到错误 错误类型:PHP通过嵌套的JSON循环查找值,php,arrays,json,object,Php,Arrays,Json,Object,我正在尝试循环遍历嵌套的JSON数据,以找到具有特定字符串的名称,并提取与该名称相关联的字符串值。在本例中,名称字符串为“myKey”,值字符串为“12345678” 在看了几个类似的问题和文档之后,我尝试了不同的方法,例如使用对象或关联数组,但最终还是无法获得所需的信息或收到错误 错误类型: Notice: Array to string conversion Warning: Invalid argument supplied for foreach() Trying to get pr
Notice: Array to string conversion
Warning: Invalid argument supplied for foreach()
Trying to get property of non-object
下面是使用$myObj=JSON_decode($result)解码的JSON片段
以下是我尝试过的PHP代码片段:
$myObj = json_decode($result);
// or I have tried
// $myObj = json_decode($result, true);
// here are different snippets of code I tried
foreach($myObj->result as $test) {
echo '<pre>';
print_r($test->name);
echo "<br>";
if ($test->name == "Units") {
$resultName = $test->name;
echo $resultName . "<br>";
}
echo '</pre>';
}
/*
foreach($myObj->result as $test) {
echo $test . "<br>";
foreach($test->name as $test1) {
echo $test1 . "<br>";
foreach($test1->value as $test2) {
echo $test2 . "<br>";
}
}
}
*/
/*
foreach($myObj->result as $test) {
if (($test->name) == "Units") {
// grab the value that corresponds to the name
$units = $test->name;
if (($units->name) == "Components") {
$components = $units->name;
print_r($components);
}
}
}
*/
但是值的位置可能不同,所以我需要通过循环找到对象的名称
有谁能提供更好的使用对象(甚至可能是关联数组)的方法吗
更新为包含原始JSON片段(解码前)
感觉需要一些递归函数(在找到结果之前调用自身的函数)来查找嵌套数组中的值 看看
当然,您必须更改该问题中的函数,但我曾经遇到过类似的问题,而且非常有用。下面是编码和解码的代码。希望这对你有帮助
您是否考虑过将此结果转换为一个新的(关联)数组,以便管理关键点/位置并轻松循环数组?嗯,我只看到$result是一个数组(3),而不是一个对象。@Adder结果是一个对象数组;)你能发布一个你必须转换的原始json的示例吗?@Eineki我已经更新了帖子,在json_decode函数之前显示了原始json的片段
$myObj = json_decode($result);
// or I have tried
// $myObj = json_decode($result, true);
// here are different snippets of code I tried
foreach($myObj->result as $test) {
echo '<pre>';
print_r($test->name);
echo "<br>";
if ($test->name == "Units") {
$resultName = $test->name;
echo $resultName . "<br>";
}
echo '</pre>';
}
/*
foreach($myObj->result as $test) {
echo $test . "<br>";
foreach($test->name as $test1) {
echo $test1 . "<br>";
foreach($test1->value as $test2) {
echo $test2 . "<br>";
}
}
}
*/
/*
foreach($myObj->result as $test) {
if (($test->name) == "Units") {
// grab the value that corresponds to the name
$units = $test->name;
if (($units->name) == "Components") {
$components = $units->name;
print_r($components);
}
}
}
*/
print_r($myObj->result[1]->value[0][5]->value[0][1]->name);
print_r($myObj->result[1]->value[0][5]->value[0][1]->value);
string(21420) "{
"info": {
.
.
.
},
"stuff": [{
"name":
"type":
.
.
.
}],
"result": [
{
"name":
"value":
"description":
},
{
"name": "Units",
"value": [
[
{
"name":
"value":
"description":
},
.
.
.
{
"name": "Components",
"value": [
[
{
"name":
"value":
"description":
},
{
"name": "myKey",
"value": "12345678",
"description":
},
.
.
.
] (end inner Components value)
] (end outer Components value)
] (end inner Units value)
] (end outer Units value)
} (end results Units)
] (end result)
} (end opening)
//Encoding
//Array Values
$a1=array("name"=>"abc", "value"=>"def", "description"=>"ghi");
$a2=array("name"=>"jkl", "value"=>"mno", "description"=>"pqr");
$a3=array("name"=>"stu", "value"=>"wxy", "description"=>"zab");
//Array of Value Arrays
$info=array($a1, $a2, $a3);
$result=array($a1, $a2, $a3);
$stuff=array($a1, $a2, $a3);
//The Main Enclosed Array
$main=json_encode(array("info"=>$info, "result"=>$result, "stuff"=>$stuff));
//Decoding
//Fetching the name from result's $a1 array
$main_array=json_decode($main);
echo $main_array->result[1]->name; //Displays jkl