php 删除数组中的第一级标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9481980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:55:12  来源:igfitidea点击:

Remove first levels of identifier in array

phparraysidentifierlevels

提问by Fredrik

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

回答by prodigitalson

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.

这里的问题是保留您想要的标识符的键。您有一些具有相同键的名称字符串(如 Bl? 和 R?d)。您要么需要将这些存储在数组中,要么愿意丢失密钥。

Example with php5.3:

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:

很明显,需要保留内部数组上的键,因为它们是某种 id。话虽如此,您必须更改您要使用的最终结构,因为您可以在单个数组中拥有 2 个相同的键。最简单的结构就变成了:

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

回答by Aivaras

Try to merge array with splat operator:

尝试将数组与 splat 运算符合并:

   print_r(array_merge(...$array));

回答by Steven Johnston

PHP array_column

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.

这将为 $old_array 中的每个数组检索索引 0 处的值。也可以与关联数组一起使用。

回答by Mouna Cheikhna

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

它将以与原始数组相同的顺序返回第二级数组值。或者你可以使用 array_walk

   $results = array();
   array_walk($array, function($v, $k) use($key, &$val){
              array_merge($results, $v);
    });

回答by Ravi Hirani

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.

注意:我忽略了 OP 的预期结果键。因为数组中不可能有相同的键。如果相同,最后一个键将替换前一个。

回答by Luc

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

untested!

未经测试!

回答by Sam T

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