0

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);
1
  • please make an array of array to the output Commented Mar 16, 2014 at 6:43

2 Answers 2

2

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.

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

Comments

0

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);

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.