0

我正在尝试合并代码的两个部分。为此,我必须将我多次调用的函数的回显数组列输出存储到一个或多个我可以执行计算的数组中。

这是我正在尝试做的事情:

<?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) 作为数组存储在当前工作表中。

我正在尝试将 find.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];

这部分有效!但是我在将 echo'd$searchresult输出合并到一个数组中时遇到了麻烦,我可以在该数组上执行上述 for 循环。在这个例子中,我通过在 php 表中键入“venusarrayt 和 jupiterarrayt”来手动完成。

我确信有某种方法可以将我多次调用的函数的回显数组列结果存储到数组中,但我还没有弄清楚如何。请帮忙。

4

3 回答 3

0

无论如何,进一步处理搜索数据的明确解决方案可能如下所示:

$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';
于 2012-05-03T13:10:02.670 回答
0

我希望这有帮助:

<?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];
  }
 }
于 2012-05-03T12:16:12.910 回答
0

你必须创建嵌套数组来解决这个问题我举个小例子怎么办?

$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 个查询的第一个结果,第二个结果,依此类推......

于 2012-05-03T13:04:50.350 回答