Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 从函数外部的多维数组中回显变量_Function_Multidimensional Array_Echo - Fatal编程技术网

Function 从函数外部的多维数组中回显变量

Function 从函数外部的多维数组中回显变量,function,multidimensional-array,echo,Function,Multidimensional Array,Echo,下面的代码适用于字符串值,但在我尝试直接访问变量时不起作用。 正在访问的数据是位于的表http://webrates.truefx.com/rates/connect.html?f=html 我的代码去掉了它的标签,并将其放入一个数组$row0中 并将其放入函数中。但是我不能把它弄出来。针对这个问题简化了函数。一旦我发现我做错了什么,我打算连接函数中的一些变量 $row0 = array(); include "scrape/simple_html_dom.php"; $url = "http

下面的代码适用于字符串值,但在我尝试直接访问变量时不起作用。 正在访问的数据是位于的表http://webrates.truefx.com/rates/connect.html?f=html 我的代码去掉了它的标签,并将其放入一个数组$row0中 并将其放入函数中。但是我不能把它弄出来。针对这个问题简化了函数。一旦我发现我做错了什么,我打算连接函数中的一些变量

$row0 = array(); 
include "scrape/simple_html_dom.php";
$url = "http://webrates.truefx.com/rates/connect.html?f=html";
$html = new simple_html_dom();
$html->load_file($url);
foreach ($html->find('tr') as $i => $row) {
    foreach ($row->find('td') as $j => $col) {
        $row0[$i][$j]= strip_tags($col);
    }
}

myArray($row0); //table stripped of tags

function myArray($arr) {
    $a = 'hello';         //$arr[0][0]; HELLO will come out but not the variable
    $b = $arr[1][0];
    $r[0] = $a;
    $r[1] = $b;
    //echo $r[1]; If the //'s are removed one can see the proper value here but not outside the function.
    return $r;
}

$arrayToEcho = myArray($arr);

echo $arrayToEcho[0]; // will echo "first"
我尝试了这里的所有建议:

http://stackoverflow.com/questions/3451906/multiple-returns-from-function
http://stackoverflow.com/questions/5692568/php-function-return-array

感谢您的建议,如有需要,请提供更多信息。非常感谢您的查看。

您需要在循环中获取$col的内部文本。像这样:

$row0[$i][$j]= $col->innertext;
下一件事是:

myArray($row0);
此调用将正确返回已解析的数组;试着重复一下,你就会明白。但当你这样做的时候:

$arrayToEcho = myArray($arr);
…您正在引用$arr,它是一个局部变量,是一个参数,实际上在函数myArr中。所以你的意思可能是:

$arrayToEcho = myArray($row0);
希望这有帮助

更新

听着,我告诉你调用函数时会发生什么:


非常感谢你的建议。我做了那个改变,但没什么不同。我可以正确访问函数中数组的所有索引,因此我认为问题不在于在函数之前编码。问题可能在函数中或之后。??哦,是的,您在函数之外使用$arr。那个是本地的,只属于myArray。尝试打印第一次通话的结果。。。我要更新我的帖子。我需要访问$arr而不是$row。我知道我能得到这个。我将在函数内部连接并需要将其取出。例如,我将连接$r=$arr[3][3]。$arr[3][4];该函数将被调用10次。所以为了节省代码,我试图设计一个函数。之后,值会在数据库中结束。谢谢你。是的,把它拿出来的方法是把它退回。这就是您在返回$r时所做的操作-此步骤将把$r的值放回调用该函数的人的外部。因此,当您点击返回值$r时,外部$row0将获得内部$r的值。现在$arr的值是定义的!$row0的值,因为您像myArr$row0一样调用函数。这将外部$row0变量放入函数中,它将被命名为$arr。这就像别名一样。现在,如果这还不清楚,也许您应该检查php的函数调用部分。