explode() each element in array, yielding multidimensional array

强颜欢笑 提交于 2019-12-25 03:00:33

问题


How can I use the PHP function explode() on each array entry?

For example:

foreach ($persons as $person) {
    $zips = $person->getZipResponsibility();
    $zips = nl2br($zips);

    $rangesArray = explode('<br />', $zips);
}

Returns this:

Array
(
    [0] => 10000-20000
    [1] => 30000-40000
)

This works great but now I have to explode each array part for an output like this:

Array
(
  [0] => Array (
     [0] => 10000
     [1] => 20000
  )
  [1] => Array (
     [0] => 30000
     [1] => 40000
  )
)

回答1:


This should work, haven't tested.

$rArray = array();

foreach($rangesArray as $key=>$val) {
    $rArray[] = explode('-', $val);
}



回答2:


Just use another explode, this time like this: explode('-', $i);. You'll have to put it in a foreach like this:

$x = 0;
foreach ($rangesArray as $i)
{
  $arr = explode('-', $i);
  foreach ($arr as $j)
  {
    $arr1[$x] = $j;
  }
  $x++;
}

var_dump($arr1);


来源:https://stackoverflow.com/questions/12322801/explode-each-element-in-array-yielding-multidimensional-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!