0
$array = array();
$array = $dom->find('a')->each(function ($dom, $array) {
    $array[] = $dom->getText();
    echo $dom->getText(); // WORKING OK!
    return $array;
});

print_r($array); // RETURN EMPTY ARRAY

How can I add values to array outside function? In this example I can get these values and show them in function, but if I add them to array and next return array, then outside function I have empty array.

desef
  • 9
  • 2

1 Answers1

0
$links = array();

$dom->find('a')->each(function ($dom, $array) {
    callBack($dom->getText());
});

function callBack($item){
    global $links;  
    $links[] = $item;
}

print_r($links);
Cristofor
  • 2,077
  • 2
  • 15
  • 23
  • 1
    `global` is a terrible idea. [Dependency Injection](http://stackoverflow.com/documentation/php/194/variables/2496/global-variable-best-practices) is better – Machavity Oct 09 '16 at 21:25