PHP - 4个索引的回声组

时间:2017-11-17 01:48:10

标签: php arrays indexing

我从具有以下信息的文本文档中提取信息

  

示例1,测试,示例1,测试,示例2,测试,示例2,测试,

我正在尝试从该文档中提取信息并将其显示如下:

  

Example1,Test,Example1,Test

     

示例2,测试,示例2,测试

     

依旧.......

这就是我现在所拥有的PHP代码,因为它取消了数组和索引中的消息。我的目标是尝试仅打印前4个索引,然后打破并打印下一个4索引,依此类推。我不确定我应该从哪里开始,所以如果有人能提供帮助,我会非常感激。



}




输出:



    <!DOCTYPE html>

<html>
<head>
</head>
<body>
<?php 
$print_data=file_get_contents("http://xxxxxxxxx.log");
$array = explode(',', $print_data);
$length = count($array);
$new_formatted_array = array();
for($i = 0; $i < $length; $i++){
    if( ($i + 1) % 4 == 1){
        $new_formatted_array[] = array();
    }
    $new_array_index = count($new_formatted_array) - 1;
    $new_formatted_array[$new_array_index][] = $array[$i];
}

//Just for demo
echo "<pre>";
print_r($new_formatted_array);
?>
</div>
</body>
</html>
&#13;
&#13;
&#13;

我使用了array_chuck,它显示了我想要的索引块,但它显示了array(),[1]数组等。我的最终目标是将四个索引作为一个组,但然后能够将其格式化为一个表。

表:

&#13;
&#13;
Array
(
    [0] => Array
        (
            [0] => Example1
            [1] =>  Test
            [2] =>  Example1
            [3] =>  Test
        )

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

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

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

)
&#13;
&#13;
&#13;

有人可以帮忙修复表的格式吗?不确定为什么行不均匀。

想出来,只是没有回显print_r

1 个答案:

答案 0 :(得分:0)

您可以将此作为基础。我只是在数组中放入一个值,以便您可以看到输出。

$array  = array(
     'Example1',
     'Test',
     'Example1',
     'Test',
     'Example2',
     'Test',
     'Example2',
     'Test',
     'Example3',
     'Test',
     'Example3',
     'Test'
);
$length = count($array);
$new_formatted_array = array();
for($i = 0; $i < $length; $i++){
    if( ($i + 1) % 4 == 1){
        $new_formatted_array[] = array();
    }
    $new_array_index = count($new_formatted_array) - 1;
    $new_formatted_array[$new_array_index][] = $array[$i];
}

//Just for demo
echo "<pre>";
print_r($new_formatted_array);

$output = '<table>';
//Display it in table
foreach($new_formatted_array as $i){
    $output .= '<tr>';
    foreach($i as $value){
        $output .= '<td>' . $value . '</td>';
    }
    $output .= '</tr>';
}

$output .= '</table>';
//Just for demo
echo htmlentities($output);

新阵列输出:

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

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

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

)

创建表格输出:

<table>
    <tr>
        <td>Example1</td>
        <td>Test</td>
        <td>Example1</td>
        <td>Test</td>
    </tr>
    <tr>
        <td>Example2</td>
        <td>Test</td>
        <td>Example2</td>
        <td>Test</td>
    </tr>
    <tr>
        <td>Example3</td>
        <td>Test</td>
        <td>Example3</td>
        <td>Test</td>
    </tr>
</table>