0

hey guys i have the following code

$link_repository=array();

foreach ($links as $value) {
   $link_repository=$crawler->fetchLinks($value);
   print_r($link_repository);
   echo 'Count:'.count($link_repository);
   echo '<br><br>';
  }

for each link, fetchLinks function returns an array of elements. I want to add the array returned by the function for each link into one array. how can i go about it

thanks

Nyfer
  • 163
  • 2
  • 5
  • 15

4 Answers4

0

If I would have to guess:

$link_repository = array();

foreach ($links as $value) {
   $temp = $crawler->fetchLinks($value); //Add to array
   $link_repository = array_merge((array)$link_repository, (array)$temp);
   print_r($link_repository);
   echo 'Count:'.count($link_repository);
   echo '<br><br>';
  }
}
Kermit
  • 33,827
  • 13
  • 85
  • 121
0

try this:

$link_repository[] = $crawler->fetchLinks($value);
Emil Aspman
  • 996
  • 1
  • 17
  • 26
  • this gives me an array in array suppose i have $link1={1,2,3} and $link2={5,6,7} i want the final array to be $linkrepo={1,2,3,5,6,7} – Nyfer Jan 21 '13 at 04:02
0
$link_repository=array();

foreach ($links as $value) {
    array_push($link_repository,$crawler->fetchLinks($value));
    //$link_repository[]=$crawler->fetchLinks($value);
    //$link_repository=$crawler->fetchLinks($value); I wouldn't use this
    print_r($link_repository);
    echo 'Count:'.count($link_repository).'<br><br>';
}
return $link_repository;

I've supplied two methods of entering data into your array. I wasn't sure which would be best.

bashleigh
  • 8,813
  • 5
  • 29
  • 49
0

I think the other answers here are misunderstanding your question.

fetchLinks() returns an array as its result. If you were to simply add this result to the existing $link_repository variable, that variable would end up as a multidimensional array - i.e. an array of arrays. For example, if the first call to fetchLinks() returned array(1,2) and the second call returned array(3,4), this would give you a $link_repository variable that looks like array(array(1,2), array(3,4).

What I think you're actually asking for is a way to make it so that the results of all calls to fetchLinks() combine to form a one-dimensional array. In the above example, you would want the final result of $link_repository to be array(1,2,3,4).

If this is correct, then what you want to do is merge the arrays together with array_merge:

$link_repository=array();
foreach ($links as $value) {
   $link_repository= array_merge($link_repository, $crawler->fetchLinks($value));
}

I'm not sure about the performance tradeoffs, but you may want to defer the array_merge until the end. If you add each fetchLinks array as its own element in $link_repository, then you can merge them all at once by passing every element in the array to array_merge at once by using user_call_func_array().

It's worth noting that you may want to use the array union operator, +, to merge the arrays, depending on what sort of data fetchLinks returns. See here for discussion on how that operator works, and see here for a discussion on how it differs from array_merge.

Community
  • 1
  • 1
AgentConundrum
  • 20,288
  • 6
  • 64
  • 99