0

The code below works for a string value but not when I try to access the variable directly. The data being accessed is a table at http://webrates.truefx.com/rates/connect.html?f=html My code strips it of tags and put it in an array $row0 And puts it in a function. But I can't get it out. The function is simplified for this question. I intend to concatenate some of the variables inside the function once I find out what I'm doing wrong.

$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"

I have tried all the suggestions from here:

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

Suggestion appreciated please and more info available if required. Thank you very much for viewing.

1 Answers1

0

You need to get the innertext of $col in your loop. Like this:

$row0[$i][$j]= $col->innertext;

The next thing is:

myArray($row0);

This call will correctly return the parsed array; try echoing it and you'll see. But when you do this:

$arrayToEcho = myArray($arr);

...you're referencing to $arr which is a local variable (a parameter, actually) inside your function myArr. So what you probably meant was this:

$arrayToEcho = myArray($row0);

Hope this helps!

UPDATE

Look, I show you what happens when you call a function:

function-call

dkellner
  • 8,726
  • 2
  • 49
  • 47
  • Many thanks for your suggestion. I made that change but no difference. I can access all the indexes of the array inside the function correctly so therefore I don't think the problem is coding before the function. Likely the problem is in the function or after it.??? – Charles Haughey Oct 02 '15 at 20:28
  • Oh yes, you're using $arr outside the function. That one is local, belonging to "myArray" only. Try printing the results of your first call... I'm going to update my post. – dkellner Oct 02 '15 at 20:31
  • I do need to access $arr and not $row. I know I can get this. I will be concatenating inside the function and need to get it out. – Charles Haughey Oct 02 '15 at 21:51
  • For example I will concatenate $r =$arr[3][3].$arr[3][4]; And the function will be called 10 times. So to save on code I'm trying to devise a function. Values end up in a database afterwards. Thank you. – Charles Haughey Oct 02 '15 at 21:54
  • Yes - the way to "get it out" is by returning it. That is what you do at "return $r" - this step will put the value of $r back outside to whoever called the function. So at the very moment you hit the "return $r", the outer $row0 will get the value of the inner $r. Now the "value of $arr" is (by definition!) the value of $row0 because you call your function like "myArr($row0)". This puts the (outer) $row0 variable inside the function WHERE IT WILL BE NAMED $arr. It's like an alias thing. Now if this is not clear maybe you should check out "function calls" part of php. – dkellner Oct 02 '15 at 22:15
  • I've updated the post again so you see what really happens when you do a function call. – dkellner Oct 02 '15 at 22:23
  • Thank you again. I have made the changes. The source website is off until Monday morning so no data to test. I'll immediately let you know. – Charles Haughey Oct 03 '15 at 10:21
  • Finally got my head around it thanks to your kind perseverance. Hurray to you. Sorry I took so long. Best wishes and hope we'll meet again. Source website usually off at weekend up on today. Got it. – Charles Haughey Oct 03 '15 at 10:50