如何从php中的json_解码中获取ID

如何从php中的json_解码中获取ID,php,Php,我正在尝试使用json_decode函数从json获取所有id,如何在一个数组中只获取id?我真的很感谢你的帮助 答复: { "columns": [ "name", "id", "sno" ], "data": [ [ "test1", "123", "1" ], [ "test2", "456", "2" ] ] } 代码: 您可以使用和执行以

我正在尝试使用json_decode函数从json获取所有id,如何在一个数组中只获取id?我真的很感谢你的帮助

答复:

{
  "columns": [
    "name",
    "id",
    "sno"
  ],
  "data": [
    [
      "test1",
      "123",
      "1"
    ],
    [
      "test2",
      "456",
      "2"
    ]
  ]
}
代码:


您可以使用和执行以下操作:


您可以使用和执行以下操作:


你的报价乱七八糟。您需要退出Word中的编辑。您的响应不是有效的json数据,因此json_decode将返回null。抱歉,更新了json响应。您的引号太乱了。您需要退出Word中的编辑。您的响应不是有效的json数据,因此json_decode将返回null。抱歉,已更新json响应。出现以下错误:无法将stdClass类型的对象用作array@JayBlanchard您缺少
$obj=json\u decode($result,true)中的
true
复制并粘贴了所有代码,包括
true
仍然在测试中留下错误
true
返回一个对象,该对象可以如下方式获取:
$obj->data
。当测试工作正常时更改为该类型。由此产生错误:无法将stdClass类型的对象用作array@JayBlanchard您缺少
$obj=json\u decode($result,true)中的
true
复制并粘贴了所有代码,包括
true
仍然在测试中留下错误
true
返回一个对象,该对象可以如下方式获取:
$obj->data
。当测试完美地工作时,改变到这一点。
$obj = json_decode($result, true);
foreach ($obj as $key => $value) {
    foreach ($value as $k => $val) {
        echo $val;
        if ($k == "id") {
            array_push($all_ids, $val);
        }
    }
}
$obj     = json_decode($result);
$all_ids = array();

foreach ($obj->data as $el) {
  array_push($all_ids, $el[1]);
}
$ids = array_map(function($a){return $a[1];}, $obj['data']);
$obj = json_decode($result);
// identify which column number corresponds to "id"
$idColumnNo = in_array('id', $obj->columns);
// collect the elements at that column number from the data array
$all_ids = array_map(function ($elem) {
    return $elem[$idColumnNo];
}, $obj->data);