php数组解析的问题,遍历不大会
发布网友
发布时间:2023-01-01 12:56
我来回答
共2个回答
热心网友
时间:2023-10-06 06:11
你把数组弄成多行,就能一眼看出数组的结构,我整理如下:
array(1) {
["data"]=> array(6) {
[0]=> array(7) {
["id"]=> string(1) "4"
["pid"]=> string(1) "6"
["danbaofang"]=> string(4) "个人"
["danbao1name"]=> string(4) "测试"
["danbao1haoma"]=> string(4) "测试"
["nativeplace"]=> string(4) "测试"
["danbao1tel"]=> string(4) "测试"
}
[1]=> array(7) {
["id"]=> string(1) "5"
["pid"]=> string(1) "6"
["danbaofang"]=> string(4) "个人"
["danbao1name"]=> string(4) "测试"
["danbao1haoma"]=> string(4) "测试"
["nativeplace"]=> string(4) "测试"
["danbao1tel"]=> string(4) "测试"
}
[2]=> array(7) {
["id"]=> string(1) "6"
["pid"]=> string(1) "6"
["danbaofang"]=> string(4) "个人"
["danbao1name"]=> string(4) "测试"
["danbao1haoma"]=> string(4) "测试"
["nativeplace"]=> string(4) "测试"
["danbao1tel"]=> string(4) "测试" }
[3]=> array(7) {
["id"]=> string(1) "7"
["pid"]=> string(1) "6"
["danbaofang"]=> string(4) "个人"
["danbao1name"]=> string(4) "测试"
["danbao1haoma"]=> string(4) "测试"
["nativeplace"]=> string(4) "测试"
["danbao1tel"]=> string(5) "0测试"
}
[4]=> array(7) {
["id"]=> string(1) "8"
["pid"]=> string(1) "6"
["danbaofang"]=> string(4) "个人"
["danbao1name"]=> string(4) "测试"
["danbao1haoma"]=> string(4) "测试"
["nativeplace"]=> string(4) "测试"
["danbao1tel"]=> string(4) "测试"
}
[5]=> array(7) {
["id"]=> string(1) "9"
["pid"]=> string(1) "6"
["danbaofang"]=> string(4) "个人"
["danbao1name"]=> string(4) "测试"
["danbao1haoma"]=> string(4) "测试"
["nativeplace"]=> string(4) "测试"
["danbao1tel"]=> string(4) "测试"
}
}
}
现在是不是觉得很简单了,可以使用foreach和join输出,例如(加上你的数组是$arr):
echo '<table>';
foreach ($arr['data'] as $tr){
echo '<tr><td>' . implode('<td>', $tr);
}
echo '</table>';
追问那么假如以上数组我是存在变量$users中!您的代码我改如何改呢!新手 请见谅!
追答只改一个地方:第2行的arr三个字母修改为users五个字母。
热心网友
时间:2023-10-06 06:11
foreach($user['data'] as $v){
$str.="<td>".$v['id']."</td><td>".$v['pid']."</td>......";//自己补完
}