<HTML>
<HEAD>
<TITLE>对分查找法</TITLE>
</HEAD>
<BODY><?php
/*
定义一个函数
*/
function byName($left, $right)
{
return(strcmp($left[0], $right[0]));
} /*
** 创建数组:Name, Title, Wage
*/
$employee[] = array("tom, John", "Programmer", 20);
$employee[] = array("jas, John", "Programmer", 20);
$employee[] = array("june, John", "Programmer", 20);
$employee[] = array("aili, John", "Programmer", 20);
$employee[] = array("Doe, Jane", "Programmer", 20);
$employee[] = array("Mann, Joe", "Manager", 35);
$employee[] = array("Smith, John", "CEO", 50);
$employee[] = array("Leeds, Al", "President", 100); /*
** 排序
*/
usort($employee, "byName");for($i=0;$i<=7;$i++)
print $employee[$i][0]."<br>"; /*
** 查找对象
*/
$Name = "Mann, Joe";
$lower_limit = 0;
$upper_limit = count($employee) - 1;$index = intval(($lower_limit + $upper_limit)/2); if(strcmp($employee[$index][0], $Name) > 0)
{
/*
** 结果在上半部分
*/
while(strcmp($employee[$index][0], $Name) != 0)
{$index = $index - 1;}
echo $index."<br>";
}
elseif(strcmp($employee[$index][0], $Name) < 0)
{
/*
** 结果在下半部分
*/
while(strcmp($employee[$index][0], $Name) != 0)
{$index = $index + 1;}
echo $index."<br>";
}
else
{
/*
** 发现结果
*/
echo $index."<br>";
}
/*
** 打印结果
*/
print("Mann, Joe 在数组中的位置:"." $index<BR>\n");?>
</BODY>
</HTML>
输出
Doe, Jane
Leeds, Al
Mann, Joe
Smith, John
aili, John
jas, John
june, John
tom, John
2
Mann, Joe 在数组中的位置: 2
我的问题是:
我不知道该怎么从if else语句 中提取$return的值
我只能用echo 来提取 但会被输出
可以用什么来提取呢?
用return 来代替echo 好像不行,好像因为不是函数,return不管用。
谢谢了。。
<HEAD>
<TITLE>对分查找法</TITLE>
</HEAD>
<BODY><?php
/*
定义一个函数
*/
function byName($left, $right)
{
return(strcmp($left[0], $right[0]));
} /*
** 创建数组:Name, Title, Wage
*/
$employee[] = array("tom, John", "Programmer", 20);
$employee[] = array("jas, John", "Programmer", 20);
$employee[] = array("june, John", "Programmer", 20);
$employee[] = array("aili, John", "Programmer", 20);
$employee[] = array("Doe, Jane", "Programmer", 20);
$employee[] = array("Mann, Joe", "Manager", 35);
$employee[] = array("Smith, John", "CEO", 50);
$employee[] = array("Leeds, Al", "President", 100); /*
** 排序
*/
usort($employee, "byName");for($i=0;$i<=7;$i++)
print $employee[$i][0]."<br>"; /*
** 查找对象
*/
$Name = "Mann, Joe";
$lower_limit = 0;
$upper_limit = count($employee) - 1;$index = intval(($lower_limit + $upper_limit)/2); if(strcmp($employee[$index][0], $Name) > 0)
{
/*
** 结果在上半部分
*/
while(strcmp($employee[$index][0], $Name) != 0)
{$index = $index - 1;}
echo $index."<br>";
}
elseif(strcmp($employee[$index][0], $Name) < 0)
{
/*
** 结果在下半部分
*/
while(strcmp($employee[$index][0], $Name) != 0)
{$index = $index + 1;}
echo $index."<br>";
}
else
{
/*
** 发现结果
*/
echo $index."<br>";
}
/*
** 打印结果
*/
print("Mann, Joe 在数组中的位置:"." $index<BR>\n");?>
</BODY>
</HTML>
输出
Doe, Jane
Leeds, Al
Mann, Joe
Smith, John
aili, John
jas, John
june, John
tom, John
2
Mann, Joe 在数组中的位置: 2
我的问题是:
我不知道该怎么从if else语句 中提取$return的值
我只能用echo 来提取 但会被输出
可以用什么来提取呢?
用return 来代替echo 好像不行,好像因为不是函数,return不管用。
谢谢了。。
if(strcmp($employee[$index][0], $Name) > 0)
{
/*
** 结果在上半部分
*/
while(strcmp($employee[$index][0], $Name) != 0)
{$index = $index - 1;}
print("Mann, Joe 在数组中的位置:"." $index<BR>\n");
}
<HEAD>
<TITLE>对分查找法 </TITLE>
</HEAD>
<BODY> <?php
/*
定义一个函数
*/
function byName($left, $right)
{
return(strcmp($left[0], $right[0]));
} /*
** 创建数组:Name, Title, Wage
*/
$employee[] = array("tom, John", "Programmer", 20);
$employee[] = array("jas, John", "Programmer", 20);
$employee[] = array("june, John", "Programmer", 20);
$employee[] = array("aili, John", "Programmer", 20);
$employee[] = array("Doe, Jane", "Programmer", 20);
$employee[] = array("Mann, Joe", "Manager", 35);
$employee[] = array("Smith, John", "CEO", 50);
$employee[] = array("Leeds, Al", "President", 100); /*
** 排序
*/
usort($employee, "byName"); for($i=0;$i <=7;$i++)
print $employee[$i][0]." <br>"; /*
** 查找对象
*/
$Name = "Mann, Joe";
$lower_limit = 0;
$upper_limit = count($employee) - 1; $index = intval(($lower_limit + $upper_limit)/2);
if(strcmp($employee[$index][0], $Name) > 0)
{
/*
** 结果在上半部分
*/
while(strcmp($employee[$index][0], $Name) != 0)
{$index = $index - 1;
$result = $index;
}
}
elseif(strcmp($employee[$index][0], $Name) < 0)
{
/*
** 结果在下半部分
*/
while(strcmp($employee[$index][0], $Name) != 0)
{$index = $index + 1;
$result = $index;
}
}
else
{
/*
** 发现结果
*/
$result = $index;
} echo $result;