Problem using json_decode () (Array to string conversion) php

2

I'm trying to use json_decode () as follows:

<?php
//Este es el json
$fol ='[{"name":"Taili Silva","screen_name":"taili_silva","id":837254166619770880,"id_str":"837254166619770880","connections":["following","followed_by"]}]';

$str =json_decode($fol, TRUE);

echo $str['connections'];
?>

and this is the error:

  

Notice: Array to string conversion in line 3

They could give me some explanation or solution

    
asked by BotXtrem Solutions 30.09.2017 в 02:55
source

2 answers

1

The notice

  

Array to string conversion

is given in this case because you are trying to print an array with echo , which is impossible.

If you want to print the array just to verify what's inside it, you can use print_r , var_dump or var_export .

If you need to save it in a variable or print it with echo you have to go through the array in a loop and recover its values.

Also, in the data you have, connections is in turn an array:

        ...    [connections] => Array
                (
                    [0] => following
                    [1] => followed_by
                ) ...

Therefore, it is impossible to access your values directly as you are doing. If you do not want to use loops anyway, you can access each value in this elegant and impractical way, especially in arrays with many values:

echo $arr[0]["connections"][0];

Print:

following

Y

echo $arr[0]["connections"][1];

Print:

followed_by

Let's see:

<?php

$fol='[{
    "name": "Taili Silva",
    "screen_name": "taili_silva",
    "id": 837254166619770880,
    "id_str": "837254166619770880",
    "connections": ["following", "followed_by"]
}]';


$arr =json_decode($fol, TRUE);

echo "VER ARRAY COMPLETO:\n\n";
print_r($arr);

echo "\n\nBUSCAR CONNECTIONS EN ARRAY:\n";


foreach ($arr as $row)
{
    $arrConnections=$row["connections"]; //Es un array
}

echo "\n\nCONNECTIONS ES A SU VEZ UN ARRAY:\n";


print_r($arrConnections);

echo "\n\nVER VALORES EN ARRAY CONNECTIONS:\n";


foreach ($arrConnections as $row)
{
    echo $row."\n"; //Aquí podemos usar los valores como variables o usar echo
}


?>

Result:

--VER ARRAY COMPLETO:

Array
(
    [0] => Array
        (
            [name] => Taili Silva
            [screen_name] => taili_silva
            [id] => 837254166619770880
            [id_str] => 837254166619770880
            [connections] => Array
                (
                    [0] => following
                    [1] => followed_by
                )

        )

)


--BUSCAR CONNECTIONS EN ARRAY:    
--CONNECTIONS ES A SU VEZ UN ARRAY:

Array
(
    [0] => following
    [1] => followed_by
)


--VER VALORES EN ARRAY CONNECTIONS:

following
followed_by

In addition, your JSON has another supplementary situation, which is that, being between [] it is a JSON array, which is not the same as a JSON object, since the latter are enclosed in {} .

If you want to directly access a property of the array then you have to put the index in front of it.

For example:

print_r($arr[0]["connections"]);

Will result in:

Array
(
    [0] => following
    [1] => followed_by
)

If it were a JSON object (without [] ):

$fol='{
    "name": "Taili Silva",
    "screen_name": "taili_silva",
    "id": 837254166619770880,
    "id_str": "837254166619770880",
    "connections": ["following", "followed_by"]
}';

Then you could read it like this:

print_r($arr["connections"]);

Result:

Array
(
    [0] => following
    [1] => followed_by
)

P.D .: If interested, I will later complete a demo with more details.

    
answered by 30.09.2017 / 03:20
source
1

Use var_dump($str); to print the arrays or print_r($str);

echo does not print arrays

    
answered by 30.09.2017 в 03:14