Eric
Eric

Reputation: 21

Foreach loop in function only returns last item in array

I am having trouble with my php code below. I am trying to have the function below to return the UPC and imageURL for each item. When I print_r the result after the loop I receive this.

Array
(
    [upc] => 043396066731
    [ImageURL] => http://ecx.images-amazon.com/images/I/51HKXNNT53L._SL200_.jpg
)
Array
(
    [upc] => 096009394097
    [ImageURL] => http://ecx.images-amazon.com/images/I/512NKNWC8EL._SL200_.jpg
)

However, when I use return result and then print_r I only receive the last response. Why is this and how can I fix my code to receive the values from both items? I have searched Google and other Stackoverflow questions and can find similar situations, but I am still struggling.

Array
(
    [upc] => 096009394097
    [ImageURL] => http://ecx.images-amazon.com/images/I/512NKNWC8EL._SL200_.jpg
)

Here is my function

 function invokeGetMatchingProductForId(MarketplaceWebServiceProducts_Interface $service, $request) 
      {
          // try {
                  $response = $service->getMatchingProductForId($request);

        $dom = new DOMDocument();
        $dom->loadXML($response->toXML());
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $parsed_xml = simplexml_import_dom($dom);
        //print_r($parsed_xml);

        $Result = array();

          foreach($parsed_xml->GetMatchingProductForIdResult as $item ) 
          {          
               $status = $item->attributes()->status;
              if (stristr($status, "Success") == true)
              {
                $Result['upc'] = (string)$item->attributes()->Id;
                $Result['ImageURL'] = str_replace('SL75','SL200',$item->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->SmallImage->URL);
              }  else {

                          $Result['upc'] = (string)$item->attributes()->Id;
                          $Result['ImageURL'] = "";
                      }
          }         
        print_r($Result);
}
// return $Result;
// }

// $amazonResult =invokeGetMatchingProductForId($service, $request);



// print_r($amazonResult);

Upvotes: 0

Views: 1405

Answers (2)

PravinS
PravinS

Reputation: 2584

according to your requirement you need to create two dimensional array

use foreach like this

foreach($parsed_xml->GetMatchingProductForIdResult as $item ) 
{          
     $status = $item->attributes()->status;
     if (stristr($status, "Success") == true)
     {
         $Result[]['upc'] = (string)$item->attributes()->Id;
         $Result[]['ImageURL'] = str_replace('SL75','SL200',$item->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->SmallImage->URL);
     }
     else 
     {
          $Result[]['upc'] = (string)$item->attributes()->Id;
          $Result[]['ImageURL'] = "";
     }
}         
print_r($Result);

Upvotes: 0

dlyaza
dlyaza

Reputation: 238

You need to assign second key for your array:

$Result['upc'][] = ;
$Result['ImageURL'][] = ;
                    ^-----

Currently you are resetting this $Result['upc'] every time you assign new value.

Upvotes: 2

Related Questions