How can I create an array from the values of another array's key?

how to store an array in another array in java
copy array to another array javascript
php array same key different value
array map
copy array to another array in c
php merge multidimensional array by key value
array_combine multidimensional php

I have an array as follows:

$arr1 = array(
  0 => array(
    'name' => 'tom',
    'age' => 22
  1 => array(
    'name' => 'nick',
    'age' => 18

However I want to create an array from it which consists of all the names, so it would become:

$arr2 = array('tom', 'nick');

I have looked at array_filter(), but that would not work as this is a multi-dimensional array!


How can I create an array with the values of a specific key (name) from another multi-dimensional array?

Newer versions of PHP allow using array_map() with a function expression instead of a function name:

$arr2 = array_map(function($person) {
    return $person['name'];
}, $arr1);

But if you are using a PHP < 5.3, it is much easier to use a simple loop, since array_map() would require to define a (probably global) function for this simple operation.

$arr2 = array();

foreach ($arr1 as $person) {
    $arr2[] = $person['name'];

// $arr2 now contains all names

array_combine - Manual, array_combine — Creates an array by using one array for keys and another for its values Cycling on each keys values, making an offset for each */ Create an Array. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets:

This can be done in still more simple way by using array_column

$arr2= array_column($arr1, 'name');

print_r($arr2); //Array ( [0] => tom [1] => nick )

array_column is used to get the columns of a sub-array.

Array Functions - Manual, array_change_key_case — Changes the case of all keys in an array; array_chunk Creates an array by using one array for keys and another for its values index association; compact — Create array containing variables and their values� The reason for Array.Copy() being so fast is that it doesn't allocate any memory. However, if you require your arrays to be copied to a new region of memory each time, then Array.Copy() is no longer the fastest method. Here are my performance results: Copy: 0 ms Copy (with allocation): 449 ms Clone: 323 ms ToArray: 344 ms And here's the code I

$array = array(0 => array('name' => 'tom', 'age' => 22), 1 => array('name' => 'nick', 'age' => 18));
foreach($array as $arr => $a){
    $names[] = $array[$arr]["name"];

print_r($names); //Array ( [0] => tom [1] => nick ) 

How to copy elements of one array to another array in Java, There are multiple ways to copy elements from one array in Java like you can manually copy elements by using a loop, create a clone of the array, use Arrays. I'm trying to create a new variable array out of the unique values that are in another array but I'm not getting the desired result. Original Array # echo ${owner[*]} 390920ad-2858-e651-a4af-

if you are using Laravel, then simply use array_pluck:

$arr2 = array_pluck($arr1 , 'name');

Copy elements of an array into another array in JavaScript, The solution should in add elements of an array to another array and should not create a new array. 1. Array.prototype.push(). In order to append values of an� Or you can make your second array an array of pointers to the previous array. float *allData[] = { data1, data2 }; or even: float (*allData[])[5] = { &data1, &data2 }; For all of the above, the expression allData[1][2] should yield the float value 253 (the third element of the second array).

Create a new array on the basis of another array, Then in all the other places I want to put 100 minus the value in V for each column. obtaining: A = [22 40 36 100 22 30 50 36� First, let’s create a one-dimensional array or an array with a rank 1. arange is a widely used function to quickly create an array. Passing a value 20 to the arange function creates an array with values ranging from 0 to 19. 1 2 3 import Numpy as np array = np.arange(20) array

Array.Copy Method (System), Copies a range of elements in one Array to another Array and performs type casting and boxing as required. In Excel, an Array Formula allows you to do powerful calculations on one or more value sets. The result may fit in a single cell or it may be an array. An array is just a list or range of values, but an Array Formula is a special type of formula that must be entered by pressing Ctrl + Shift + Enter.

ES6 Way to Clone an Array, Array.from is Another Way to Clone Array To create a real copy of an array, you need to copy over the value of the array under a new value variable. That way� In order to use the above-declared array variable, you need to instantiate it and then provide values for it. The array is instantiated using ‘new’. The general syntax of instantiating is as follows: array_name = new data_type [size];

  • It is still efficient to create a global function than a complex loop.
  • @Christian Sciberras: True, but it pollutes the namespace with unneeded micro-functions. The loop is not that complex here. It is a quite simple iteration (assuming that the initial array does not contain massive loads of data).
  • Actually, for .. each is quicker. Tested it and it seems 2 - 3x faster. Couldn't tell you why though....
  • both array_map and foreach() are O(n), but the array_map call has to build the function and call it for reach item in the array, in addition to doing the actual iteration. The foreach() loop doesn't have the function overhead, so it's faster. array_map should only really be used if you're doing something complicated with each item in the array. It's overkill for this particular problem.
  • Nathan describes it perfectly, the exact same reason I said "complex loop" (which is not the case here). +1
  • This doesn't seem to be a standard PHP function. array_column can be used instead.
  • Thanks for your note. I use array_pluck in Laravel. I have updated my answer.