删除阵列中的标识符的第一层次 [英] Remove first levels of identifier in array

查看:179
本文介绍了删除阵列中的标识符的第一层次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这以前一直在涨,但could'nt找到任何答案。如果它已经回答,请点我一个链接正确的方向。

我有我wan't删除标识符的第一个层次的数组。我认为这是一个功能呢?

是多么示例:

  [0] =>排列
        (
            [8] =>竿
        )[1] =>排列
        (
            [8] => BLA
        )[2] =>排列
        (
            [6] =>波波
        )[3] =>排列
        (
            [8] => GRON
        )[4] =>排列
        (
            [7] =>斯登
        )[5] =>排列
        (
            [8] =>维生素
        )[6] =>排列
        (
            [7] => GULD
        )[7] =>排列
        (
            [6] => Lyxig
        )

我wan't什么

  [8] =>竿
[8] => BLA
[6] =>波波
[8] => GRON
[7] =>斯登
[8] =>维生素
[7] => GULD
[6] => Lyxig


解决方案

这里的问题是preserving你想要的标识的按键。你必须具有相同的密钥(如BLA和杆)的一些名称的字符串。你要么需要将这些存储阵列或愿意失去的关键。

与PHP5.3例如:

  $ =加工array_map(函数($ A){返回array_pop($ A);} $ ARR);

会给你:

  [0] =>竿
[1] => BLA
[2] =>波波
[3] => GRON
[4] =>斯登
[5] =>维生素
[6] => GULD
[7] => Lyxig

现在已经很清楚的内阵列上的按键需要preserved监守它们某种的id。随着中说,你必须改变最终结构youre去,因为你可以有相同的键2在一个单一的阵列。那么最简单的结构变为:

  [8] =>排列
        (
            [0] =>竿,
            [1] => BLA,
            [2] =>维生素,
            [3] => GRON
        )[6] =>排列
        (
            [0] =>波波,
            [1] => Lyxig
        )[7] =>排列
        (
            [0] =>斯登,
            [1] => GULD
        )

要得到这个结构的简单循环将工作:

  $ =处理阵列();
的foreach($改编为subarr $){
   的foreach($ subarr为的$ id => $值){
      如果(!使用isset($处理[$ ID]){
         $处理[$ ID] =阵列();
      }      $处理[$ ID] [] = $价值;
   }
}

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

解决方案

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

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 becuase they are some kind of id. With that said you must change the end structure youre 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;
   }
}

这篇关于删除阵列中的标识符的第一层次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆