1
Array
(
    [0] => stdClass Object
        (
            [card] => stdClass Object
                (
                    [name] => Abc
                    [number] => 1234567123456798
                    [exp_month] => 1
                    [exp_year] => 2015
                    [cvc] => 123
                )

            [deliveryPeriod] => stdClass Object
                (
                    [id] => 1
                    [store_id] => 1
                    [start_date] => 2014-11-01
                    [end_date] => 2014-12-31
                    [delivery_schedules] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 1
                                    [delivery_period_id] => 1
                                    [start_time] => 09:00:00
                                    [end_time] => 18:00:00
                                    [slot_capacity] => 25
                                    [delivery_fee] => 2
                                    [used_capacity] => 1
                                )

                        )

                )

                )

        )

)

I have this stdclass object array and i want to convert this to a normal array.I was using foreach but i can loop through inner array.how can i do that in php.please help me i am new to php.

1
  • What is a "normal array"? What have you tried so far? Please add your code... Commented Dec 8, 2014 at 10:09

4 Answers 4

4

Use json_decode() and json_encode()

$array = json_decode(json_encode($object), true);

Where $object is your multi-dimensional nested object.

Explanation:

First JSON encode your object, then JSON decode it.

With second argument associative to TRUE.

This way, you will get an associative array without caring of nested depth.

Sign up to request clarification or add additional context in comments.

Comments

2

Create a recursive function

function objectToArray($obj, &$new_array){
    foreach($obj as $key => $value){
       if(is_object($value){
          $new_array[$key] = array();
          objectToArray($value, $new_array[$key]);
       } else {
          $new_array[$key] = $value;
       }

       return $new_array;
    }
}

Comments

1

You can just use typecasting by using (array) in front of the array in question... So for you:

foreach($array as $item) {
    $itemArray = (array)$item;
}

Comments

0

You could typecast your array like the example below:

<?php
$obj = new stdClass();
$obj->test = 'Hello';
var_dump($obj);

$arr = (array)$obj;
var_dump($arr);

Returns:

object(stdClass)#1 (1) {
  ["test"]=>
  string(5) "Hello"
}
array(1) {
  ["test"]=>
  string(5) "Hello"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.