PHP存储来自我多次调用的函数的数组列输出

时间:2012-05-03 12:08:43

标签: php

我正在尝试合并代码的两个部分。为此,我必须存储我调用的函数的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)作为数组存储在当前工作表中。

我试图将find.php函数中的echos存储为数组 我可以在当前工作表中执行以下操作:

$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”手工完成。

我确信有一些方法可以存储一个函数的echo'd数组列结果,我多次调用一个函数,但是我 还没弄明白怎么样。请帮忙。

3 个答案:

答案 0 :(得分: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];
  }
 }

答案 1 :(得分: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个查询的第一个结果,第二个结果等等.......

答案 2 :(得分: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';