2

I couldn't quite find the exact solution that I am looking for.

I am trying to create an array from a foreach loop that retains the key. Here is the code I have so far but it only keeps the last value in the array:

foreach($links as $link) {
  //runs scrape_amazon function for each of the links
  $ret = scrape_amazon($link);

  foreach($ret as $key => $value) {
    //echo $key; 
    //echo $value;
    $final_results[$key] = $value;
  }
}

Could anyone help with a solution to keep all the values and the keys?

Thanks in advance!

6
  • Are you asking for an array of keys? Commented Jun 18, 2011 at 19:19
  • Based on the way you're using the foreach loop it looks to me like your initial array would already have to be in the format of $ret[key] = value? Commented Jun 18, 2011 at 19:20
  • 3
    Your code should end up with $final_results being an exact copy of $ret (assuming it was empty to begin with). What did you expect to happen? Commented Jun 18, 2011 at 19:21
  • No I have a function that gathers data and puts it into $ret but each time it runs it overwrites the previous values. I'm trying to keep the key and value such as: [ASIN] => 123445 [Name] => Name [Retail] => 14.99. Commented Jun 18, 2011 at 19:29
  • Then the problem is not with the code you provided, but with when you assign values to $ret. The above simply creates an exact duplicate of $ret and places it in to $final_results, as Jon says. Commented Jun 18, 2011 at 19:33

4 Answers 4

8

Based on your most recent comment, this should solve your problem:

$ret = array();

foreach($links as $link) {
  $ret[] = scrape_amazon($link);
}

Each time scrape_amazon() is called, it'll add the array returned to $ret making it into an array of arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep. That does it to and creates a multidemensional. Thanks!
@Reg - Seeing that you're new on this site, I just thought I'd kindly remind you to accept the answer. Thanks! :)
1

If you need an array of just keys, you need to do this

$ret_keys = array_keys($ret);

Comments

1

why did you do that ? it's look like :

$final_results = $ret;

3 Comments

I know $final_results is returning the same as $ret. That's the problem. I was think of using array_push but couldn't get it to work to build the results of $ret each time into a single $final_results array.
the only solution you have is : $final_results[] = $ret; or $final_results[$ret['ASIN']] = $ret;
Thank you. I see now the problem is it has to be multidemensional because there are 3 keys. Thanks for your help.
-1
  // Create an empty array first      
  $final_results = array();
  foreach($links as $link) {
      //runs scrape_amazon function for each of the links
      $ret = scrape_amazon($link);
      $final_results[] = $ret; // DONE :)
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.