Basically, I'm performing very similar loops to build an array. The first two work very well and do exactly what I think they should. The last one seems to overwrite the values and I don't understand why.

My question is probably better explained by pointing at where I think the problem is towards the end of the code...

//get all posted values and create csv string of sizeids
$sids="";
$qtys="";
foreach($_POST as $k => $v)
{
$k=trim(mysql_real_escape_string($k));
$v=trim(mysql_real_escape_string($v));
$multik=explode("_", "$k");
if ($multik[0]=="sid")
	{
	if(strlen($sids)>0){$sids=$sids.",".$v;}else{$sids=$v;}
	}
if ($multik[0]=="qty")
	{
	if(strlen($qtys)>0){$qtys=$qtys.",".$v;}else{$qtys=$v;}
	}
}

//referrer must send the apikey as a 10 digit integer
if(!isset($apikey) || !is_numeric($apikey) || strlen($apikey)!=10){$apikey="1346798520";}
if(!isset($returnurl)){$returnurl="https://www.example.com/response.php";}


$sids=explode(",", $sids);
$qtys=explode(",", $qtys);

//build an array of items
////one of the functions will use the UPS class in 'inc/shipping/ups.php', so include the class now...
require_once("inc/shipping/ups.php");
$item=array();
$i=0;
foreach($sids as $sid){$i++;$item[$i]['sid']=$sid;}
//this builds an array with the structure I want
echo"BUILDING item array with \"sid\" <pre>";var_dump($item);echo"</pre>";
$i=0;
foreach($qtys as $qty){$i++;$item[$i]['numof']=$qty;}
//this adds another key/value set to the array like I think it should...
echo"BUILDING item array attaching \"numof\" <pre>";var_dump($item);echo"</pre>";
$i=0;
foreach($item as $item)
{
$i++;
$item[$i]['sid']=$item['sid'];
$item[$i]['numof']=$item['numof'];
$item[$i]['size']=getsize($item['sid']);
$item[$i]['product']=getproduct($item[$i]['size']['product_uid']);
$item[$i]['shipping']['err']=canweship();
if($item[$i]['shipping']['err']=="OK"){$item[$i]['shipping']['options']=getshipping($item[$i], $item[$i]['numof']);}else{$item[$i]['shipping']['options']="";}
//$item[$i]['price']=getprice($i['sid'], $i['numof'], $apikey);
}

#####HERE IS THE PROBLEM...#####
//when I get here, it appears all my values have overwritten with the results from the final loop.
echo"READING ITEMs <pre>";var_dump($item);echo"</pre>";

Here are the results from each var_dump()

BUILDING item array with "sid"

array(4) {
  [1]=>
  array(1) {
    ["sid"]=>
    string(2) "14"
  }
  [2]=>
  array(1) {
    ["sid"]=>
    string(2) "15"
  }
  [3]=>
  array(1) {
    ["sid"]=>
    string(2) "17"
  }
  [4]=>
  array(1) {
    ["sid"]=>
    string(2) "18"
  }
}

BUILDING item array attaching "numof"

array(4) {
  [1]=>
  array(2) {
    ["sid"]=>
    string(2) "14"
    ["numof"]=>
    string(1) "1"
  }
  [2]=>
  array(2) {
    ["sid"]=>
    string(2) "15"
    ["numof"]=>
    string(1) "2"
  }
  [3]=>
  array(2) {
    ["sid"]=>
    string(2) "17"
    ["numof"]=>
    string(1) "3"
  }
  [4]=>
  array(2) {
    ["sid"]=>
    string(2) "18"
    ["numof"]=>
    string(1) "4"
  }
}

READING ITEMs

array(3) {
  ["sid"]=>
  string(2) "18"
  ["numof"]=>
  string(1) "4"
  [4]=>
  array(5) {
    ["sid"]=>
    string(2) "18"
    ["numof"]=>
    string(1) "4"
    ["size"]=>
    array(30) {
      [0]=>
      string(2) "18"
      ["uid"]=>
      string(2) "18"
      [1]=>
      string(4) "1027"
      ["product_uid"]=>
      string(4) "1027"
      [2]=>
      string(3) "RPS"
      ["brand"]=>
      string(3) "RPS"
      [3]=>
      string(4) "0004"
      ["qty"]=>
      string(4) "0004"
      [4]=>
      string(1) "G"
      ["measurement"]=>
      string(1) "G"
      [5]=>
      string(3) "CON"
      ["type"]=>
      string(3) "CON"
      [6]=>
      string(4) "CASE"
      ["group"]=>
      string(4) "CASE"
      [7]=>
      string(3) "512"
      ["floz"]=>
      string(3) "512"
      [8]=>
      string(2) "30"
      ["ratio"]=>
      string(2) "30"
      [9]=>
      string(6) "158.36"
      ["buyprice"]=>
      string(6) "158.36"
      [10]=>
      string(6) "239.96"
      ["sellprice"]=>
      string(6) "239.96"
      [11]=>
      string(2) "45"
      ["weight"]=>
      string(2) "45"
      [12]=>
      string(2) "13"
      ["length_inches"]=>
      string(2) "13"
      [13]=>
      string(2) "13"
      ["width_inches"]=>
      string(2) "13"
      [14]=>
      string(2) "13"
      ["height_inches"]=>
      string(2) "13"
    }
    ["product"]=>
    array(18) {
      [0]=>
      string(4) "1027"
      ["uid"]=>
      string(4) "1027"
      [1]=>
      string(1) "1"
      ["priority"]=>
      string(1) "1"
      [2]=>
      string(1) "N"
      ["nsf"]=>
      string(1) "N"
      [3]=>
      string(1) "Y"
      ["dfe"]=>
      string(1) "Y"
      [4]=>
      string(1) "0"
      ["ratio"]=>
      string(1) "0"
      [5]=>
      string(13) "Example Product"
      ["product_name"]=>
      string(13) "Example Product"
      [6]=>
      string(17) "Example Product.png"
      ["product_image"]=>
      string(17) "Example Product.png"
      [7]=>
      string(54) "Our all purpose 100% eco-friendly product."
      ["product_tag"]=>
      string(54) "Our all purpose 100% eco-friendly product."
      [8]=>
      string(189) "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
      ["product_desc"]=>
      string(189) "TLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
    }
    ["shipping"]=>
    array(2) {
      ["err"]=>
      string(2) "OK"
      ["options"]=>
      string(309) "$ 837.10 UPS Next Day Air$ 818.27 UPS Next Day Air Saver$ N/A UPS 2nd Day Air (Early AM)$ 267.56 UPS 3 Day Select$ 106.96 UPS Ground"
    }
  }
}
    foreach($item as $item) 

    I'm surprised you don't get a E_NOTICE or even E_WARN level error message if you try to do something like this.

    You're using the same variable to hold each iterated value as the array itself. Thus, a copy of the array $item is passed to the foreach() function, and then $item is used to store the values of that array during each iteration of the loop. When the loop is over, $item will contain the last iteration's value because that is the last thing that was assigned to the variable called $item.

      Thanks bradgrafelman!

      As soon as you point it out, I realize that I know better than that. :o

      I guess I've been staring at it too long.

      It seems to be working the way I expect it to work now.

      Thanks again!

        Write a Reply...