0

I have an array looks like this:

 Array
     (
        [0] => Array
               (
                  [name] => color
                  [value] => red
               )

        [1] => Array
               (
                  [name] => shape
                  [value] => square
               )

        [2] => Array
              (
                  [name] => price
                  [value] => $15
              )
     )

I want to have a result like this:

    $myArr = array (
        'color' => 'red', 
        'shape' => 'square',
        'price' => '$15'
    )

I have tried to loop but can not get it to work.

foreach($myArr as $id => $values){
    foreach ($values as $key => $value) {
        if($key == 'name') {
            //add to array key
        } else {
            //add to that array key value 
        }
    }
 }

hope you guys can help me with solution.

pexichdu
  • 849
  • 1
  • 10
  • 15
  • 1
    really, this has been here a million times already – Kevin Kopf Apr 05 '18 at 03:12
  • 1
    Possible duplicate of [How to "flatten" a multi-dimensional array to simple one in PHP?](https://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php) – Aluan Haddad Apr 05 '18 at 03:14
  • Thanks guys, I actually knew it's very basic but don't know what term to search for the right answer, my search result is always something else. – pexichdu Apr 05 '18 at 03:30

3 Answers3

2

You can use array_column and array_combine

$arr = array(
    array("name" => 'color',"value" => 'red'),
    array("name" => 'shape',"value" => 'square'),
    array("name" => 'price',"value" => '$15')
);

$newArr = array_combine(array_column($arr,'name'),array_column($arr,'value'));

echo "<pre>";
print_r( $newArr );
echo "</pre>";

This will result to:

Array
(
    [color] => red
    [shape] => square
    [price] => $15
)

Doc: array_column, array_combine

Eddie
  • 26,593
  • 6
  • 36
  • 58
1
$a = [
    0 => [
        "name" => "color",
        "value" => "red",
    ],
    1 => [
        "name" => "shape",
        "value" => "square",
    ],
    2 => [
        "name" => "price",
        "value" => "$15",
    ]
];

$b = [];

foreach($a as $v)
{
    $b[$v["name"]] = $v["value"];
}

var_dump($b);

result

array(3) {
  ["color"]=>
  string(3) "red"
  ["shape"]=>
  string(6) "square"
  ["price"]=>
  string(3) "$15"
}
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
0
$arr = array
     (
        0 => array
               (
                  'name' => 'color',
                  'value' => 'red'
               ),

        1 => array
               (
                  'name' => 'shape',
                  'value' => 'square'
               ),

        2 => array
              (
                  'name' => 'price',
                  'value' => '$15'
              )
);
$final_array =[];

foreach($arr as $key=> $val){
    $final_array[$val['name']] = $val['value'];
    }
echo "<pre>"; print_r($final_array);
Rafiqul Islam
  • 1,636
  • 1
  • 12
  • 25