PHP Get keys with values from object - php

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);
}

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);
}

Read through dynamic JSON in php

I'm needing to know how to read through JSON in PHP that I don't know the key's for. Here is an example of a json that I would need to be going through however none of the key->value pair are always going to be the same. Some of these key names can be spelled different or have numbers on the end of the names so I need to be able to have them dynamic.
Here is an exact example of the JSON I would be cycling through
[
{
"var5":"item-company-1",
"asd2":"item-company-1",
"tie1":"0",
"cxs1":"481.891px",
"xcve2":"130.563px"
},...
I understand if I know the exact name of the key I would do something like..
$someObject = json_decode($someJSON);
print_r($someObject); // Dump all data of the Object
echo $someObject[0]->name; // Access Object data
However I don't know the keys. I assume a for next loop would have to be created to cycle through each object, but I have no clue how to do that in PHP.
Im assuming your json is always of the below format
$json = '[
{
"var5":"item-company-1",
"asd2":"item-company-1",
"tie1":"0",
"cxs1":"481.891px",
"xcve2":"130.563px"
},
{
"var6":"item-company-2",
"asd7":"item-company-2",
"tie8":"0",
"cxs9":"481.891px",
"xcve10":"130.563px",
"extra" : "unknown"
}
]';
Then you can convert the whole JSON into an array and run a loop
$array = json_decode ($json ,true);
foreach ($array as $item) {
foreach($item as $key => $value) {
echo $value;
}
}
I marked the answer because he technically answered my question.
I also found this solution hopefully it might help someone in the future as a great alternative to the above. This will actually go over a multi-level array to get out all the values.
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($json, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
echo "$key:\n";
} else {
echo "$key => $val\n";
}
}

PHP array, substitute the key within a foreach loop

I have read a lot of posts and have not been able to find a solution to my issue.
I have a $_POST array named "Water", $_POST['Water'] and its contents are:
[Water] => Array ( [0] => 55.0 [1] => 22 )
Is it possible to use the name of the post inside a foreach loop so the $key could use the name "Water":
foreach($_POST['Water'] as $key => $val) {
$fields[] = "$field = '$val'";
//echo "Key: $key, Value: $val<br/>\n";
}
Many thanks for your time.
Not really. foreach() operates on the contents of an array. Whatever actually contains that array is outside of foreach's view. If you want to dynamically use the Water key elsewhere, you'll have to do that yourself:
$key = 'Water'
foreach($_POST[$key] as $val) {
$fields[] = "$key = '$val'";
}
If I read this right you basically want $field='water' inside the foreach. This can be done be rethinking the way we build the foreach.
You simply need to make the field value a variable and pass use that rather everywhere the value is needed.
$field = 'Water';
foreach($_POST[$field] as $key => $val) {
$fields[] = "$field = '$val'";
//echo "Key: $key, Value: $val<br/>\n";
}
The advantage of this approach is that if you change your mind and the $_POST key is later called, "liquid" one single line needs to be edited and that is all. Furthermore, if your foreach were part of a function $field could be a function parameter. In a roundabout way, you are actually getting pretty close to some code reuse principles.

Pull data from nested json loop php

Ok, I have been fighting this for two days now!! Here is my Json structure.
[
{
"uuid":"/random number==",
"meeting_number":7196503037,
"host_id":"random number",
"topic":"My's Personal Meeting Room",
"start_time":"2015-01-06T22:01:07Z",
"timezone":"America/Denver",
"duration":56,
"total_size":378080,
"recording_count":1,
"recording_files":[
{
"id":"long number",
"meeting_id":"/NS90bsSTLeGzo6cT0nwXw==",
"recording_start":"2015-01-06T22:01:09Z",
"recording_end":"2015-01-06T22:01:14Z",
"file_size":378080,
"play_url":"https://myurl"
}
]
},
I am trying to pull the Start Time from the top level array (which is named 'meetings') and the Play Url from the second level array (recording_files). I am using json_decode to decode the file into arrays.
I have tried many variations found on this site and nothing seems to get to the play url.
VARIATION 1
$aha = json_decode($response,true);
foreach ($aha['meetings'] as $key => $value){
foreach($key['recording_files'] as $subkey => $newvalue){
echo '<p>Time : '.$value['start_time'].'</p>
Recording<br>';
}}
This pulls an invalid argument in foreach.
VARIATION 2
foreach ($aha['meetings'] as $key => $value){
echo '<p>Time : '.$value['start_time'].'</p>
Recording<br>';
}
This pulls undefined index for play_url. As does the same code with the reference to ['recording_files'] placed before ['play_url']
VARIATION 3:
Then I tried giving up on the first level and just pulling data from the second level:
$aha = json_decode($response,true);
foreach ($aha['meetings']['recording_files'] as $key => $value){
echo '<p>Time : '.$value['recording_start'].'</p>
Recording<br>';
}
That gives me an undefined index on ['recording_files'] and a foreach error.
VARIATION 4.
$aha = json_decode($response,true);
foreach ($aha['meetings'] as $key => $value){
$link=$key['recording_files'];
echo '<p>Time : '.$value['start_time'].'</p>
Recording<br>';
}
This gets me the closest - no errors but no link. I presume that here the code is seeing the field but just not pulling the data from it...
Please help a newbie json person!!
If there is really an $aha['meetings'] (you don't show it), then use the value not the key in the second foreach:
foreach($aha['meetings'] as $value){
foreach($value['recording_files'] as $newvalue){
Final code that fixed it.
$aha = json_decode($response,true);
foreach ($aha['meetings'] as $key => $value){
echo '<p>Time : '.$value['start_time'].'</p>
Recording<br>';}
It was just a matter of finding the correct format for referencing the play_url piece.
Thank you so much to everyone!
Edit:
If your data looks like this :
$str = "{\"meetings\":[{
\"uuid\":\"/random number==\",
\"meeting_number\":7196503037,
\"host_id\":\"random number\",
\"topic\":\"My's Personal Meeting Room\",
\"start_time\":\"2015-01-06T22:01:07Z\",
\"timezone\":\"America/Denver\",
\"duration\":56,
\"total_size\":378080,
\"recording_count\":1,
\"recording_files\":[
{
\"id\":\"long number\",
\"meeting_id\":\"/NS90bsSTLeGzo6cT0nwXw==\",
\"recording_start\":\"2015-01-06T22:01:09Z\",
\"recording_end\":\"2015-01-06T22:01:14Z\",
\"file_size\":378080,
\"play_url\":\"https://myurl\"
}
]
}]}";
$json = json_decode($str);
$res = $json->{'meetings'};
foreach($res as $keys){
echo $keys->{'start_time'}."<br>";
foreach ($keys->{'recording_files'} as $data){
echo $data->{'play_url'}."<br>";
echo $data->{'recording_start'}."<br>";
}
}

PHP - looping through an array of JSON Variables

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.

Categories