在PHP中,筛选关联数组的数组

时间:2022-10-30 12:48:23

In PHP, are there any inbuilt functions to turn

在PHP中,是否有内建函数要转换

[{"id":1, "name":"John"}, {"id":2, "name":"Tim"}]

into

[{"id":1}, {"id":2}]

?

吗?

I've used JSON to describe the objects above, but that's just a conceptual representation of my array of associative arrays. I don't want to have to loop manually - something short and elegant I can fit on one line would be nice.

我使用了JSON来描述上面的对象,但这只是我的关联数组数组的概念表示。我不想要手动循环——一些短小而优雅的我可以放在一行上的东西会很好。

4 个解决方案

#1


4  

One line, using array_map:

一行,使用到:

$arr = json_decode('[{"id":1, "name":"John"}, {"id":2, "name":"Tim"}]');

$new_arr = array_map(function($el){$ret=array("id"=>$el->id);return $ret;},$arr);

var_dump(json_encode($new_arr));

#2


1  

array_map(function($arr){return $arr[0];}, $array);

This should do it.

这应该这样做。

Edit As noted by Jonathon Hibbard, you could pass array element by reference, this way you do not to assign result of the function and just use changed old array. The code should then be modified appropriately.

按照Jonathon Hibbard的说明进行编辑,您可以通过引用传递数组元素,这样就不必分配函数的结果,只需使用修改过的旧数组。然后应该适当地修改代码。

#3


0  

First decode json by json_decode. You will get an array. Then follow this link to remove an index from an associative array. Then again decode it. It should work.

首先通过json_decode对json进行解码。你会得到一个数组。然后按照这个链接从关联数组中删除索引。然后再解码。它应该工作。

#4


0  

do something like:

做些什么:

$array = json_decode($some_json_string, true);
array_walk($array, function($value, $key) use(&$array) {
  if($key == "name") {
    unset($array[$key]);
  }
});

Edit: Cthulhu's answer won't get ya there without re-assigning it. Could use it as a reference though (equal to the walk. though if you want to use the map, its a bit better not to reallocate with a brand new array copy and just pass it by reference, then remove the key with an unset within it and move on.)

编辑:Cthulhu的答案不会在没有重新分配的情况下得到。可以用它作为参考(相当于步行)。不过,如果您想使用映射,最好不要重新分配一个全新的数组拷贝,然后通过引用传递它,然后在其中删除未设置的键,然后继续。

array_map(function(&$array) { unset($array['name']; }, $array);

#1


4  

One line, using array_map:

一行,使用到:

$arr = json_decode('[{"id":1, "name":"John"}, {"id":2, "name":"Tim"}]');

$new_arr = array_map(function($el){$ret=array("id"=>$el->id);return $ret;},$arr);

var_dump(json_encode($new_arr));

#2


1  

array_map(function($arr){return $arr[0];}, $array);

This should do it.

这应该这样做。

Edit As noted by Jonathon Hibbard, you could pass array element by reference, this way you do not to assign result of the function and just use changed old array. The code should then be modified appropriately.

按照Jonathon Hibbard的说明进行编辑,您可以通过引用传递数组元素,这样就不必分配函数的结果,只需使用修改过的旧数组。然后应该适当地修改代码。

#3


0  

First decode json by json_decode. You will get an array. Then follow this link to remove an index from an associative array. Then again decode it. It should work.

首先通过json_decode对json进行解码。你会得到一个数组。然后按照这个链接从关联数组中删除索引。然后再解码。它应该工作。

#4


0  

do something like:

做些什么:

$array = json_decode($some_json_string, true);
array_walk($array, function($value, $key) use(&$array) {
  if($key == "name") {
    unset($array[$key]);
  }
});

Edit: Cthulhu's answer won't get ya there without re-assigning it. Could use it as a reference though (equal to the walk. though if you want to use the map, its a bit better not to reallocate with a brand new array copy and just pass it by reference, then remove the key with an unset within it and move on.)

编辑:Cthulhu的答案不会在没有重新分配的情况下得到。可以用它作为参考(相当于步行)。不过,如果您想使用映射,最好不要重新分配一个全新的数组拷贝,然后通过引用传递它,然后在其中删除未设置的键,然后继续。

array_map(function(&$array) { unset($array['name']; }, $array);