-1

I need to convert an associative array into a new associative array where the original keys are ignored and every two consecutive values become new key-value pairs.

Input:

Array
(
    [SELECT0] => 'orange'
    [INPUT0] => '100'
    [SELECT1] => 'bannana'
    [INPUT1] => '200'
    [SELECT2] => 'apple'
    [INPUT2] => '300'
)

Desired output:

Array
(
    [orange] => '100'
    [bannana] => '200'
    [apple] => '300'
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
user315338
  • 67
  • 6

4 Answers4

3

Here is one way combining three PHP array functions:

$result = array_combine(...array_map(null, ...array_chunk($array, 2)));

array_chunk gives you

[
    ['orange', '100'],
    ['banana', '200'],
    ['apple', '300']
];

array_map converts that to:

[
    ['orange', 'banana', 'apple'],
    ['100', '200', '300'],
];

Which can feed directly into array_column.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • I don't think i fully understand your answer. i was trying to use that first short code and i am getting the right format, but for some reason it doesn't return all values, only some, and a weird order... – user315338 Aug 12 '19 at 22:13
  • My real array is `array(12) { ["SELECT0"]=> string(3) "000" ["INPUT0"]=> string(2) "50" ["SELECT1"]=> string(3) "000" ["INPUT1"]=> string(3) "150" ["SELECT2"]=> string(4) "5260" ["INPUT2"]=> string(3) "200" ["SELECT3"]=> string(4) "5261" ["INPUT3"]=> string(3) "300" ["SELECT4"]=> string(3) "000" ["INPUT4"]=> string(3) "400" ["SELECT5"]=> string(4) "5262" ["INPUT5"]=> string(3) "500" } ` – user315338 Aug 12 '19 at 22:16
  • What i get with your code is: `array(4) { ["000"]=> string(3) "400" [5260]=> string(3) "200" [5261]=> string(3) "300" [5262]=> string(3) "500" } ` – user315338 Aug 12 '19 at 22:20
  • Yes, that's because a PHP array can't have duplicate keys, so you can't really get the structure you're going for from that input array without losing data. – Don't Panic Aug 12 '19 at 22:21
  • Since you can't have multiple '000' keys, you just end up with whatever the last value is. You're going to have the same problem with any of the other answers, I believe. – Don't Panic Aug 12 '19 at 22:22
1

All you need to do, is loop over all you array values and then take every odd one as key and the next index as value.

$arr = [
    'SELECT0' => 'orange',
    'INPUT0' => '100',
    'SELECT1' => 'bannana',
    'INPUT1' => '200',
    'SELECT2' => 'apple',
    'INPUT2' => '300'
];

$arr = array_values($arr);
$newData = [];
for ($i = 0; $i < count($arr); $i++) {
    $newData[$arr[$i]] = $arr[++$i];
}

Now $newData contains this:

Array
(
    [orange] => 100
    [bannana] => 200
    [apple] => 300
)
Manuel Mannhardt
  • 2,191
  • 1
  • 17
  • 23
1

assuming the format will never change ...

$new=array();//start a new array
$count=count($array)/2; //get the number of items to loop through


for ($i = 0; $i < $count; $i++) { //loop

$k='SELECT'.$i; //key
$v='INPUT'.$i; //value

$new[$array[$k]]=$array[$v]; //create new array

}

print_r($new); //display
1
  1. Remove the uselss original keys from the input array with array_values()
  2. Group the data into 2-element subarrays
  3. Iterate the pairs and use "array destructuring" to temporarily declare the $newKey and then push the related value into the result array using $newKey. The foreach's body is not required.

Code: (Demo)

$result = [];
foreach (array_chunk(array_values($arr), 2) as [$newKey, $result[$newKey]]);
var_export($result);

Output:

array (
  'orange' => '100',
  'bannana' => '200',
  'apple' => '300',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 1
    +1 for using "disembodied foreach", hadn't ever thought it works without a body. Provides an interesting alternative for cases where `array_map` is basically what you want but you need keys, and `array_walk` feels clunky. – Markus AO Apr 21 '22 at 10:49
  • 1
    This is my latest epiphany while toying with array destructuring: [While destructuring an array, can the same element value be accessed more than once?](https://stackoverflow.com/q/71807960/2943403). I was very surprise to discover that that coding attempt actually worked. – mickmackusa Apr 21 '22 at 10:51