Question

I'm trying to convert a multidimensional object into an array to pass into an API call that requires an array. Problem is, even after converting the object to array using the lazy method of:

$data = json_decode(json_encode($object),true)

The returned value is acting funny. I'm getting empty array values where values should be none, so I tried running it through a foreach loop:

foreach ( $data as $key => $data_each ) {

    if ( is_array($data_each[$key]) ) { $data[$key] = NULL; }

}

But it's not catching the array value in the foreach loop. After running the foreach, I check the value that I happen to KNOW is coming back as an empty array and as long as I check using in_array outside the foreach loop is is catching it as an array. But not within the loop for some reason.

What am I missing?

Was it helpful?

Solution

You have this:

foreach ( $data as $key => $data_each ) {
    if ( is_array($data_each[$key]) ) { $data[$key] = NULL; }
}

Here, $data_each is a value of $data[$key]. Try is_array($data_each) if you want to check whether the value is an array or not. In the loop $data_each is same as $data[$key].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top