我正在尝试合并代码的两个部分。为此,我必须存储我调用的函数的 echo'd 数组列输出多次进入一个或多个数组,我可以在其上执行计算。
这是我正在尝试做的:
<?php
$searchquery[1] ="jupiter";
$searchquery[2] ="venus";
//can be multiple queries
include ('find.php');
for($i=0;$i<count($searchquery);$i++)
{
find($searchquery[$i]);
}
/find.php echoes back to me a MySQL query which then
creates a 2 dimensional array for me called
/$searchresult which looks like this as an example
t x (let's call first column t for example, and second x)
|1|3|
|1|4|
|2|6|
|4|8|
|7|1|
and it echoes it back to me, this works.
But, i need to use the first column (t) (11247) output from find.php
which was the result of the searchquery "jupiter",
and i need to store it as some sort of array in this current sheet,
theni need to store the "venus" searchquery which is let's say
t x
|1|3|
|2|4|
|3|4|
|4|6|
|5|4|
并将第一列 (t) 作为数组存储在此当前工作表中。
我正在尝试将查找函数的回声存储为数组.php以便我可以在当前工作表中执行以下操作:
$venusarrayt = array(1, 1, 2, 4, 7); //the manually defined
//$searchresult first column output from find.php which echos to me (t) (11247)
$jupiterarrayt = array(1, 2, 3,4,5); //the manually defined
//$searchresult first column output from find.php which echos to me (t) (12345)
//I need to perform the following operation and sum all of the t combinations
for($l=0;$l<count($venusarrayt);$l++){
for($s=0;$s<count($jupiterarrayt);$s++){
echo $venusarrayt[$l]+$jupiterarrayt[$s];
这部分有效!但是虽然将回显$searchresult
输出合并到一个可以执行上述操作的数组中,但我遇到了麻烦对于循环。在这个例子中,我通过在php表中键入"venusarrayt和jupiterarrayt"来手动完成。
确信有某种方法可以将我多次调用的函数的 echo'd 数组列结果存储到数组中,但是我还没想通怎么做。请帮忙。
我希望这有帮助:
<?php
$searchquery[0] ="jupiter";
$searchquery[1] ="venus";
//can be multiple queries
include ('find.php');
$results=null;
for($i=0;$i<count($searchquery);$i++)
{
$temp=find($searchquery[$i]);
$results[$i]=$temp[t];
}
for($l=0;$l<count($results[1]);$l++){
for($s=0;$s<count($results[0]);$s++){
echo $results[1][$l]+$results[0][$s];
}
}
进一步处理搜索数据的清晰解决方案可能如下所示:
$searchquery[1] ="jupiter";
$searchquery[2] ="venus";
$search_result = array(); $i=0;
foreach($searchquery as $current_query){
$current_result = FindResul($current_query);
// FindResult will be your function which process single query and returns result of it
$search_result[$i]['query'] = $current_query;
$search_result[$i]['result'] = $current_result;
$i++;
}
执行上面的代码后,您将拥有具有清晰且易于使用的结构的数组 2-lvl 数组。您可以按照自己的方式使用它来比较数据或以您想要的方式显示数据。
生成的数组将具有这样的结构:
$search_result[0]['query'] = 'jupiter';
$search_result[0]['result'] = '...jupiter resul';
$search_result[1]['query'] = 'venus';
$search_result[1]['result'] = '...venus result';
您必须创建嵌套数组才能解决问题,我会举一个小例子怎么做?
$searchquery[1] ="jupiter";
$searchquery[2] ="venus";
for($i=1;$i<=count($searchquery);$i++){
$temp=find($searchquery[$i]);
$results[$i]=$temp[t];
}
$k=$i;
for($z=0;$z<=$i;$z++){
$total='';
for($p=0;$p<=$k;$p++){
$total=$total+$results[$p][$z];
}
echo $total;
echo "'n";
}
function find($word){
return array('t' => array('1', '2', '4', '7'));
}
答案是这样的:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 7
)
[2] => Array
(
[0] => 1
[1] => 2
[2] => 4
[3] => 7
)
)
2
4
8
14
此解决方案将添加所有n个查询的第一个结果,第二个结果等。