热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

删除数组中的第一级标识符。-Removefirstlevelsofidentifierinarray

Ithinkthishasbeenupbefore,butcouldntfindanyanswertoit.Ifitsalreadyansweredplease

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

6 个解决方案

#1


12  

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.

这里的问题是保存所需标识符的键。有一些名称字符串具有相同的键(比如Bla和Rod)。您要么需要将它们存储在数组中,要么愿意丢失密钥。

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。最简单的结构就是:

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

#2


3  

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处的值。也可以与关联数组一起使用。

#3


1  

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

#4


0  

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

untested!

未经考验的!

#5


0  

Check this out this is what expected result

看看这个,这是预期的结果

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

#6


0  

Try to merge array with splat operator:

尝试合并数组与splat操作符:

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

推荐阅读
author-avatar
心醉逸轩_620
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有