How to retrieve the value of an array

3

Well I have the following array that is inside a $resultado variable:

{"result":[{"fone":"","email":"","id":1,"nome":"GERAL","token":"BE5DEA91EB28E98F053466E98082908545E3DCA5"}]}

I need to recover the token.

I tried this, but it did not work:

$resultado[4]

But it is returning me s

    
asked by anonymous 26.04.2017 / 15:18

2 answers

4

Use json_decode and then access the keys as follows:

<?php

$json = '{"result":
    [{"fone":"","email":"",
      "id":1,"nome":"GERAL",
      "token":"BE5DEA91EB28E98F053466E98082908545E3DCA5"}
    ]}';

$array = json_decode($json, true);

echo $array['result'][0]['token'];

IDEONE

>     
26.04.2017 / 15:35
1

Your string is in the form of JSON , first use the #

<?php

$resultado = '{"result":[{"fone":"","email":"","id":1,"nome":"GERAL","token":"BE5DEA91EB28E98F053466E98082908545E3DCA5"}]}';
$resultado = json_decode($resultado);
//Pelo formato do json você pode ter mais de um result
foreach($resultado->result as $res){
    echo $res->token;
}

//Ou simplesmente
echo $resultado->result[0]->token;

IDEONE

    
26.04.2017 / 15:35