Processing Multidimensional JSON Array with PHP
I assume you've decoded the string you gave with json_decode method, like...
$data = json_decode($json_string, TRUE);
To access the stats for the particular worker, just use...
$worker_stats = $data['workers']['myemail@gmail.com'];
To check whether it's alive, for example, you go with...
$is_alive = $worker_stats['alive'];
It's really that simple. )
How do I loop through multidimensional JSON array in PHP?
Your $data_drugs is no longer a json, after json_decode is an associative array.
You don't need any loop to see keys and values
$data_drugs = json_decode($json_drugs,true);
print_r($data_drugs);
/* or if you don't like inline */
echo'<pre>';
print_r($data_drugs);
echo'</pre>';
You can use var_dump($data_drugs)
- keys and values with types, probably you don't need this
But if you want to display keys and values more ...fancy use a recursive function
function show($x){
foreach($x as $key=>$val){
echo"<p>$key : ";
if(is_array($val)){ echo". . ."; show($val);}
else{ echo"$val</p>";}}}
show($data_drugs);
Multidimensional JSON Array In PHP?
Have a look at the data you get after json_decode
, you will probably get an array with only one key: donors
.
print_r($donors);
If so, do this instead when iterating:
foreach ($donors['donors'] as $donor) {
// do your work
}
How to build a multidimensional json array with PHP while statement?
This code when creating the schedule, creates each days events indexed by the date itself, then when a new event is found it will check if this date is already set. If so, it will increment the count and add the new event. The only complication is that it needs to check if it only has 1 existing entry - this needs to be converted to an array first. To remove the index, the json_encode()
uses array_values()
first...
while($schedule = $result_data->fetch_assoc()){
//start time conversion into american time
// 24-hour time to 12-hour time
$start_time =date("g:i a", strtotime($schedule['start_time']));
//end time conversion into american time
$end_time =date("g:i a", strtotime($schedule['end_time']));
$my_schedule_start .= $start_time;
$my_schedule_end .= $end_time;
$schedule_date= $schedule['cleaning_date'];
if ( isset($result[$schedule_date])){
// Increment count
$result[$schedule_date][$schedule_date]["number"]++;
// Convert single item to array
if ( !isset($result[$schedule_date][$schedule_date]["dayEvents"][0])) {
$result[$schedule_date][$schedule_date]["dayEvents"] =
[$result[$schedule_date][$schedule_date]["dayEvents"]];
}
// Add in new event to dayEvents
$result[$schedule_date][$schedule_date]["dayEvents"][] = [
"title"=>$schedule['first_name'].' '.$schedule['last_name'],
"status"=>$schedule['cleaning_status'],
"time"=>$start_time .' - '.$end_time
];
}
else {
// Create new days schedule
$result[$schedule_date] = [
"$schedule_date" => [
"number"=>'1',
"badgeClass"=>'',
"url"=>'url',
"dayEvents"=>[
"title"=>$schedule['first_name'].' '.$schedule['last_name'],
"status"=>$schedule['cleaning_status'],
"time"=>$start_time .' - '.$end_time
]
]
];
}
}
header('Content-Type: application/json');
echo json_encode(array_values($result), JSON_PRETTY_PRINT);
How to decode a multidimensional json array in lararvel with 'foreach' for specific values?
Ok i did it the laravel way since your going to port it to laravel anyway. Here's the complete code to get you started.
Add this to web.php route file
Route::get('meeting', 'MeetingController@index');
MeetingController.php
<?php
namespace App\Http\Controllers;
class MeetingController extends Controller
{
public function index()
{
$fileContent = file_get_contents("/Applications/MAMP/htdocs/meeting.json");
$data = json_decode($fileContent);
return view('meeting')->with(compact('data'));
}
}
meeting.blade.php
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<ul>
<li>{{ $data->msg }}</li>
<li>{{ $data->meeting->id }}</li>
<li>{{ $data->meeting->created_at }}</li>
<li>{{ $data->meeting->updated_at }}</li>
<li>{{ $data->meeting->time }}</li>
<li>{{ $data->meeting->title }}</li>
<li>{{ $data->meeting->description }}</li>
<br>
@foreach($data->meeting->users as $user)
<ul>
<li>{{ $user->id }}</li>
<li>{{ $user->email }}</li>
<li>{{ $user->created_at }}</li>
<li>{{ $user->updated_at }}</li>
</ul>
<br>
@endforeach
</ul>
</body>
</html>
PHP Json_decode Multidimensional Array
If you can access the images key, then:
<?php
$json = <<<JSON
{
"title":"A Title Here",
"images":[
{
"coverType":"fanart",
"url":"some_random_file_here.jpg"
},
{
"coverType":"banner",
"url":"another_random_file_here.jpg"
},
{
"coverType":"poster",
"url":"yet_another_random_file_here.jpg"
}
]
}
JSON;
$json = json_decode($json);
print_r($json);
foreach ($json->images as $img)
{
if ( $img->coverType == "banner" )
{
echo 'Image Cover Type: ' .$img->coverType .'<br/>';
echo 'URL: ' .$img->url .'<br/>';
}
}
?>
Gives:
Image Cover Type: banner
URL: another_random_file_here.jpg
UPDATE:
The JSON file you link to seems invalid, missing braces after each series. Here's the corrected JSON, and code:
<?php
$json = <<<JSON
[
{
"series": {
"title": "Brooklyn Nine-Nine",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/269586-15.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/269586-g3.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/269586-13.jpg"
}
],
"year": 2013
}
},
{
"series": {
"title": "The Middle",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/95021-16.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/95021-g14.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/95021-8.jpg"
}
],
"year": 2009
}
},
{
"series": {
"title": "New Girl",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/248682-43.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/248682-g20.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/248682-14.jpg"
}
],
"year": 2011
}
}
]
JSON;
$json = json_decode($json);
// echo '<pre>' .print_r($json, 1) .'</pre>';
foreach ($json as $item)
{
echo 'Title: ' .$item->series->title .'<br/>';
foreach ($item->series->images as $img)
{
if ( $img->coverType == "banner" )
{
echo 'Image Cover Type: ' .$img->coverType .'<br/>';
echo 'URL: ' .$img->url .'<br/>';
}
}
}
?>
Gives:
Title: Brooklyn Nine-Nine
Image Cover Type: banner
URL: http://thetvdb.com/banners/graphical/269586-g3.jpg
Title: The Middle
Image Cover Type: banner
URL: http://thetvdb.com/banners/graphical/95021-g14.jpg
Title: New Girl
Image Cover Type: banner
URL: http://thetvdb.com/banners/graphical/248682-g20.jpg
How to create JSON Multidimensional Array in PHP code?
fist of all creat a multidimensioal array to respresent that then encode it to json format.like this:
$array = array(
'product'=>array(
'mainid'=>1,
"productinfo"=>array(
0=>array('subid'=>1222 ,'rate'=>4),
1=>array("subid"=>"1222",
"rate"=>"74")
//and so on
)
),
);
then you encode it into josn using:
json_encode($array);
Accessing JSON array after json_decode/multidimensional array
Assuming that you've chosen to decode the JSON as a multi-dimensional array, rather than as objects:
foreach ($results as $tweet) {
$user = $tweet["from-user"];
$text = $tweet["text"];
$entities = $tweet["enities"];
$urls = $entities["urls"];
foreach ($urls as $url) {
echo $url["expanded_url"];
}
}
et cetera
Related Topics
Regular Expressions: How to Express \W Without Underscore
Is There Any Legitimate Use for Bare Strings in PHP
Is There a Function Similar to Settimeout() (Javascript) for PHP
Change Pay Button on Checkout Based on Woocommerce Chosen Payment Method
Mysqli Join Tables from 2 Different Databases
How to Add a PHP Simplexmlelement to Another Simplexmlelement
How to Convert Week Number and Year into Unix Timestamp
PHP Date - Get Name of the Months in Local Language
How to Make My PHP Script Run at a Certain Time Everyday
How to Determine the Extension(S) Associated with a Mime Type in PHP
Using Mod_Rewrite with Xampp and Windows 7 - 64 Bit
Redirect to 'Www' Before Ssl Requirement
Get Order Increment Id in Magento