分解数组并输出到表?

时间:2016-12-29 06:49:16

标签: php arrays

我运行游戏服务器并将一些信息保存在数据库中。 我运行一个MySQL查询,提取像cargo_items(下面)这样的信息。 如何在PHP中正确格式化这些数据?我希望它能在桌子上。那可能吗?我对这样处理数组的知识仅限于复杂的性质。这是从数据库返回数据的方式。

 Array
 (
   [0] => Array
   (
     [0] => Array
     (
       [id] => 2237
       [cargo_weapons] =>      
       [
         ["MMG_02_black_F","","","",[],""],
         ["arifle_SDAR_F","","","",[],""],
         ["arifle_SDAR_F","","","",[],""],        
         ["arifle_SPAR_03_khk_F","","","",[],""],
         ["LMG_Zafir _F","","","",[],""],
         ["MMG_02_black_F","","","",[],""],
         ["MMG_02_black_F","","","",[],""]
       ]
     )
   )
 )

输出应该是一个表格:

 Weapons
 -------
 MMG_02_black_F
 arifle_SDAR_F
 arifle_SDAR_F
 arifle_SPAR_03_khk_F
 LMG_Zafir_F
 MMG_02_black_F
 MMG_02_black_F

1 个答案:

答案 0 :(得分:0)

由于你有多维数组,你需要自定义递归函数,或者在你的特定情况下,你可以使用这样的东西:

function get_items($item, $key)
{
    if(!empty($item) && $key=='cargo_weapons')
    //your html table cells
    echo "$item<br>";
}

array_walk_recursive($array, 'get_items');

array_walk_recursive

将完成这项工作(此函数返回true或false),但您也可以(ab)使用它来显示所需的HTML。