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);
}
Related
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);
}
How can I loop through all the properties of object?. Right now I have to write a new code line to print each property of object
echo $obj->name;
echo $obj->age;
Can I loop through all the properties of an object using foreach loop or any loop?
Something like this
foreach ($obj as $property => $value)
If this is just for debugging output, you can use the following to see all the types and values as well.
var_dump($obj);
If you want more control over the output you can use this:
foreach ($obj as $key => $value) {
echo "$key => $value\n";
}
For testing purposes I use the following:
//return assoc array when called from outside the class it will only contain public properties and values
var_dump(get_object_vars($obj));
Before you run the $obj through a foreach loop you have to convert it to an array (see: cast to array) if you're looking for properties regardless of visibility.
Example with HTML output (PHP 8.1):
foreach ((array)$obj as $key => $val) {
printf(
"%s: %s<br>\n",
htmlspecialchars("$key"),
htmlspecialchars("$val"),
);
}
Sometimes, you need to list the variables of an object and not for debugging purposes. The right way to do it is using get_object_vars($obj). It returns an array that has all the visible class variables and their value. You can then loop through them in a foreach-loop. If used within the object itself, simply do get_object_vars($this).
Here is another way to express the object property.
foreach ($obj as $key=>$value) {
echo "$key => $obj[$key]\n";
}
Using get_object_vars is better than applying a foreach directly to the object since your code will be also compliant with PHPStan which requires that foreach is applied only to iterables (see https://github.com/phpstan/phpstan/issues/1060).
Thus, the best choice is go like this:
foreach (get_object_vars($obj) as $key => $value) {
echo "$key => $value\n";
}
David's answer is solid as long as either: a) you only need access to public attributes, or b) you are working with the stdClass object. If you have defined private or protected attributes in a class and want to display all attributes, simply add an instance method that iterates the properties:
class MyClass {
public $public_attr1;
private $private_attr2;
protected $protected_attr3;
function iterateAttributes() {
foreach ($this as $attr=>$value) {
echo "$attr: $value <br/>";
}
}
}
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!
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.
How can I loop through all the properties of object?. Right now I have to write a new code line to print each property of object
echo $obj->name;
echo $obj->age;
Can I loop through all the properties of an object using foreach loop or any loop?
Something like this
foreach ($obj as $property => $value)
If this is just for debugging output, you can use the following to see all the types and values as well.
var_dump($obj);
If you want more control over the output you can use this:
foreach ($obj as $key => $value) {
echo "$key => $value\n";
}
For testing purposes I use the following:
//return assoc array when called from outside the class it will only contain public properties and values
var_dump(get_object_vars($obj));
Before you run the $obj through a foreach loop you have to convert it to an array (see: cast to array) if you're looking for properties regardless of visibility.
Example with HTML output (PHP 8.1):
foreach ((array)$obj as $key => $val) {
printf(
"%s: %s<br>\n",
htmlspecialchars("$key"),
htmlspecialchars("$val"),
);
}
Sometimes, you need to list the variables of an object and not for debugging purposes. The right way to do it is using get_object_vars($obj). It returns an array that has all the visible class variables and their value. You can then loop through them in a foreach-loop. If used within the object itself, simply do get_object_vars($this).
Here is another way to express the object property.
foreach ($obj as $key=>$value) {
echo "$key => $obj[$key]\n";
}
Using get_object_vars is better than applying a foreach directly to the object since your code will be also compliant with PHPStan which requires that foreach is applied only to iterables (see https://github.com/phpstan/phpstan/issues/1060).
Thus, the best choice is go like this:
foreach (get_object_vars($obj) as $key => $value) {
echo "$key => $value\n";
}
David's answer is solid as long as either: a) you only need access to public attributes, or b) you are working with the stdClass object. If you have defined private or protected attributes in a class and want to display all attributes, simply add an instance method that iterates the properties:
class MyClass {
public $public_attr1;
private $private_attr2;
protected $protected_attr3;
function iterateAttributes() {
foreach ($this as $attr=>$value) {
echo "$attr: $value <br/>";
}
}
}