Turn a JSON string into a non-associative array in PHP

Asked

Viewed 95 times

1

Good afternoon,

would like to convert a string into a non-associative array in PHP, functions like json_encode/json_encode are not working properly.

String:

word = '{"world":["EC","Supply"],"regional":["CSC","Regional RJ/Verticalized"],"entityAgrupada":["G&G","BSA - Graphic"],"unit":["GGI","BSA - Graphic"],"area":null};

I’m trying to get something like:

[0=>["EC","Supply"],1=>["CSC","Regional RJ/Verticalized"],2=>["G&G","BSA - Graphic"],3=>["GGI","BSA - Graphic"]]

Thank you in advance for your attention.

2 answers

1


Try this:

$resultado = array_values( json_decode( $palavra, true ) );

The second parameter of json_decode() define whether it returns as object (false) or associative array (true). Once with an array vc only need to discard the keys using array_values();

0

   <?php


    $data = '{ "mundo":["CE","Supply"], "regional":["CSC","Regional RJ/Verticalizadas"], "entidadeAgrupada":["G&G","BSA - Gráfica"], "unidade":["GGI","BSA - Gráfica"],"area":null }';


    $data1 = json_decode(trim($data), true);

    $data2 = $data1['mundo'];

    echo "<br>"; 
    print_r( $data1); 
    echo "<br>"; 
    echo "<br>";
     echo "<br>"; 
    print_r( $data2);

Upshot:

Array ( [world] => Array ( [0] => CE [1] => Supply ) [regional] => Array ( [0] => CSC [1] => Regional RJ/Verticalized ) [entidadeAgrupada] => Array ( [0] => G&G [1] => BSA - Graphic ) [unit] => Array ( [0] => GGI [1] => BSA - Graphic ) [area] => )

Array ( [0] => CE [1] => Supply )

Browser other questions tagged

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