0

I have a strange array format after converting from a SimpleXMLElement. I have an array like this:

Array
(
    [test] => Array
        (
            [0] => Array
                (
                    [a] => Array
                        (
                            [0] => 1
                        )

                    [b] => Array
                        (
                            [0] => 2
                        )

                    [c] => Array
                        (
                            [0] => 3
                        )

And i want to transform it into this:

Array
(
    [test] => Array
        (
            [0] => Array
                (
                    [a] => 1

                    [b] => 2

                    [c] => 3

Any ideas?

4
  • What is the problem you are having? Commented Sep 10, 2012 at 18:35
  • 1
    What have you tried? Commented Sep 10, 2012 at 18:35
  • i would suggest fixing the SimpleXML format Commented Sep 10, 2012 at 18:36
  • 1
    Create a new array, loop through your existing array, and push elements onto the new array as appropriate. What's stopping you? Commented Sep 10, 2012 at 18:37

1 Answer 1

1

I use this, to optimize single element arrays from SimpleXmlElement:

function optimize( $config )
{
  foreach ( $config as $key => $value ) 
    if( is_array( $value ) && count( $value ) == 1 && isset( $value[0] ))
       $config[$key] = $value[0];              

  return $config
}

As single array elements can be nested some levels deep, you can use this function as a recursive function.

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

1 Comment

I have a simple recursive function like this: function parse($iterator) { foreach ($iterator as $key => $item) { $data[$key][] = ($iterator->hasChildren()) ? parse($item) : strval($item); }

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.