16

I think this has been up before, but could'nt find any answer to it. If it's already answered please point me in the right direction with a link.

I have an array that I wan't to remove the first levels of identifier. I think there is a function for this?

Example of how it is:

[0] => Array
        (
            [8] => Röd
        )

[1] => Array
        (
            [8] => Blå
        )

[2] => Array
        (
            [6] => Bobo
        )

[3] => Array
        (
            [8] => Grön
        )

[4] => Array
        (
            [7] => Sten
        )

[5] => Array
        (
            [8] => Vit
        )

[6] => Array
        (
            [7] => Guld
        )

[7] => Array
        (
            [6] => Lyxig
        )

What I wan't

[8] => Röd
[8] => Blå
[6] => Bobo
[8] => Grön
[7] => Sten
[8] => Vit
[7] => Guld
[6] => Lyxig
Fredrik
  • 627
  • 6
  • 14
  • 28

7 Answers7

27

Try to merge array with splat operator:

   print_r(array_merge(...$array));
Aivaras
  • 379
  • 3
  • 3
  • 1
    Please add a description on how the proposed answer will solve the problem. – Mathews Sunny Dec 11 '17 at 13:40
  • 1
    `...` basically says - send all (firstlevel) array items of `$array` as arguments to `array_merge` - which merges all sub arrays into 1 array, effectively resulting in removing 1st level - how `array_merge` works - reffer to docs: https://www.php.net/manual/en/function.array-merge.php , splat operator reference: https://wiki.php.net/rfc/argument_unpacking – jave.web Apr 29 '19 at 15:53
  • This works especially great if every element of the array has multiple sub elements. Only downside is that it doesn't preserve keys. But this worked fine for me! Thanks. – Daan van den Bergh Mar 18 '20 at 16:48
  • Worth to notice this may crash on PHP 8 due to named parameters. Workaround: `call_user_func_array( 'array_merge', array_values( $array ) )` – ZalemCitizen Nov 29 '22 at 10:07
  • This worked like a charm. – Adrian Vignolo Aug 25 '23 at 06:06
17

The problem here is preserving the keys for the identifier you want. You have some names strings that have the same key (like Blå and Röd). You either need to store these in an array or be willing to lose the key.

Example with php5.3:

$processed = array_map(function($a) {  return array_pop($a); }, $arr);

This will give you:

[0] => Röd
[1] => Blå
[2] => Bobo
[3] => Grön
[4] => Sten
[5] => Vit
[6] => Guld
[7] => Lyxig

It has become clear the keys on the inner array need to be preserved because they are some kind of id. With that said you must change the end structure you're going for because you can have 2 of the same key in a single array. The simplest structure then becomes:

[8] => Array
        (
            [0] => Röd,
            [1] => Blå,
            [2] => Vit,
            [3] => Grön
        )

[6] => Array
        (
            [0] => Bobo,
            [1] => Lyxig
        )

[7] => Array
        (
            [0] => Sten,
            [1] => Guld
        )

To get this structure a simple loop will work:

$processed = array();
foreach($arr as $subarr) {
   foreach($subarr as $id => $value) {
      if(!isset($processed[$id])) {
         $processed[$id] = array();
      }

      $processed[$id][] = $value;
   }
}
Felix
  • 126
  • 11
prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • This looks like a really neat solution. But can't get it to work. Inputing the array as you state in the $arr. Get the following message: array_map() expects parameter 1 to be a valid callback, first array member is not a valid class name or object. I have the following code ` array ( "8" => "Vit" ), "6" => array ( "7" => "Guld" ) ); $processed = array_map($arrData, function($a) { return array_pop($a); }); print_r($processed); ?>` – Fredrik Feb 29 '12 at 23:06
  • sorry flip the arguments around :-) Updated my answer. – prodigitalson Feb 29 '12 at 23:07
  • Thanks for answer. That almost work but it outputs the "first level identifier" and not the second. The output now looks like this: – Fredrik Mar 01 '12 at 15:17
  • Technically its not outputting the "first level identifier"... It is just re-indexing all the keys so they will always be form 0 - n where n is the total number of elements - 1, regardless of what any of the keys were to begin with. If you need to preserve the keys then you need to come up with a different structure to work with because as you have it now you have multiple elements with the same key like `Röd` and `Blå` (both use the "identifier"/key of `8`) – prodigitalson Mar 01 '12 at 15:24
  • Thanks for answer. But didn't catch that it outputed from 0 and up as the identifier. Is there a way to catch the "second identifier" so it will be outputed as this with your solution? `[8] => Röd [8] => Blå [6] => Bobo [8] => Grön [7] => Sten [8] => Vit [7] => Guld [6] => Lyxig` – Fredrik Mar 01 '12 at 15:29
  • Okay, thank you for your answer. Will go for another of the proposed solutions. – Fredrik Mar 01 '12 at 15:30
  • No matter what you do the array keys (numbers) **MUST BE UNIQUE**. This is the nature of an array. Like i said you need to rethink the end structure here. If i were you i would go with something like: `Array(8 => Array( 0 => 'Röd', 1 => 'Blå'))` – prodigitalson Mar 01 '12 at 15:34
6

PHP array_column

$new_array = array_column($old_array,0);

This will retrieve the value at index 0 for each array within $old_array. Can also be using with associative arrays.

Steven Johnston
  • 1,729
  • 13
  • 16
3

use :

  public function remove_level($array) {
      $result = array();
      foreach ($array as $key => $value) {
        if (is_array($value)) {
          $result = array_merge($result, $value);
        }
      }
      return $result;
}

which will return second level array values in the same order of the original array. or you can use array_walk

   $results = array();
   array_walk($array, function($v, $k) use($key, &$val){
              array_merge($results, $v);
    });
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
1

Below code will also achieve the same result.

$resultArray = array_map('current',$inputArray);

OR

$resultArray = array_map('array_pop',$inputArray);

Note: I have ignored OP's expected result keys. Because it is not possible to have the same keys in the array. The last key will replace the previous one if the same.

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
0
foreach($array as $key=>$val) { 
  $newarr[$val] = $array[$key][$val]; 
} 

untested!

Quasdunk
  • 14,944
  • 3
  • 36
  • 45
Luc
  • 985
  • 7
  • 10
0

Check this out this is what expected result

<?php
$arrData = array(
"5" => array
        (
            "8" => "Vit"
        ),

"6" => array
        (
            "7" => "Guld"
        )
);
foreach($arrData as $key=>$value):
    foreach($value as $k=>$v):
     $data[$k] = implode(',',$arrData[$key]);
    endforeach;
endforeach;


print_r($data);
?>
Sam Arul Raj T
  • 1,752
  • 17
  • 23