0

I have a string in database that actualy is an json object. How can i convert it to see as object/array instead string? this is string

[{"id_piesa":"8","cantitate_piesa":"12","garantie_piesa":false},{"id_piesa":"30","cantitate_piesa":1,"garantie_piesa":true}]

3 Answers3

2

You can use json_decode() php function.

$json_string = '[{"id_piesa":"8","cantitate_piesa":"12","garantie_piesa":false},{"id_piesa":"30","cantitate_piesa":1,"garantie_piesa":true}]';
$array = json_decode( $json_string , TRUE);

print_r( $array);

Output:

Array
(
    [0] => Array
        (
            [id_piesa] => 8
            [cantitate_piesa] => 12
            [garantie_piesa] => 
        )

    [1] => Array
        (
            [id_piesa] => 30
            [cantitate_piesa] => 1
            [garantie_piesa] => 1
        )

)

if you add TRUE as second argument the returned objects will be converted into associative arrays.

Here you can find the complete documentation.

mitkosoft
  • 5,262
  • 1
  • 13
  • 31
Sfili_81
  • 2,377
  • 8
  • 27
  • 36
0
$json_string = '[{"id_piesa":"8","cantitate_piesa":"12","garantie_piesa":false},{"id_piesa":"30","cantitate_piesa":1,"garantie_piesa":true}]';
$php = json_decode( $json_string );

print_r( $php );

Have a look at this: https://www.php.net/manual/de/function.json-decode.php

Robin Gillitzer
  • 1,603
  • 1
  • 6
  • 17
0
$str = '[{"id_piesa":"8","cantitate_piesa":"12","garantie_piesa":false},{"id_piesa":"30","cantitate_piesa":1,"garantie_piesa":true}]';
$jsonArr = json_decode( $str ,true);
var_dump($jsonArr);