dpxyfa4718 2012-09-21 12:00
浏览 26
已采纳

如何在另一个页面中获取数组值[关闭]

page1.php

$destinationCity = $_POST['destination'];
$sr = new searchArray();
$final = $sr->searcharray($destinationCity);
echo "<pre>";
print_r($final);
echo $final->vc[0];
echo "</pre>";

Page2.php

class searchArray {

public function searcharray($arr) {
   $rst = array();
   $length = strlen($arr);
    if ($length == 2){
        $sql = "select code from ukhotels where postcode LIKE '$arr%'";
        $rsd = mysql_query($sql);
        while($rs = mysql_fetch_array($rsd)){
        $co = $rs['code'];
        $vc = &$rst['vc'];
        $vi = &$rst['vi'];

        $vc[] = substr($co, 0, 2);
        $vi[] = substr($co, 3, strlen($co));

    }

    }

In page1 i am calling the function with one argument. In the page2 i am keeping the results in an associate array.

When I am printing the array from page1 its showing me output as

Array
(
[vc] => Array
    (
        [0] => CB
        [1] => RD
        [2] => RT
        [3] => YX
    )

[vi] => Array
    (
        [0] => 01461 
        [1] => 95101 
        [2] => 30818 
        [3] => 77126 
    )

)

i wanted to use the array in page1 like this

for($i=0, $j=1;$i<=20;$i++)
{
$params["VendorLocation"]["VendorCode"] = "{$final->vc[$i]}";
$params["VendorLocation"]["VendorLocationID"] = "{$final->vi[$i]}";
$params["VendorLocation"]["Key"] = "{$j}";
$j++;
}    

but it is throwing me an error saying " Notice: Trying to get property of non-object "

Can any one please help me how to solve this issues.

Regards Hailey

  • 写回答

1条回答 默认 最新

  • doutan4831 2012-09-21 12:07
    关注

    $final is an array, access the value like this :

    for($i=0, $j=1; $i<=20; $i++) {
        $params["VendorLocation"]["VendorCode"] = '{' . $final['vc'][$i] . '}';
        $params["VendorLocation"]["VendorLocationID"] = '{' . $final['vi'][$i] . '}';
        $params["VendorLocation"]["Key"] = "{$j}";
        $j++;
    }    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀