json to array php

Today, We want to share with you json to array php.In this post we will show you php json foreach key => $value, hear for php read json file.

How to Encode JSON Data in PHP?

we will give you demo and example for implement.In this post, we will learn about Convert And Loop Through JSON With PHP And JavaScript Arrays/Objects with an example.

converted into JSON by using the PHP function

<?php
$myObj->name = "Rahul";
$myObj->age = 30;
$myObj->task = "New York";

$myJSON = json_encode($myObj);

echo $myJSON;
?>

Convert JSON String to PHP Array or Object

<?php
  // JSON string
  $memberJSON = '[{"name":"Kritika Raval","gender":"male"},{"name":"Ravi Dhameliya","gender":"male"},{"name":"Sejal Ramani","gender":"female"}]';

  $members_info = json_decode($memberJSON, true);
  print_r($members_info);        
  echo $members_info[0]["name"]; 

  $someObject = json_decode($memberJSON);
  print_r($someObject);   
  echo $someObject[0]->name; 
?>

Loop through PHP Array or Object

<?php

  $members_info = ...;
  foreach ($members_info as $key => $value) {
    echo $value["name"] . ", " . $value["gender"] . "<br>";
  }

  // Loop through Object
  $memberObject = ...;
  foreach($someObject as $key => $value) {
    echo $value->name . ", " . $value->gender . "<br>";
  }
?>

Convert PHP Array or Object to JSON String

<?php
  // Array
  $members_info = [
    [
      "name"   => "Jayesh Vaghela",
      "gender" => "male"
    ],
    [
      "name"   => "Dipik Bhanderi",
      "gender" => "male"
    ],
    [
      "name"   => "Mitali sagpariya",
      "gender" => "female"
    ]
  ];

  // Convert Array to JSON String
  $memberJSON = json_encode($members_info);
  echo $memberJSON;
?>

How to convert JSON string to array?

json_decode('{member_key:"98256Es3d"}');         
json_decode('{"member_key":"98256Es3d"}', true); // returns array("member_key" => "98256Es3d")
json_decode('{"member_key":"98256Es3d"}');       

$data = json_decode($your_json_string, TRUE);

Example 1:

<?php 
 
$arr = array ( 
	

	array( 
		"name" => "Virat Kohali", 
		"age" => "20"
	), 
	array( 
		"name" => "Mayur Dhameliya", 
		"age" => "21"
	), 
	array( 
		"name" => "Chirag Savani", 
		"age" => "20"
	) 
); 

// Function to convert array into JSON 
echo json_encode($arr); 

?> 

Example-2:

<?php 
$arr = array ( 
	"first"=>array( 
		"id"=>1, 
		"product_name"=>"Vijay", 
		"cost"=>69 
	), 
	"second"=>array( 
		"id"=>2, 
		"product_name"=>"Dinesh", 
		"cost"=>41 
	), 
	"third"=>array( 
		"id"=>3, 
		"product_name"=>"Rakesh", 
		"cost"=>9898 
	) 
); 

// Function to convert array into JSON 
echo json_encode($arr); 

?> 

JSON String to Multidimensional Array

$json = '[
    {
        "title": "Online Web Tutorials",
        "website": "w3school.com"
    },
    {
        "title": "THE LARGEST ONLINE Tutorials Library",
        "website": "Tutorialspoint.com"
    },
    {
        "title": "Web Development & Good Online education",
        "website": "Pakainfo.com"
    }
]';

$website = json_decode($json);
// access property of object in array
echo $website[1]->title;

I hope you get an idea about json to array php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.