Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在数组中的每个数组之间放置字符串-PHP_Php_Arrays_Array Push - Fatal编程技术网

在数组中的每个数组之间放置字符串-PHP

在数组中的每个数组之间放置字符串-PHP,php,arrays,array-push,Php,Arrays,Array Push,假设我有一个包含9个句子的文本文件(可能更多!这只是一个示例),然后我在php中读取该文件,并将其每3个句子拆分一次,然后将其存储在一个变量中,从而生成以下数组: $table = array( array( 'text number 1', 'text number 2', 'text number 3' ), array( 'text number 4', 'text number 5',

假设我有一个包含9个句子的文本文件(可能更多!这只是一个示例),然后我在php中读取该文件,并将其每3个句子拆分一次,然后将其存储在一个变量中,从而生成以下数组:

$table = array(
   array(
        'text number 1',
        'text number 2',
        'text number 3'
    ),
   array(
        'text number 4',
        'text number 5',
        'text number 6'
    ),
   array(
        'text number 7',
        'text number 8',
        'text number 9'
    ),
 );
然后我想在每个数组之间添加这个字符串(
“[br/]”
),这样看起来:

$table = array(
   array(
        'text number 1',
        'text number 2',
        'text number 3'
    ),

   '[br/]',  // <<< ---- the string here

   array(
        'text number 4',
        'text number 5',
        'text number 6'
    ),

   '[br/]',  // <<< ---- the string here

   array(
        'text number 7',
        'text number 8',
        'text number 9'
    ),
);
从逻辑上讲,这应该是有效的,但事实并非如此

任何帮助都将不胜感激。

兄弟,我得看看手册
array\u push
更新传入的第一个参数。正确的语法是这样的

foreach( $table as $key => $row )
  array_push($output, $row, "[br /]");

您可以使用以下方法重新映射阵列:

$result = [];
foreach($table as $item) {
    $result[] = $item;
    $result[] = '[br/]';
}

阅读你的评论并试图理解你想要达到的目标,我建议你在一个数组中阅读所有的句子,然后使用

$chunks = array_chunk($input_array, 3);
将其拆分为每个数组所需数量的句子(例如3个),然后对其进行迭代,并使用
作为粘合剂内爆每个数组

$result = "";
foreach ($chunks as $chunk) {
    $result += implode("<br>", $chunk)
}
echo $result;
$result=”“;
foreach($chunk作为$chunk){
$result+=内爆(“
”,$chunk) } 回声$结果;
我可以知道你为什么要这么做吗?这听起来很像你在更大的事情上走错了方向。这个数组将如何使用?也称为@MCMXCII@FirstOne我喜欢,干杯。像魅力一样工作谢谢你,先生,也不要像其他人一样对这个问题敷衍了事,你直接回答,这就是我喜欢的,也谢谢你。问候先生:)
$result = "";
foreach ($chunks as $chunk) {
    $result += implode("<br>", $chunk)
}
echo $result;