在PHP中,Codeigniter:我的数组$ phoneList包含以下内容:
Array
(
[name] => One, Are
[telephonenumber] => 555.222.1111
)
Array
(
[name] => Two, Are
[telephonenumber] => 555.222.2222
)
Array
(
[name] => Three, Are
[telephonenumber] => 555.222.3333
)
如何列出每个名字?每个号码出来?我说我的数组包含三个不同的数组吗?对于包含数组的数组,这是正常的吗?
当我执行print_r($ phoneList)时,我得到以下内容:
Array ( [0] => Array ( [name] => One, Are [telephonenumber] => 555.222.1111 ) [1] => Array ( [name] => Two, Are [telephonenumber] => 555.222.2222 ) [2] => Array ( [name] => Three, Are [telephonenumber] => 555.222.3333 ) )
答案 0 :(得分:2)
您可能希望使用foreach
来循环播放它们。像这样:
foreach($data as $arr) { // assuming $data is the variable that has all this in
echo $arr['name'].": ".$arr['telephonenumber']."<br />";
}
答案 1 :(得分:1)
答案 2 :(得分:1)
拥有一个数组数组(在本例中是一个关联数组数组)是完全正常的。它们可以这样写:
$arrayofarray = array(array('name' => 'aname', 'phone'=>'22233344444'), array('name' => 'bobble', 'phone'=>'5552223333'));
print_r($arrayofarray);
您应该能够以这种方式打印出内容:
foreach ($arrayofarray as $arr){
print $arr['name']."\n";
print $arr['phone']."\n";
}
如果您想知道每个关联数组中设置了哪些术语,可以使用array_keys()返回它们(作为简单数组)。例如:
foreach ($arrayofarray as $arr){
$setterms=array_keys($arr);
foreach ($setterms as $aterm){
print "$aterm -> ".$arr[$aterm]."\n";
}
}