-6

Having this kind of array:

Array (
   [0] => Array (
        [0] => Array (
              [title] => "Test string"
              [lat] => "40.4211"
              [long] => "-3.70118"
              )
         )
   [1] => Array (
            [0] => Array (
                  [title] => "Test string 2"
                  [lat] => "10.0"
                  [long] => "-23.0"
                  )
            [1] => Array (
                  [title] => "Test string 3"
                  [lat] => "10.0"
                  [long] => "-23.0"
                  )
          )
   [2] => Array (
        [0] => Array (
              [title] => "Test string 6"
              [lat] => "11.1"
              [long] => "7.7"
              )
         )
)

How can I get rid of that array of arrays for all the inner arrays that have length = 1?

My desired output would be:

Array (
   [0] => Array (
          [title] => "Test string"
          [lat] => "40.4211"
          [long] => "-3.70118"
   )
   [1] => Array (
            [0] => Array (
                  [title] => "Test string 2"
                  [lat] => "10.0"
                  [long] => "-23.0"
                  )
            [1] => Array (
                  [title] => "Test string 3"
                  [lat] => "10.0"
                  [long] => "-23.0"
                  )
    )
    [2] => Array (
          [title] => "Test string 6"
          [lat] => "11.1"
          [long] => "7.7"
     )
)

Thanks in advance.

PS: I'm using PHP 5.3

Avión
  • 7,963
  • 11
  • 64
  • 105
  • 2
    Why did you accept an answer that didn't get you where you wanted? https://stackoverflow.com/questions/47629049/group-array-by-inner-value-in-php and why are you not including that in this question. You have one array, and you asked for a way to change it, then you ask how to change the changed array. Isn't it better to do it correct to begin with instead of changing an already changed array. – Andreas Dec 04 '17 at 09:16

3 Answers3

4

You can do it like below:-

foreach($array as $key=>$value){
  if(is_array($value) && count($value) ==1){
    $array[$key] = $value[0];
  }
}

Output:- https://eval.in/912263

Or you can use Passing by Reference mechanism also:-

foreach($array as &$value){
  if(is_array($value) && count($value) ==1){
    $value = $value[0];
  }
}

Output:- https://eval.in/912264

Reference:- Passing by Reference

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
2

Transform each value, if it has length 1 return its first child, else return the entire thing unchanged:

$arr = array_map(function ($a) { return count($a) == 1 ? $a[0] : $a; }, $arr);
deceze
  • 510,633
  • 85
  • 743
  • 889
1

Assuming 2D arrays remain as is

foreach ($array as $key => $value) {      // Loop to result array
    if (count($value) <= 1) {           // Checks if array count is less than or equal to 1
        $array[$key] = reset($value);  // Reset to reduce from 2d array to 1d
    }
}

print_r($array);
Goma
  • 2,018
  • 1
  • 10
  • 19