Arrays::key()
\nn\t3::Arrays()->key($key = 'uid', $value = false);
Use a field in the array as the key of the array, e.g. to get a list, whose key is always the UID of the associative array:
Example:
$arr = [['uid'=>'1', 'title'=>'Title A'], ['uid'=>'2', 'title'=>'Title B']];
\nn\t3::Arrays($arr)->key('uid'); // ['1'=>['uid'=>'1', 'title'=>'Title A'], '2'=>['uid'=>'2', 'title'=>'Title B']]
\nn\t3::Arrays($arr)->key('uid', 'title'); // ['1'=>'Title A', '2'=>'Title B']
Copied!
| @return array
Source Code
public function key( $key = 'uid', $value = false ) {
$arr = (array) $this;
$values = $value === false ? array_values($arr) : array_column( $arr, $value );
$combinedArray = array_combine( array_column($arr, $key), $values );
$this->exchangeArray($combinedArray);
return $this;
}
Copied!