<?php
$data = 'a:4:{s:4:"Name";s:8:"John Doe";s:7:"Address";a:4:{s:6:"Street";s:11:"123 Main St";s:4:"City";s:5:"Dover";s:5:"State";s:2:"DE";s:3:"Zip";s:5:"19904";}s:5:"Phone";s:12:"555-555-1234";s:5:"Email";s:16:"john@example.com";}
a:4:{s:4:"Name";s:8:"Jane Doe";s:7:"Address";a:4:{s:6:"Street";s:11:"123 Main St";s:4:"City";s:5:"Dover";s:5:"State";s:2:"DE";s:3:"Zip";s:5:"19904";}s:5:"Phone";s:12:"555-555-5678";s:5:"Email";s:16:"jane@example.com";}
a:4:{s:4:"Name";s:15:"Jonny Appleseed";s:7:"Address";a:4:{s:6:"Street";s:15:"1 Infinite Loop";s:4:"City";s:10:"Coopertino";s:5:"State";s:2:"CA";s:3:"Zip";s:5:"90201";}s:5:"Phone";s:12:"555-555-9101";s:5:"Email";s:15:"jonny@apple.com";}
a:4:{s:4:"Name";s:12:"Jack Sparrow";s:7:"Address";a:4:{s:6:"Street";s:15:"The Black Pearl";s:4:"City";s:17:"Cut Throat Island";s:5:"State";s:7:"Tortuga";s:3:"Zip";s:5:"00001";}s:5:"Phone";s:12:"555-555-1213";s:5:"Email";s:14:"jack@savvy.com";}
a:4:{s:4:"Name";s:14:"Luke Skywalker";s:7:"Address";a:4:{s:6:"Street";s:15:"17 Jedi Council";s:4:"City";s:8:"Corasant";s:5:"State";s:2:"XX";s:3:"Zip";s:5:"99999";}s:5:"Phone";s:12:"555-555-1415";s:5:"Email";s:17:"luke@theforce.com";}';
$fexplode = explode(PHP_EOL, $data);
foreach ($fexplode as $uline) {
?>
<table cellpadding="2" cellspacing="2" align="left">
<table>
<?php
foreach (unserialize($uline) as $item => $value){
?>
<tr>
<td><b><?php echo $item . ": ";?></b></td>
<td><?php if ($item == 'Address'){
print "Need to print address array here";
}
else {
echo $value;
}
?> </td>
</tr>
<br>
<?php } ?>
</table>
<?php } ?>
这会在一个表中打印数组,但是当它到达数组中的数组的地址部分时,如果我替换'print'需要在这里打印地址数组,那么就会吐出“数组转换为字符串” echo $ value;'
答案 0 :(得分:1)
在你的情况下echo $value;
如果$ value是一个数组,它将显示通知“数组到字符串转换”。
使用foreach来回显数组。
if(is_array($value)):
foreach($value as $value_in):
echo $value_in;
endforeach;
endif;
答案 1 :(得分:0)
检查具有地址值的项目,然后分配内爆。
<td><b><?php echo ($item == 'Address') ? '' : $value." " ;?></b></td>
<td><?php if ($item == 'Address'){
print implode("<br />",$value);
}
else {
echo $value;
}
?> </td>
答案 2 :(得分:0)
<?php
$data = 'a:4:{s:4:"Name";s:8:"John Doe";s:7:"Address";a:4:{s:6:"Street";s:11:"123 Main St";s:4:"City";s:5:"Dover";s:5:"State";s:2:"DE";s:3:"Zip";s:5:"19904";}s:5:"Phone";s:12:"555-555-1234";s:5:"Email";s:16:"john@example.com";}
a:4:{s:4:"Name";s:8:"Jane Doe";s:7:"Address";a:4:{s:6:"Street";s:11:"123 Main St";s:4:"City";s:5:"Dover";s:5:"State";s:2:"DE";s:3:"Zip";s:5:"19904";}s:5:"Phone";s:12:"555-555-5678";s:5:"Email";s:16:"jane@example.com";}
a:4:{s:4:"Name";s:15:"Jonny Appleseed";s:7:"Address";a:4:{s:6:"Street";s:15:"1 Infinite Loop";s:4:"City";s:10:"Coopertino";s:5:"State";s:2:"CA";s:3:"Zip";s:5:"90201";}s:5:"Phone";s:12:"555-555-9101";s:5:"Email";s:15:"jonny@apple.com";}
a:4:{s:4:"Name";s:12:"Jack Sparrow";s:7:"Address";a:4:{s:6:"Street";s:15:"The Black Pearl";s:4:"City";s:17:"Cut Throat Island";s:5:"State";s:7:"Tortuga";s:3:"Zip";s:5:"00001";}s:5:"Phone";s:12:"555-555-1213";s:5:"Email";s:14:"jack@savvy.com";}
a:4:{s:4:"Name";s:14:"Luke Skywalker";s:7:"Address";a:4:{s:6:"Street";s:15:"17 Jedi Council";s:4:"City";s:8:"Corasant";s:5:"State";s:2:"XX";s:3:"Zip";s:5:"99999";}s:5:"Phone";s:12:"555-555-1415";s:5:"Email";s:17:"luke@theforce.com";}';
$fexplode = explode(PHP_EOL, $data);
foreach ($fexplode as $uline) {
?>
<table cellpadding="2" cellspacing="2" align="left">
<table>
<?php
foreach (unserialize($uline) as $item => $value){
?>
<tr>
<td><b><?php echo $item . ": ";?></b></td>
<td><?php if ($item == 'Address'){
foreach ($value as $current_address_data) {
echo $current_address_data ;
}
}
else {
echo $value;
}
?> </td>
</tr>
<br>
<?php } ?>
</table>
<?php } ?>