Array vertical slice. Is there any built-in function? an array with many keys, key1, key2 ,...
I want to generate a new array based on a key, which is to extract all values of a specific key to form an array.
Is there any built-in function in PHP?
This is very common. I think there should be a built-in method.
Is a vertical slice for the array key.
Is to implement the following functions
If (count ($ arr)> 0 ){
Foreach ($ arr as $ v ){
Array_push ($ newArr, $ v ['key1']);
}
}
Is there any built-in method?
Reply to discussion (solution)
Is for a key, and then take out the values under this key to form a new array
Obviously, you are referring to a two-dimensional array.
Php arrays are nested and not traditional arrays. So it's impossible to predict your operation and built-in functions
Solve this problem only by traversing it by yourself
Generally, the source of the array is database query.
The required array has been constructed when reading the query data, so there is no need to transpose the array.
To make the code more elegant and easy to read, you can use the iterator provided by SPL.
However, the efficiency may not be faster than self-writing a traversal.
Just a few lines of code are not complicated.