将一组分隔字符串分解为两个数组 [英] explode an array of delimited strings into two arrays

查看:127
本文介绍了将一组分隔字符串分解为两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

 数组

[0] => 10 -7
[1] => 11-3
[2] => 11-7
[3] => 12-3
[4] => ; 12-7
[5] => 13-3
[6] => 13-7
[7] => 14-3
[8] => 14-7
[9] => 15-7

我需要将 - 分隔为两个数组作为分隔符:

  Array 

[0] => 10
[1] => 11
[2] => 11
[3 ] => 12
[4] => 12
[5] => 13
[6] => 13
[7] => 14
[8] => 14
[9] => 15



 数组

[0] => 7
[1] => 3
[2] => 7
[3] => 3
[4] => 7
[5] => 3
[6] => 7
[7] => 3
[8] => 7
[9] => 7

有没有像 array_explode 那是我想要的吗?或者php array 函数的组合?如果可能的话,我希望在没有通过我自己的 for / 每个循环的情况下执行此操作,或者至少使当内置的东西已经存在时,不得不重新发明轮子。我已经用for循环做了这个。但我不能动摇那种巧妙地使用数组函数或类似东西的更优雅方式的感觉。非常感谢,伙计。

其他信息:



不确定是否事实上,但我实际上是在所得到的两个数组中的唯一值:

  Array 

[0] => 10
[1] => 11
[2] => 12
[3] => 13
[4] => ; 14
[5] => 15

数组

[0] => 7
[1] => 3数组
$ b

  

唯一值不需要排序,键可以保留或者不是,第一个数组的合法值范围从 0 23 ,而第二个 c $ c> 1 7 。然而,除了这些值( 0 23 1 7 或者甚至是不受限制的流浪字符串或者超出我的控制范围的其他数据类型),我肯定会抛弃它。

解决方案

您正在寻找的魔术项目是 array_reduce() ,例如(PHP 5.3 +):

  list($ left,$ right)= array_reduce($ input,
function $ memo,$ item){
list($ l,$ r)= explode(' - ',$ item);
$ memo [0] [$ l] = $ memo [1] [ $ b $返回$ memo;
},
数组(array(),array())
);

var_dump(array_keys($ left),array_keys($ right));

您可以

使用PHP< 5.3时,您必须提前声明函数:

p>

 函数my_reducer($ memo,$ item){
list($ l,$ r)= // ...
// ...如上...
}

list($ left,$ right)= array_reduce(
$ input,'my_reducer',
array(array(),array())
);


I have the following array:

Array
    (
        [0] => 10-7
        [1] => 11-3
        [2] => 11-7
        [3] => 12-3
        [4] => 12-7
        [5] => 13-3
        [6] => 13-7
        [7] => 14-3
        [8] => 14-7
        [9] => 15-7
    )

that I need to split into two arrays using "-" as delimiter:

Array
    (
        [0] => 10
        [1] => 11
        [2] => 11
        [3] => 12
        [4] => 12
        [5] => 13
        [6] => 13
        [7] => 14
        [8] => 14
        [9] => 15
    )

and

Array
    (
        [0] => 7
        [1] => 3
        [2] => 7
        [3] => 3
        [4] => 7
        [5] => 3
        [6] => 7
        [7] => 3
        [8] => 7
        [9] => 7
    )

Is there anything like array_explode that does what I want? or a combination of php array functions? I'd like to do this without going through my own for/each loop, if possible, or at least minimize having to reinvent the wheel when something (pseudo)in-built is already out there. I already did this with a for loop. But I can't shake the feeling that there's a more elegant way that smartly uses array functions or anything of that kind. Thanks so much, guys.

Additional info:

Not sure if it matters, but I'm actually after the unique values in the resulting two arrays:

Array
    (
        [0] => 10
        [1] => 11
        [2] => 12
        [3] => 13
        [4] => 14
        [5] => 15
    )

and

Array
    (
        [0] => 7
        [1] => 3
    )

The unique values don't need to be sorted, the keys may be preserved or not, and the legal values of the first array range from 0 to 23, while those of the second 1 to 7. However it's possible to have values other than these (0 to 23 and 1 to 7 or even undelimited stray strings or other data types beyond my control), which I would definitely want to throw out.

解决方案

The magic bullet you're looking for is array_reduce(), e.g. (PHP 5.3+):

list( $left, $right ) = array_reduce( $input,
  function( $memo, $item ) {
    list( $l, $r ) = explode( '-', $item );
    $memo[0][$l] = $memo[1][$r] = true;
    return $memo;
  },
  array( array(), array() )
);

var_dump( array_keys( $left ), array_keys( $right ) );

You can see it in action here.

With PHP <5.3 you'll have to declare the function ahead of time:

function my_reducer( $memo, $item ) {
  list( $l, $r ) = // ...
  // ... as above ...
}

list( $left, $right ) = array_reduce(
  $input, 'my_reducer',
  array( array(), array() )
);

这篇关于将一组分隔字符串分解为两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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