PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 23, 2022

[FIXED] How can I decode an array formatted in json in php?

 July 23, 2022     arrays, json, php     No comments   

Issue

I am setting up dialogflow in php to retrieve input from google assistant.

I want to use a parameter I got from dialogflow.

But I don't know how to get it out of the json.

I've already got a function that gets parameters from the json, but it's in a weird format.

How can I get only the pet_name in a variable?

The formats are:

When I use print_r I get:

Array
(
    [pet_name] => gizmo
)

And when I do var_dump I get:

array(1) {
  ["pet_name"]=>
  string(5) "gizmo"
}

Hope this makes it clearer


Solution

You can get the name from your index as like:

<?
$string = '{"pet_name":"gizmo"}';
$parsed = json_decode($string);
echo $parsed->pet_name;
?>

if you are using true as a second param in json_decode() then this will return an array and you can get as like:

<?
$string = '{"pet_name":"gizmo"}';
$parsed = json_decode($string,true);
echo $parsed['pet_name'];
?>


Answered By - devpro
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing