没有索引值行的打印数组

时间:2018-07-29 08:27:22

标签: php arrays

print "<pre>";
print_r($arr);
print "</pre>";

结果:

Array
(
    [0] => Array
        (
            [home_id] => 51
            [0] => 51
            [home_what] => bann
            [1] => bann
            [home_artid] => 
            [2] => 
            [home_bannid] => 27
            [3] => 27
       )
   [1] => Array
       (. . . etc

如何在没有index - value行的情况下获得结果? 像这样:

[home_id] => 51
[home_what] => bann
[home_artid] => 
[home_bannid] => 27

lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum

2 个答案:

答案 0 :(得分:1)

您可以尝试将array_maparray_unique

摘要

$arr = [['home_id' => 51,
            '0' => 51,
            'home_what' => 'bann',
            '1' => 'bann',
            'home_artid' => '',
            '2' => '',
            'home_bannid' => 27,
            '3' => 27]];
$result = array_map("array_unique", $arr);
print_r($result);

输出

Array
(
    [0] => Array
        (
            [home_id] => 51
            [home_what] => bann
            [home_artid] => 
            [home_bannid] => 27
        )

)

实时demo

详细了解

array_map

array_unique

注意:如果您的数据来自数据库,则可以使用mysqli_fetch_assoc

答案 1 :(得分:0)

为什么不使用简单的foreach循环并打印它。

$arr = array(1,2,3,4,5);

foreach($arr as $arr_item){

   echo $arr_item;
}