Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何从原始数组的每个单词开始在新数组中分块数组?所以每个数组的第一个字应该是前一个数组的第二个字。
例如
$list(1=>we, 2=>have, 3=>a, 4=>good, 5=>day);
使用 array_chunk 会给出新的数组 (we, have), (a, good), (day, and) 等等。但我想要
$newList(0=>(we, have), 1=>(have, a), 2=>(a, good), 3=>(good, day));
for ($i = 0; $i < count($list) - 2; $i++) { $newList[] = array($list[$i], $list[$i+1]); }
另一种方式:
<?php foreach ($list as $key => $word) { if ($key < count($list) - 1) $newlist[$key][] = $word; if ($key > 0) $newlist[$key-1][] = $word; } ?>