PHP递归获取二维数组中指定key的值

痴心易碎 提交于 2019-12-24 08:07:20
$data = [
    "resulterrorCode" => 0,
      "resultraw" => [
        "result" => "ok",
        "trace_id" => "Xdm00694540804871731h0",
        "code" => 0,
        "data" => [
              "id" => "tdm006945408048717384T",
        ],
        "description" => "成功",
        "info" => "Received push messages for 1 TOPIC"
    ],
];
echo "<pre>";
var_dump($data);

function getDeepValue($array, $keys, $delimiter = '.')
{
    $keys = explode($delimiter, $keys);
    $key = array_shift($keys);
    if (sizeof($keys) > 0 && isset($array[$key])) {
        return get_deep_value($array[$key], implode($delimiter, $keys), $delimiter);
    } else {
        return $array[$key] ?? null;
    }
}

$res = getDeepValue($data,'resultraw.data.id');
var_dump($res);

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!