Convert every two values of an associative array into key-value pairs



PHP Snippet 1:

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

PHP Snippet 2:

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

PHP Snippet 3:

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

PHP Snippet 4:

$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];
}

PHP Snippet 5:

Array
(
    [orange] => 100
    [bannana] => 200
    [apple] => 300
)

PHP Snippet 6:

$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

PHP Snippet 7:

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

PHP Snippet 8:

array (
  'orange' => '100',
  'bannana' => '200',
  'apple' => '300',
)