1

I have an array that looks like this:

Array (
    [0] => Array ([order_variable_key] => surname [order_variable_value] => Hudsons )
    [1] => Array ( [order_variable_key] => number [order_variable_value] => 13 )
    [2] => Array ( [order_variable_key] => firstname [order_variable_value] => Dave )
)

I want to convert it to an array that looks like this:

Array(
    'surname' => Hudsons,
    'number' => 13,
    'firstname' => Dave);

I managed to isolate the values but was not able to pair them with eachother. I want to pair the values of the nested array with eachother.

Hicham
  • 13
  • 3
  • array_combine(array_column($array, 'order_variable_key'), array_column($array, 'order_variable_value')) – LF00 Dec 15 '16 at 13:22

3 Answers3

2
$new_array= array();
foreach($array_name1 as $key=>$val){
    $new_array[$val['order_variable_key']] = $val['order_variable_value'];
}
Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
1

Try this:

$arr = // your array;

$data = array();
foreach($arr as $val)
{
    $arr[$val['order_variable_key']] = $val['order_variable_value'];
}

print_r($arr);

will give your desired format.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
  • I finaly used the following solution: – Hicham Dec 15 '16 at 20:23
  • I finaly used the following solution: $result = //my array here; $data = array(); foreach($result as $val) { $data[$val['order_variable_key']] = $val['order_variable_value']; } print_r($data); – Hicham Dec 15 '16 at 20:24
1

you can use

$newArray = array();
foreach($array as $obj)
{
   $newArray[$obj['order_variable_key']] = $obj['order_variable_value']
}   
Abhishek
  • 1,008
  • 1
  • 16
  • 39