把一排推到阵列throgh foreach

时间:2016-07-28 13:49:23

标签: php arrays

现在我为我的网站做点什么。 我有一个数组并使用foreach循环来获取它,我想在键结束时,值将推入数组 例如:

 foreach($arr as $key => $value){
 $resulset = $model->getResultset($value);
  foreach($resultset as $k => $v){
   $k will increase from 0-5 or 0-3 depends on result set return it will loop through arr

  }    }

我希望将值从循环内的结果集推送到数组

但它给了我结果

array(
 array(
  [0] => a
  )
array(
  [0] => b
  )
...
)

我想要的是

array (
 array (
   [0] => a,
   [1]=> b


 )
)

2 个答案:

答案 0 :(得分:1)

使用str_split将字符串的字符拆分为数组。

<?php

$arr = ['012345', '012345', '012', '012', '0123', '0123'];

$output = array_map('str_split', $arr);

print_r($output);

输出:

Array
(
    [0] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
            [5] => 5
        )

    [1] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 4
            [5] => 5
        )

    [2] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
        )

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

    [4] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
            [3] => 3
        )

    [5] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
            [3] => 3
        )

)

答案 1 :(得分:0)

试试这个:

<?php 
$arr = array('012345','012345','012','012','0123','0123');
$out = array();
foreach($arr as $k => $v){
    $number_array = str_split($v);
    $out[] = $number_array;
}
echo "<pre>"; print_r($out); exit;