PHP 遍历多维数组

Array
(
[comments] => Array
(
[672] => Array
(
[cid] => 672
[gid] => 650
[pid] => 0
[date] => 约 1 小时前
[poster] => Firefox
[comment] => 测试下验证码刷新
[mail] => dyelcwh%40outlook.com
[url] => http%3A%2F%2Faaaaaaaa
[ip] => 118.123.16.120
[hide] => n
[content] => %E6%B5%8B%E8%AF%95%E4%B8%8B%E9%AA%8C%E8%AF%81%E7%A0%81%E5%88%B7%E6%96%B0
[children] => Array
(
)
[level] => 0
)
[671] => Array
(
[cid] => 671
[gid] => 650
[pid] => 0
[date] => 约 2 小时前
[poster] => Firefox
[comment] => 大集合的么么的ad
[mail] => dyelcwh%40outlook.com
[url] => http%3A%2F%2Faaaaaaaa
[ip] => 118.123.16.120
[hide] => n
[content] => %E5%A4%A7%E9%9B%86%E5%90%88%E7%9A%84%E4%B9%88%E4%B9%88%E7%9A%84ad
[children] => Array
(
)
[level] => 0
)
)
[commentStacks] => Array
(
[0] => 672
[1] => 671
)
[commentPageUrl] =>
)

function ajaxListComment($params) {
$Comment_Model = new Comment_Model();
$blogId = isset($_GET['gid']) ? intval($_GET['gid']) : -1;
$arr = $Comment_Model->getCommentsByAjax(0, $blogId, '', 1);
$json = '{"comments":[';
foreach((array)$arr as $key=>$value){//第一层
if($key == 'comments'){
foreach((array)$value as $key2=>$value2){//第二层
$comm = '{';
foreach ($value2 as $key3 => $value3) {//第三层
if($key3 == 'children'){
continue;
}
$comm .= '"'.$key3.'":"'.urldecode($value3).'",';
}
$comm = substr($comm, 0, strlen($comm) - 1);
$json .= $comm.'},';
}
}
}
$json = substr($json, 0, strlen($json) - 1);
echo $json.']}';
//echo print_r($arr);
//进行URL编码,防止Unicode化
//echo urldecode(json_encode($arr));
}

 

知识共享许可协议
《PHP 遍历多维数组》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

1 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     159.70ms