PHP - looping through an array of JSON Variables - php

I have the following array that is in PHP (this is a print_r() output).
I'm currently struggling to loop through this data - need to be able to process each part and access the values in each array item. How can I do this.
I've tried the following unsuccessfully...
foreach (array as $key => $value) {
echo $key;
}

Try this. Since you have an array of objects, you should be able to access each object property using ->
foreach($array as $value) {
echo $value -> userid;
}
It should echo out all the user id in that array of objects

You have an array of objects, so try something like this:
<?php
foreach ($array as $value) {
echo $value->userid;
echo $value->action;
echo $value->photo_name;
}
You don't need the $key since you're not using it in the loop. Each iteration will put the object in the $value variable, on which you can access it's properties.

Related

get object data stdCLass in PHP

i want get data from stdclass object. i use foreach method to get key and value.
with var_dump i can get all infromation about the post, buy i want extract all 'display'.
foreach($data as $key=>$value){
var_dump($value);
}
var_dump result :
i just want extract all display_ur property. can anyone exlain me ?
do like below:-
foreach($data as $key=>$value){
foreach($value as $val){
echo $val->node->display_url;
echo PHP_EOL;
}
}
Your original array is made out of stdClassObjects. Each of these class objects has a public property called node that is also a stdClassObject.
That means that if you want to retrieve the display_url for each of these objects, you need:
foreach ($array as $object) {
$node = $object->node;
var_dump($node->display_url); // this should return what you are looking for
}
$value->display_url. I think this will work for you.
With type cast
$array=(object) $stdClassObject;
foreach($array as $key=>$value){
var_dump($value);
}

PHP Get keys with values from object

I am using simplexml_load_string() in PHP to parse my XML File.
Now, for better reading, I json_encode()d my object:
http://pastebin.com/Hk8YCssR (Example Representation Plan of a german School)
I want one object with all actions in it and one with all keys of the "head".
I tried to loop through these keys, but I wasn't abled to get the value of a keys key, e.g.:
"action":[
{
"class":"5/2",
"period":"5",
...
In this case, to get the value "5/2".
I don't have the exact code, but I am writing in sketch what I've done:
$value = current((array)$xml);
echo $value;
foreach ($xml as $key) {
// echo $key[];
}
P.S.: Thank you for reading this
Try this:
$sv_infch=get_object_vars($xml[0]->info);
foreach($sv_infch as $key => $value) {
print "$key => $value\n";
echo "{$key} => {$value} ";
print_r($sv_infch);
}

Loop through nested array PHP

I have the following structure (i use json_decode to loop through it on PHP):
{"721":{"forms":0},"722":{"forms":"[82]","ope":"48723","ini":"01/03/2015 00:00","fin":"01/03/2015 00:20"},"723":{"forms":0},"724":{"forms":0},"725":{"forms":0},"726":{"forms":0},"727":{"forms":0},"729":{"forms":0},"730":{"forms":0},"731":{"forms":0}}
How can I access the property "forms"?
Use a foreach loop to go through every key/value pair in your array, then just access the forms property of the value array.
$a = json_decode($stuff, true); // where $stuff is that JSON string
foreach ($a as $key => $value) {
print $value['forms']."<br />"
}

json_encode get values foreach object

Hello I can't figure out how to loop through this json encoded array and for each object, get all its values. I need each value as a variable for itself.
echo json_encode($formulars);
This is what i get when i echo it out
[{"project_name":"polle","type":"support","title":"vi","reason":"prover","solution":"igen","comments":"okay ","date_stamp":"2013-08-20 14:06:37","used_time":132},{"project_name":"dolla","type":"support","title":"lolol","reason":"skl","solution":"dskal","comments":"kflafda ","date_stamp":"2013-08-20 14:11:36","used_time":210},{"project_name":"polle","type":"fejl","title":"lol","reason":"aksdl","solution":"fdjks","comments":"djsks ","date_stamp":"2013-08-20 14:13:27","used_time":1230}]
I have tried this piece of code and I managed to get out the project_name from the first object and that's it:
foreach ($formulars as $current => $project_name) {
$project_name['project_name'];
}
So is there any way i can get all the variables for each object in my array instead of just the project_name?
Like this:
foreach ($formulars as $current){
$projectName = $current['project_name'];
$type = $current['type'];
$reason = $current['reason'];
}
Thanks in advance
Seems like you have an objects inside an array. So you will need to loop through the array and get each object. Just JSON_DECODE your encoded string like below.
Perhaps:
$data = json_decode($formulars,true);
/* Since it's only one object inside the array, you could just select element zero, but I wil loop*/
//You should now be able to do this
foreach ($data as $current){
$projectName = $current['project_name'];
$type = $current['type'];
$reason = $current['reason'];
}
The reason I loop is because there is a object inside an array(Javascript way I think).
Use json_decode to convert the json object to an array; then use foreach to loop through the array. That should work.
<?php
$arr_json = json_decode($formulars);
foreach($arr_json as $key => $value)
//Code to perform required actions
?>
This should give you some ideas.
Use json_decode (with TRUE for getting an associative array)to convert your JSON object to an associative array. After that, you can use a foreach loop to traverse through your multidimensional array and print the required values.
Code:
$json = json_decode($string, true);
foreach ($json as $key => $value) {
foreach($value as $key2 => $value2) {
echo $value2."\n";
}
}
Working Demo!

Getting the name of an array dynamically

I have an array which looks like this:
$example = [
['rendered'][0]['rendereditem1']
['rendered'][4]['rendereditem2 and more']
['rendered'][2]['rendereditem3']
]
Now I want to iterate with foreach to get the contents of 0,4,2!
Normally I would write:
foreach($example as $value){
print $value['rendered'][int which is the same everywhere];
}
But that is obviously not working because the array name is always different...how could I iterate in this case?
Simply add a second loop to iterate over the members :
foreach($example as $value) {
foreach($value['rendered'] as $key=>$item) {
// Do what you want here, $key is 0,4,2 in your example
}
}

Categories