问题
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