How to use array values as keys without loops?

Asked
Active3 hr before
Viewed126 times

7 Answers

array
90%

Are there any gaps in the range of gravitational wave frequencies we can detect? , Student asked me if it is necessary to simplify fractions at the end of answering a question. I'm not sure how to respond ,So, as a conclusion: plain iteration with simple for loop is a winner (as mentioned in this answer). On a second place there is array_combine allowed only for new versions of PHP. And the worst case is using my own solution with closure and array_reduce., Horror short story told through a collection of postcards about a man killed by an ancient snake goddess

Simply use array_combine along with the array_column as

array_combine(array_column($array, 'id'), array_column($array, 'name'));

Or you can simply use array_walk if you have PHP < 5.5 as

$result = array();
array_walk($array, function($v) use( & $result) {
   $result[$v['id']] = $v['name'];
});

For future user who has PHP > 5.5 can simply use array_column as

array_column($array, 'name', 'id');
load more v
88%

Looping is time consuming, we all know that. That's exactly something I'm trying to avoid, even though it's on a small scale. Every bit helps. Well, if it's unset of course :),Simply use array_combine along with the array_column as,What I'm trying to do is to convert this array as follows:,So, as a conclusion: plain iteration with simple for loop is a winner (as mentioned in this answer). On a second place there is array_combine allowed only for new versions of PHP. And the worst case is using my own solution with closure and array_reduce.

I've got an array:

array(3) {
   '0' => array(2) {
         'id' => 1234,
         'name' => 'blablabla',
      },
      '1' => array(2) {
         'id' => 1235,
         'name' => 'ababkjkj',
      },
      '2' => array(2) {
         'id' => 1236,
         'name' => 'xyzxyzxyz',
      },
}

What I'm trying to do is to convert this array as follows:

array(3) {
   '1234' => 'blablabla',
   '1235' => 'asdkjrker',
   '1236' => 'xyzxyzxyz',
}
load more v
72%

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference. , A notice will be generated if there aren't enough array elements to fill the list(): , The second form will additionally assign the current element's key to the $key variable on each iteration. , You can provide fewer elements in the list() than there are in the nested array, in which case the leftover array values will be ignored:

foreach(iterable_expression as $value)
statement
foreach(iterable_expression as $key => $value)
statement
load more v
65%

We can use the foreach loop to access and use the elements in an array of simple values, such as strings or numbers.,In PHP, the foreach loop can be used to loop over an array of elements. It can be used in many ways such as,We will use the same foreach loop to iterate over an array of key-value pairs.,Using the foreach loop with simple values:

load more v
75%

Search Answer Titles

var arr = ['one', 'two', 'three', 'four', 'five'];
$.each(arr, function(index, value) {
   console.log('The value at arr[' + index + '] is: ' + value);
});
load more v
40%

Below, we loop through the numbers array using the JavaScript map() function. We return a <li> element for each item. Finally, we assign the resulting array of elements to listItems:,Given the code below, we use the map() function to take an array of numbers and double their values. We assign the new array returned by map() to the variable doubled and log it:,Let’s assign a key to our list items inside numbers.map() and fix the missing key issue.,We can refactor the previous example into a component that accepts an array of numbers and outputs a list of elements.

load more v
22%

The for...of statement iterates over values that the iterable object defines to be iterated over.,The for...in statement iterates over the enumerable properties of an object.,Object whose iterable properties are iterated., You can also iterate over an object that explicitly implements the iterable protocol:

for (variable of iterable) {
   statement
}
load more v

Other "array-undefined" queries related to "How to use array values as keys without loops?"