如何让这段代码工作?代码组织array2,输出array2中的前两个结果,然后输出下面数组的其余部分。"有些行来自python".
$array2 = array(101040=>"one",101060=>"two",102040=>"three",102020=>"four");
$array1 = new ArrayObject();
$array1->append("101040");
$array1->append("101060");
$array1->append("102020");
if (strlen($array1)==0)
{
print "Array is empty.";
}
else if(strlen($array1)>2)
{
$array1->asort();
print ($array2[$array1[0]]);
print ($array2[$array1[1]]);
for a in range(int(strlen($array1))-2);
print ($array2[$array1[a+2]]);
}
else
{
$array1->asort();
for a in range(strlen($array1)):
print($array2[$array1[a]]);
}
这个怎么样?
<?php
$array1 = array("101040", "101060", "102020");
$array2 = array(101040 => "one", 101060 => "two", 102040 => "three", 102020 => "four");
if (count($array1) == 0) {
print "Array is empty.";
} else if(count($array1) > 2) {
asort($array1);
print ($array2[$array1[0]]);
print ($array2[$array1[1]]);
for( $i = 0, $max = count($array1)-2; $i < $max; $i++) {
print ($array2[$array1[$i+2]]);
}
} else {
asort($array1);
for( $i = 0, $max = count($array1); $i < $max; $i++) {
print ($array2[$array1[$i]]);
}
}
?>
输出onetwofour
。查看codepad.org上运行的测试