Question

I have this php associative input array.

array(
    (int) 0 => array(
        'Data' => array(
            'id' => '12',
            'type_id' => '1',
            'data_value' => '35.5000'
        ),
        'Type' => array(
            'id' => '1',
            'name' => 'Temperature'
        )
    ),
    (int) 1 => array(
        'Data' => array(
            'id' => '11',
            'type_id' => '1',
            'data_value' => '33.7000'
        ),
        'Type' => array(
            'id' => '1',
            'name' => 'Temperature'
        )
    )

I want to convert it to this output array;

array(
    (int) 0 => array(
        (int) 0 => array(
            'v' => (int) 1
        ),
        (int) 1 => array(
            'v' => '35.5000'
        )
    ),
    (int) 1 => array(
        (int) 0 => array(
            'v' => (int) 2
        ),
        (int) 1 => array(
            'v' => '33.7000'
        )
    )

The element data_value is extracted from the input array into the output array.

The code I have written looks like this;

                $data2 = array();
                foreach ($InputArray as $key=>$value)
                {
                    $data2[] = array(
                                array(                                        
                                    array('v' => $key),
                                    array('v' => $value['Data']['data_value']) 
                            )
                        );
                }  

Unfortunately, this code does not work. The output from this code looks like this;

array(
    (int) 0 => array(
        (int) 0 => array(
            (int) 0 => array(
                [maximum depth reached]
            ),
            (int) 1 => array(
                [maximum depth reached]
            )
        )
    ),
    (int) 1 => array(
        (int) 0 => array(
            (int) 0 => array(
                [maximum depth reached]
            ),
            (int) 1 => array(
                [maximum depth reached]
            )
        )
    )

What did I do wrong? Why do I get the error "maximum depth reached"? How can I retrieve the desired output array? I am actually doing this in cakephp.

Thank you very much for any help.

Was it helpful?

Solution

That is one wrapping array() to many:

        $data2 = array();
        foreach ($InputArray as $key=>$value)
        {
            $data2[] = array(                                      
                            array('v' => $key),
                            array('v' => $value['Data']['data_value']) 

                );
        } 

You can see this working here. It is good use to prepare your code in services like ideome or plnkr in order to make it easier for people to help you with debugging.

OTHER TIPS

As far as I know, the problem is related to a setting for PHP http://www.hardened-php.net/suhosin/configuration.html#suhosin.executor.max_depth . Could you check with your hosting provider?

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