QuizCure
Find Available Solution Here!

Stdclass object Php foreach loop

Deepak Jul 27, 2024

In the current article we are going to learn about how to implement stdclass object php foreach iterations with examples.

Ways of accessing stdclass object parameter depending on object complexity.

Fetch & Print Multidimensional Stdclass Object Values using foreach loop

Its like a normal class object iteration. Please refer below example for more understanding and learn how to iterate Stdclass object foreach in php with following example block of code.

Stdclass object Php foreach loop

You may try the following code and execute at your end for multilevel arrays.

Let's create first multidimensional stdClass object for foreach loop demonstration

$student1 = new stdClass(); 
$student1->name = "John";
$student1->id = 123;


$student2 = new stdClass(); 
$student2->name = "Peter";
$student2->id = 321;


$students = array(

   "math" => $student1,
   "science" => $student2
);

$objects = (object) $students;


$objects will be generated as

stdClass Object
(
    [math] => stdClass Object
        (
            [name] => John
            [id] => 123
        )

    [science] => stdClass Object
        (
            [name] => Peter
            [id] => 321
        )

)

Loop through php stdClass object $objects example as below.

foreach($objects as $obj => $student){
    echo "Department : " . $obj. " => Name: ". $student->name . " | id:" .  $student->id ; 
    echo "<br>";
   }

Output:

Department : math => Name: John | id:123
Department : science => Name: Peter | id:321

How to get stdclass object value in php for single dimensional object

// Single Dimensional Array
$employees =  array(
        'name' => 'Peter',
        'age' => '21',
        'address' => '123, wall street'
    
);


//Convert array to object
$obj = (object) $employees;

Now we have stdClass object as $obj as below

stdClass Object
(
    [name] => Peter
    [age] => 21
    [address] => 123, wall street
)


Next let's see How to print stdClass object in php

echo "Name: ". $obj->name . " | Age:". $obj->age ." | Address:".$obj->address ;  

Output: Name: Peter | Age:21 | Address:123, wall street

Therefore as per above code sample we can fetch value using $obj->paramName

How to extract stdClass object values from Mixed type Array?

Lets understand with following example code.

Creating multidimensional stdClass object for demonstration purpose.

$student1 = new stdClass();
$student1->name = "John";
$student1->id = 123;


$student2 = new stdClass();
$student2->name = "Peter";
$student2->id = 321;


$students = array(
    "math" => $student1,
    "science" => $student2
);

$objects = (object) $students;

$mixedArray = array(
    "user" => array(
        array(
            'name' => 'Henry',
            'age' => '24'
        )
    ),
    "students" => $objects
);

$mixedObjects = (object) $mixedArray;

Above Code will generate stdClass object as:

stdClass Object
(
    [user] => Array
        (
            [0] => Array
                (
                    [name] => Henry
                    [age] => 24
                )

        )

    [students] => stdClass Object
        (
            [math] => stdClass Object
                (
                    [name] => John
                    [id] => 123
                )

            [science] => stdClass Object
                (
                    [name] => Peter
                    [id] => 321
                )

        )

)

Loop through stdClass Object $mixedObjects->students and render values as below

foreach ($mixedObjects->students as $obj => $student) {
    echo "Department : " . $obj . " => Name: " . $student->name . " | id:" . $student->id;
    echo "<br>";
}

Output:

Department : math => Name: John | id:123
Department : science => Name: Peter | id:321


Was this post helpful?

Send Feedback

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up