0

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.

1
  • Could you please elaborate what v=>1 and v=>2 in your output array are? Commented Apr 4, 2014 at 12:26

2 Answers 2

4

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.

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

4 Comments

Amazing! How did you catch my mistake so quickly? Thanks but you made me feel like an idiot:)
@user3293156 consider accepting my answer thus marking this question as answered thus preventing this question from getting more attention thus keeping others from reading about you feeling like an idiot ;-)
Of course. I have been trying to accept the answer a few times already. Stack Overflow only allows me to accept in 5 minutes time from now.
@user3293156 Well, take those 5 minutes and check out the services i mentioned in my edit. They might help you in the future, either for asking or for answering questions. Or just to play with them :D
1

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?

2 Comments

That would only solve the problem of displaying the result, not the fact that the result is wrong :-/
@Andresch Serj you had answered in right way, no need to duplicate 100500 times same answer. I just add 'why maximum depth reached happens'. ;)

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.