Forum Moderators: coopster
I'm listing these items in a form and when I get to an array I do foreach.
My problem is that sometimes foreach doesn't work when there is one item...
here's the one item array
[parts] => Array
(
[qty] => 1
[partno] => #DST50297618006
[price] => 48.86 )
Here's one that works
[parts] => Array
(
[qty] => Array
(
[0] => 1
[1] => 0
) [partno] => Array
(
[0] => #NEWPRO
[1] => #CDA FW420
)
[price] => Array
(
[0] => 349.00
[1] => 0.00
)
)
I was going to use is_array and then do two sets of code but I'm wondering if there is a better way?
if you want to test whether it is 1D array or 2D array you do not want to use is_array cos they both are arrays anyway unless you mean to do (is_array($array[0]) then yes it would do it
but you can perform this test:
let's assume your array is $array
if(!is_array($array[0])){
$newarray[0] = $array;
}
now you can deal with $newarray the way you are doing now cos it is 2D