How to display array property with php [duplicate] - php

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/>";
}
}
}

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

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.

Looping through all the properties of object php

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/>";
}
}
}

to print values of an array after extracting the array in php

Please help in code that i am unable to print values of associative array after extracting itself
class display{
protected $variables = array();
function set($name,$value) {
$this->variables[$name] = $value;
}
function render(){
extract($this->variables);
// ?? to print values of $variable array
}
foreach($this->variables as $key => $value) {
echo "{$key}: {$value}\n";
}
And how do you try to print the values? The array itself (it's $varables, not $variable, btw) should not be affected.
Update: For what I can tell by your reply to the other answer, you do not really need to extract array. extract jusst puts the variables into local namespace where they will be harder to enumerate. What you need is to use array as is.
foreach($this->variables as $k => $v) echo "$k: $v\n";
or whatever you want to do with them.
if you are using classes, u will need to have something like
var $variables = array(); or
public $variables = array();
and if you are using structured , you will need to do
global $variables;
inside the function .. but as u are using $this-> it indicates ur using a class. You will have to put in some more code in here to make the situation clear.

Categories