Alejandro Torres Alejandro Torres - 1 month ago 5
PHP Question

Arrays PHP, store values into keys and retrieve then

I´m stuck with this problem, i´m the function i´m creating receives two params, $codeList, $priceList both of then are needed to send a XML and for each code and price one more node must be added to the XML, this the code i´m using by now, i have problems

$feed = '
<?xml version="1.0" encoding="utf-8" ?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>XXXXXXX</MerchantIdentifier>
</Header>
<MessageType>Price</MessageType>';


I create two arrays to make and example:

$SKU = array('ASUSVNA1','esx','rer','ASUSasVNA1','itemsdasd','item 24');
$price = array(2,3,4,5,6,7);


then i do this :

$i = 0;
foreach ($price as $price) {

foreach ($SKU as $SKU) {
$i++;
$feed .='
<Message>
<MessageID>'.$i.'</MessageID>
<Price>
<SKU>'.$SKU.'</SKU>
<StandardPrice currency="USD">'.$price.'</StandardPrice>
</Price>
</Message>';
}



}


$feed .= '
</AmazonEnvelope>';


Works to create the XML but in the output this message is send : Warning: Invalid argument supplied for foreach().

Answer

try using different name for array and vars

  $i = 0;
  foreach ($price as $my_price) {

    foreach ($SKU as $my_SKU) {
      $i++;
      $feed .='
      <Message>
        <MessageID>'.$i.'</MessageID> 
        <Price>
          <SKU>'.$my_SKU.'</SKU> 
          <StandardPrice currency="USD">'.$my_price.'</StandardPrice> 
          </Price>
      </Message>';
    }
  }