Conveying common array in associative array

1

I need to transform this array:

Array
            (
                [0] => Array
                    (
                        [name] => Email
                        [value] => [email protected]
                    )

                [1] => Array
                    (
                        [name] => Telefone
                        [value] => 22222222222222
                    )

                [2] => Array
                    (
                        [name] => Code
                        [value] => D9CI8C
                    )

                [3] => Array
                    (
                        [name] => Nome
                        [value] => Igor de Oliveir
                    )

                [4] => Array
                    (
                        [name] => Problema
                        [value] => teste
                    )

            )

    )

In an assoc array, which is in the format:

    array(
   ['email'] = [email protected]
   ['telefone'] = 2222222
)

and so on, I tried to apply a foreach, but it returns me the same original array:

$arrayfields = $node['fields'];
    $newarrray = array();
    foreach ($arrayfields as $name => $value) {
      $newarrray[$name] = $value;

    }
    
asked by anonymous 24.05.2018 / 20:50

3 answers

2

If you always have the vector with par 'name' and 'value', you can do more simplified:

$arrayfields = $node['fields'];
$newarrray = array();
foreach ($arrayfields as $item){
    $newarrray[$item['name']] = $item['value'];
}
    
24.05.2018 / 21:00
0

You can not because you have array within your first array where you run foreach , so it will work:

<?php

$array_errado = array(
    0 => array(
        'name' => 'email',
        'value' => '[email protected]'
    ),
    1 => array(
        'name' => 'telefone',
        'value' => '3445-4545'
    ),
    2 => array(
        'name' => 'code',
        'value' => 'D9CI8C'
    )
);

$array_certo = array();

foreach ($array_errado as $sub_array) {
    foreach ($sub_array as $key => $value) {
        $array_certo[$key] = $value;
    }
}
?>
    
24.05.2018 / 20:58
0

There are two ways to do this, the first with array_column() to extract a column / key from an array that in this case will do this twice, one for name and another for value for array_combine() creates a new array where keys are the extraction result of name and the extraction values of value

Example - idone

<?php

$arr = array(array('name' => 'email', 'value' => '[email protected]'),
             array('name' => 'code', 'value' => 'D9CI8C'),
             array('name' => 'nome', 'value' => 'fulano'),
             array('name' => 'problema', 'value' => 'algo errado')
);

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

print_r($novo);

Or just with a foreach:

$novo = array();
foreach($arr as $item) $novo[$item['name']] = $item['value'];

print_r($novo);

Example - ideone

In both codes the output is:

Array
(
    [email] => [email protected]
    [code] => D9CI8C
    [nome] => fulano
    [problema] => algo errado
)
    
24.05.2018 / 20:57