5 ], 3 => [ "id" => 6 ], 4 => [ "id" => 7 ], ); var_dump(array_column($items,"id...">

PHP array_column - how to save keys?

$items = array( 1 => [ "id" => 5 ], 3 => [ "id" => 6 ], 4 => [ "id" => 7 ], ); var_dump(array_column($items,"id")); 

result

 array (size=3) 0 => int 5 1 => int 6 2 => int 7 

But how can I save the $items key to get this below?

 array (size=3) 1 => int 5 3 => int 6 4 => int 7 
+7
source share
7 answers
 foreach(key($parameters) as $key) { print($key); } 

You can also save this result in other variables if you wish.

And to show both keys and values, try the following:

 foreach ($parameters as $key => $value) { echo $key . ' = ' . $value . '<br>'; } 
+1
source

See if this can help.

 array_filter(array_combine(array_keys($items), array_column($items, 'id'))); 
+14
source

Looking for the same solution and combine some tricks, I created this:

 $userdb=Array ( "test1" => array ( 'uid' => '100', 'name' => 'Sandra Shush', 'url' => 'urlof100' ), "test2" => array ( 'uid' => '5465', 'name' => 'Stefanie Mcmohn', 'pic_square' => 'urlof100' ), "test3" => array ( 'uid' => '40489', 'name' => 'Michael', 'pic_square' => 'urlof40489' ) ); echo $key = array_search( 40489, array_filter( array_combine( array_keys($userdb), array_column( $userdb, 'uid' ) ) ) ); 

The result is "test3".

Work with arrays or named arrays.

+4
source

I think this is the fastest way to save keys without loops and iterations

 array_diff(array_combine(array_keys($items), array_column($items, 'id')), [null]) 
+3
source

Another alternative is to use array_map

$result = array_map(function($item) {return $item['id'];}, $items);

+1
source

I wrote a simple array_column_keys function that has the same parameters as array_column .

 /** * Return the values from a single column in the input array by keeping the key * * @param array $array A multi-dimensional array (record set) from which to pull a column of values. * @param mixed $column The column of values to return. This value may be the integer key of the column you wish to retrieve, or it may be the string key name for an associative array. It may also be NULL to return complete arrays (useful together with index_key to reindex the array). * @param mixed $index_key [optional] The column to use as the index/keys for the returned array. This value may be the integer key of the column, or it may be the string key name. * * @return array Returns an array of values representing a single column from the input array. */ function array_column_keys($array, $column, $index_key = null) { $output = []; foreach ($array as $key => $item) { $output[@$item[$index_key] ?? $key] = @$item[$column]; } return array_filter($output, function($item) { return null !== $item; }); } 

The third parameter, index_key , is what I need too. This will answer the question when setting the third parameter to null as in the following example:

 $result = array_column_keys($items, 'id'); 

... and also allow you to determine the key value

 $result = array_column_keys($items, 'id', 'any_key'); 

This will lead to

 array (size=3) string 'any_value1' => int 5 string 'any_value2' => int 6 string 'any_value3' => int 7 
0
source

For your example with only one id column

 array_map('current', $items); 
-1
source

Source: https://habr.com/ru/post/1207940/


All Articles