1

I have an array like:

$array = array(
 'name' => 'Humphrey',
 'email' => 'humphrey@wilkins.com
);

This is retrieved through a function that gets from the database. If there is more than one result retrieved, it looks like:

$array = array(
 [0] => array(
  'name' => 'Humphrey1',
  'email' => 'humphrey1@wilkins.com'
 ),
 [1] => array(
  'name' => 'Humphrey2',
  'email' => 'humphrey2@wilkins.com'
 )
);

If the second is returned, I can do a simple foreach($array as $key => $person), but if there is only one result returned (the first example), I can't run a foreach on this as I need to access like: $person['name'] within the foreach loop.

Is there any way to make the one result believe its a multidimensional array?

Ryan Hipkiss
  • 648
  • 1
  • 7
  • 20
  • $new_array[] = $array; , use $new_array in foreach – Rijin Oct 24 '16 at 13:28
  • @Rijin I need to only do this if there is one result returned. – Ryan Hipkiss Oct 24 '16 at 13:30
  • 1
    Personally unless a function / method can only ever return 1 row, I would have it always return an array of items even if only one was returned. So my suggestion is to change the function that returns the data. – Jonnix Oct 24 '16 at 13:30
  • 1
    then check for no of results using count($array) == 1 – Rijin Oct 24 '16 at 13:33
  • @Rijin Nope. Look at the examples and you'll see why that won't work. – Jonnix Oct 24 '16 at 13:33
  • check if $person is array if so use the same function. So just write recursive function – Robert Oct 24 '16 at 13:35
  • check my answer, changing to multi array if only one result – Rijin Oct 24 '16 at 13:37
  • I had `array_shift()` running on returned results, rather than on my `get()` function I was calling. Since moving it to the `get()` function, which was overwritten in this particular controller, it fixes the issue. So anything using my default `get()` function returns like question, otherwise it will always return an array of items unless told otherwise. – Ryan Hipkiss Oct 24 '16 at 13:40

5 Answers5

1

Try this :

if(!is_array($array[0])) {
    $new_array[] = $array;
    $array = $new_array;
}
Rijin
  • 625
  • 1
  • 4
  • 13
0

I would highly recommended making your data's structure the same regardless of how many elements are returned. It will help log terms and this will have to be done anywhere that function is called which seems like a waste.

You can check if a key exists and do some logic based on that condition.

if(array_key_exists("name", $array){
    //There is one result
    $array['name']; //... 
} else {
    //More then one
    foreach($array as $k => $v){
        //Do logic
    }
}

You will have the keys in the first instance in the second yours keys would be the index.

nerdlyist
  • 2,842
  • 2
  • 20
  • 32
0

Based on this, try:

function isAssoc(array $arr)
{
    if (array() === $arr) return false;
    return array_keys($arr) !== range(0, count($arr) - 1);
}

if(isAssoc($array)){
    $array[] = $array;
}
Community
  • 1
  • 1
Vanojx1
  • 5,574
  • 2
  • 23
  • 37
0

First check if the array key 'name' exists in the given array. If it does, then it isn't a multi-dimensional array.

Here's how you can make it multi-dimensional:

if(array_key_exists("name",$array))
{
 $array = array($array);
}

Now you can loop through the array assuming it's a multidimensional array.

foreach($array as $key => $person)
{
  $name = $person['name'];
  echo $name;
}
Akshay Khetrapal
  • 2,586
  • 5
  • 22
  • 38
0

The reason of this is probably because you use either fetch() or fetchAll() on your db. Anyway there are solutions that uses some tricks like:

$arr = !is_array($arr[0]) ? $arr : $arr[0]; 

or

is_array($arr[0]) && ($arr = $arr[0]); 

but there is other option with array_walk_recursive()

$array = array(
 array(
  'name' => 'Humphrey1',
  'email' => 'humphrey1@wilkins.com'
 ),
  array(
  'name' => 'Humphrey2',
  'email' => 'humphrey2@wilkins.com'
 )
);

$array2 = array(
  'name' => 'Humphrey2',
  'email' => 'humphrey2@wilkins.com'
 );

$print = function ($item, $key) {
   echo $key . $item .'<br>';
};

array_walk_recursive($array, $print);
array_walk_recursive($array2, $print);
Robert
  • 19,800
  • 5
  • 55
  • 85