Common conveter array in associative array

Asked

Viewed 82 times

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 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;

    }

3 answers

2


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

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

0

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

0

You couldn’t because you have one array within your first array in which you rotate the foreach, this 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;
    }
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.