douzai2562
2016-04-07 12:47
浏览 137
已采纳

获取一个数组的每个第n个元素并将其放入另一个数组PHP中

So I have an array like this:

array(6) { [0]=> string(11) "12323423423" [1]=> string(4) "tito" [2]=> string(6) "235345" [3]=> string(14) " 564534534534" [4]=> string(5) "kralj" [5]=> string(6) "435345" }

Depending on number of elements from another array called $anotherArray, let's say $anotherArray has 3 elements, I should take first 3 elements of first array, then if there are second 3 elements and so on, and put them into another array. I tried it like so:

$lengthManuelni=count($string);// $string being array displayed uphere
$lengthAnothera=count($anotherArray);
for ($i = 0; $i < $lengthManuelni; $i += $lengthAnothera) { 
    for ($j = 0; $j < $lengthAnothera; $j++) {
        $restructured [$j] = $string[$i + $j];
        var_dump($restructured);
    }
    }

So i would like this $restructured array to look like this:

array(2) { [0]=> string(23) "12323423423,tito,235345" [1]=> string(28) " 564534534534,kralj,435345" }

Instead it when I do var_dump($restructured) it looks like this:

array(1) { [0]=> string(11) "12323423423" } array(2) { [0]=> string(11) "12323423423" [1]=> string(4) "tito" } array(3) { [0]=> string(11) "12323423423" [1]=> string(4) "tito" [2]=> string(6) "235345" } array(3) { [0]=> string(14) " 564534534534" [1]=> string(4) "tito" [2]=> string(6) "235345" } array(3) { [0]=> string(14) " 564534534534" [1]=> string(5) "kralj" [2]=> string(6) "235345" } array(3) { [0]=> string(14) " 564534534534" [1]=> string(5) "kralj" [2]=> string(6) "435345" }

Please help, I'm stuck with this.

图片转代码服务由CSDN问答提供 功能建议

所以我有一个 array ,如下所示: < pre> array(6){[0] =&gt; string(11)“12323423423”[1] =&gt; string(4)“tito”[2] =&gt; string(6)“235345”[3] =&gt; string(14)“564534534534”[4] =&gt; string(5)“kralj”[5] =&gt; string(6)“435345”}

根据另一个名为 $ anotherArray 的数组中元素的数量,假设 $ anotherArray 有3个元素,我应该取第一个数组的前3个元素,然后如果有第二个3个元素,依此类推,并将它们放入另一个数组中。 我尝试过这样:

  $ lengthManuelni = count($ string); // $ string是数组显示的最大值
 $ lengthAnothera = count($ anotherArray); 
for  ($ i = 0; $ i&lt; $ lengthManuelni; $ i + = $ lengthAnothera){
 for($ j = 0; $ j&lt; $ lengthAnothera; $ j ++){
 $ restructured [$ j] =  $ string [$ i + $ j]; 
 var_dump($ restructured); 
} 
} 
   
 
 

所以我希望这个 $ 重组的数组看起来像这样:

  array(2){[0] =&gt;  string(23)“12323423423,tito,235345”[1] =&gt;  string(28)“564534534534,kralj,435345”} 
   
 
 

相反,当我执行 var_dump($ restructured)时,它看起来像这样 :

  array(1){[0] =&gt;  string(11)“12323423423”} array(2){[0] =&gt;  string(11)“12323423423”[1] =&gt;  string(4)“tito”} array(3){[0] =&gt;  string(11)“12323423423”[1] =&gt;  string(4)“tito”[2] =&gt;  string(6)“235345”} array(3){[0] =&gt;  string(14)“564534534534”[1] =&gt;  string(4)“tito”[2] =&gt;  string(6)“235345”} array(3){[0] =&gt;  string(14)“564534534534”[1] =&gt;  string(5)“kralj”[2] =&gt;  string(6)“235345”} array(3){[0] =&gt;  string(14)“564534534534”[1] =&gt;  string(5)“kralj”[2] =&gt;  string(6)“435345”} 
   
 
 

请帮助,我坚持这一点。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujiao6116 2016-04-07 13:01
    已采纳

    It's much simpler to achieve this using array_chunk and array_map functions:

    $restructured = array_map(function($v){
        return implode(",", $v);
    }, array_chunk($lengthManuelni, 3));
    
    print_r($restructured);
    

    The output:

    Array
    (
        [0] => 12323423423,tito,235345
        [1] => 564534534534,kralj,435345
    )
    

    http://php.net/manual/en/function.array-chunk.php

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题