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

Asked
Active3 hr before
Viewed126 times

7 Answers

valuesarraycreate
90%

How can I create an array with the values of a specific key (name) from another multi-dimensional array?, @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). – jwueller Dec 29 '10 at 2:28 , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,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.

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
load more v
88%

array_combine — Creates an array by using one array for keys and another for its values, Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values. , Returns the combined array, false if the number of elements for each array isn't equal. ,array_values() - Return all the values of an array

Array
   (
      [green] => avocado[red] => apple[yellow] => banana
   )
72%

The array_combine() is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of this new array.,$values_array: This is an array of values that is to be used in the new array.,$keys_array: This is an array of keys. If illegal values are passed as the key, then it will be converted into a string.,Below program illustrates the array_combine() function in PHP:

Examples:

Input: $array1 = ("Ram", "Akash", "Rishav");
$array2 = ('24', '30', '45');
Output:
   Array(
      [Ram] => 24[Akash] => 30[Rishav] => 45
   )

Input: $array1 = ("65824", "92547", "12045");
$array2 = ('1', '2', '3');
Output:
   Array(
      [65824] => 1[92547] => 2[12045] => 3
   )

Syntax:

array_combine($keys_array, $values_array)

Output:

Array
   (
      [Ram] => 24[Akash] => 30[Rishav] => 45
   )
load more v
65%

// two arrays one become keys and second becomes values
array_combine(array $keys, array $values);
load more v
75%

What are a key and value in an array?,A common problem faced by programers is looping over an enumerable dataset. This data can come in the form of arrays, lists, maps or other objects. In this article we will deal with this problem and learn 4 ways to loop through objects using javascript to retrieve multiple key-value pairs. ,Flattening an array is a process of reducing the dimensionality of an array. In other words, it a process of reducing the number of dimensions of an array to a lower number.,Different methods to obtain key-value pairs


var arr = {
   "one": 1,
   "two": 2,
   "three": 3
};
load more v
40%

Here are a few examples of creating arrays in PHP:,There are two different ways of creating arrays. One is to use array() to specify the elements as key-value pairs. The other method is to put all elements inside []. There are two important points that you should remember when creating associate arrays with key pairs.,To get a part of an array—for example, just the first three elements—use array_slice():,To generate an array with a range of keys and values, like hours in the day or letters, use range():

Here are a few examples of creating arrays in PHP:

$first = array(10, "Apple", 20, -18, "Monkey");
print_r($first);
/* Array
(
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
) */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r($second);
/* Array
(
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
) */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r($third);
/* Array
(
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
) */
load more v
22%

If you are working on PHP or other PHP framework and you want to create array of another array value. now you can see on following example how can you make array form another multidimensional array key's.,so, we can make this type of array from multi-dimensional array using array_column() funtion.,My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.,Laravel - Category Treeview Hierarchical Structure Example with Demo

	 $multi = array(['1'] => array('id' => 1, 'name' => 'hardik'), ['2'] => array('id' => 1, 'name' => 'vimal'), ['3'] => array('id' => 1, 'name' => 'harshad'), )
load more v

Other "values-array" queries related to "How can I create an array from the values of another array's key?"