How to print a specific JSON value in PHP

Asked

Viewed 331 times

0

I have the following JSON file and would like to print only the value of the 'name' field, which is within 'contact'. I am using PHP language.

C:\wamp64\www\macmind\client-search.php:31:
array (size=1)
  'retorno' => 
    array (size=3)
      'status_processamento' => string '3' (length=1)
      'status' => string 'OK' (length=2)
      'contato' => 
        array (size=41)
          'id' => string '393021930' (length=9)
          'codigo' => string '21656' (length=5)
          'nome' => string '  REST SIDER DE VOLTA REDONDA COMERCIO DE ALIMENTOS LTDA ME' (length=59)
          'fantasia' => string 'GIRAFFAS' (length=8)
          'tipo_pessoa' => string 'J' (length=1)
          'cpf_cnpj' => string '08.348.956/0001-90' (length=18)
          'ie' => string '  78.191.830' (length=12)
          'rg' => string '' (length=0)
          'im' => string '' (length=0)
          'tipo_negocio' => string '' (length=0)
          'endereco' => string 'RUA DOZE,300 ' (length=13)
          'numero' => string '300' (length=3)
          'complemento' => string 'Loja 309' (length=8)
          'bairro' => string 'Vila Santa Cecília' (length=19)
          'cep' => string '27260-315' (length=9)
          'cidade' => string 'Volta Redonda' (length=13)
          'uf' => string 'RJ' (length=2)
          'pais' => string '' (length=0)
          'endereco_cobranca' => string '' (length=0)
          'numero_cobranca' => string '' (length=0)
          'complemento_cobranca' => string '' (length=0)
          'bairro_cobranca' => string '' (length=0)
          'cep_cobranca' => string '' (length=0)
          'cidade_cobranca' => string '' (length=0)
          'uf_cobranca' => string '' (length=0)
          'contatos' => string '' (length=0)
          'fone' => string '(24) 7836-5440' (length=14)
          'fax' => string '' (length=0)
          'celular' => string '(24) 7836-5442' (length=14)
          'email' => string '[email protected]' (length=26)
          'email_nfe' => string '' (length=0)
          'site' => string '' (length=0)
          'limite_credito' => int 0
          'situacao' => string 'A' (length=1)
          'obs' => string '' (length=0)
          'id_lista_preco' => int 0
          'id_vendedor' => string '0' (length=1)
          'nome_vendedor' => string '' (length=0)
          'data_criacao' => string '13/11/2017 09:02:21' (length=19)
          'tipos_contato' => 
            array (size=1)
              ...
          'pessoas_contato' => 
            array (size=0)
              ...
  • Utilize $obj = json_decode('seu-json'); var_dump( $obj->retorno->contato->nome );

1 answer

0

Have you looked at the API on json_encode of PHP? You just need to get inside your array specifically what value you want returned to the user. Example:

$objectJson= [
  'nome'=>$seuArray['retorno']['contato']['nome']
];
json_encode($objetoJson);
// A saída deve ser: {"nome":"Nome pego do array"}

Browser other questions tagged

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