Remove first levels of identifier in array [duplicate]
Asked Answered
A

7

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
Airship answered 28/2, 2012 at 12:34 Comment(3)
I think these are the droids you're looking for: #1182191Infelicity
You end up with duplicate keys... Can the keys be "renumbered" (1, 2, 3, ...?)Toothless
check my answer that will Remove first levels of identifier in array and will give you the exact resultNunez
L
29

Try to merge array with splat operator:

   print_r(array_merge(...$array));
Linsang answered 11/12, 2017 at 13:15 Comment(4)
Please add a description on how the proposed answer will solve the problem.Irfan
... 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_unpackingLungi
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.Rosebud
Worth to notice this may crash on PHP 8 due to named parameters. Workaround: call_user_func_array( 'array_merge', array_values( $array ) )Gurkha
W
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;
   }
}
Wicketkeeper answered 28/2, 2012 at 12:44 Comment(7)
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); ?>Airship
sorry flip the arguments around :-) Updated my answer.Wicketkeeper
Thanks for answer. That almost work but it outputs the "first level identifier" and not the second. The output now looks like this:Airship
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)Wicketkeeper
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] => LyxigAirship
Okay, thank you for your answer. Will go for another of the proposed solutions.Airship
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å'))Wicketkeeper
H
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.

Horny answered 27/9, 2016 at 18:13 Comment(0)
C
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);
    });
Chiropractic answered 28/2, 2012 at 13:11 Comment(0)
D
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.

Duyne answered 13/2, 2019 at 14:23 Comment(0)
M
0
foreach($array as $key=>$val) { 
  $newarr[$val] = $array[$key][$val]; 
} 

untested!

Maximilianus answered 28/2, 2012 at 12:40 Comment(1)
One should add that dublicated keys are of course not possible and will get overwritten.Prove
N
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);
?>
Nunez answered 28/2, 2012 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.