vote up 1 vote down star

I hope you can help me out with this problem I have with xml2array. when there is one item in the XML it returns just the array (example 1), when there are multiple items it returns an extra dimension 0,1,2,3 (example 2). I don't want to have two sets of foreach code for 1 and many arrays.

How do I can convert example 1 into example 2? [line][0]? I tried simply adding it to the existing array but then how do I remove the old one?

EXAMPLE 1

[line] => Array
            (
                [lineno] =>  1
                [partno] => 1234
                [desc] => Furniture
                [qty] =>     1.00

              )

EXAMPLE 2

  [line] =>[0] => Array
                    (
                        [lineno] =>  2
                        [partno] => 1234
                        [desc] => Furniture
                        [qty] =>     1.00
                    )

           [1] => Array
                    (
                        [lineno] =>  1
                        [partno] => 1234
                        [desc] => Furniture
                        [qty] =>     1.00
                     )
flag

25% accept rate

2 Answers

vote up 0 vote down

like

if(count($line) == 1) $line = array($line);
link|flag
vote up 0 vote down

you'll need a conditional to check whether there is a single item or not.

something like this:

if (isset($array['line']['lineno'])){
    $array['line'] = array($array['line']);
    }
link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.