且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从多维数组中提取垂直数组值

更新时间:2022-06-10 00:08:15

有一个名为 array_column 的函数可以抓取数组中的一列.
首先需要将字符串转换为带有 Json_decode 和第二个参数为 true 的数组.

There is a function called array_column that will grab one column in an array.
First the string needs to be converted to array with Json_decode and second parameter to true.

然后 array_column 返回您的预期输出.

Then array_column returns your expected output.

不需要循环.

$filteredZips='[{
"id": 21,
"distance": "0"},{
"id": 20,
"distance": "3.9399923305414037"},{
"id": 29,
"distance": "8.33045537474091"}]';

$filteredZipsarr = json_decode($filteredZips,true);

$id = array_column($filteredZipsarr, "id");
Var_dump($id);

https://3v4l.org/hFOKR

如果您不需要 $filteredZipsarr,您可以将其设为单行:

If you don't need the $filteredZipsarr you can make it a one liner:

$id = array_column(json_decode($filteredZips,true), "id");