17

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
4
  • I think these are the droids you're looking for: stackoverflow.com/questions/1181691/… Commented Feb 28, 2012 at 12:41
  • You end up with duplicate keys... Can the keys be "renumbered" (1, 2, 3, ...?) Commented Feb 28, 2012 at 12:45
  • check my answer that will Remove first levels of identifier in array and will give you the exact result Commented Feb 28, 2012 at 12:54
  • This question requires an impossible result. Commented Mar 19 at 8:39

7 Answers 7

30

Try to merge array with splat operator:

   print_r(array_merge(...$array));
Sign up to request clarification or add additional context in comments.

4 Comments

Please add a description on how the proposed answer will solve the problem.
... 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: php.net/manual/en/function.array-merge.php , splat operator reference: wiki.php.net/rfc/argument_unpacking
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.
Worth to notice this may crash on PHP 8 due to named parameters. Workaround: call_user_func_array( 'array_merge', array_values( $array ) )
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;
   }
}

7 Comments

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 <?php $arrData = array( "5" => array ( "8" => "Vit" ), "6" => array ( "7" => "Guld" ) ); $processed = array_map($arrData, function($a) { return array_pop($a); }); print_r($processed); ?>
sorry flip the arguments around :-) Updated my answer.
Thanks for answer. That almost work but it outputs the "first level identifier" and not the second. The output now looks like this:
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)
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
|
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.

Comments

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);
    });

Comments

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.

Comments

0
foreach($array as $key=>$val) { 
  $newarr[$val] = $array[$key][$val]; 
} 

untested!

1 Comment

One should add that dublicated keys are of course not possible and will get overwritten.
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);
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.