Page 1 of 1

need to split items in an array of an array according to...

Posted: Thu Jan 25, 2007 9:34 am
by jayreed21
Hello,

I am trying to take an array entitled cartcontent() which holds arrays of shopping cart items. For example:
cartcontent[0] = product id=>, seller=>, name=>, quanity=>
cartcontent[1]= " " " "

I need to split cartcontent into separate arrays which all hold products that contain the same seller value. I am having a really tough time trying to figure out an algorithm to accomplish this task.


Does anyone have any idea how to do this or even how to reword this question into something more comprehend able that can help me find my solution.

Thanks,


JR

Posted: Thu Jan 25, 2007 9:37 am
by feyd
Use foreach() to iterate over cartcontent. Use the "seller" element as an index into a new array.

Posted: Thu Jan 25, 2007 9:45 am
by jayreed21
How do I use seller element to index a new array? So I would do something like:

Code: Select all

foreach($cartcontent as &$value)
{
     foreach($value as $key=>$value)
           {
               if ($key=="seller")
                    {
                          
                     }

          }


}

I am having trouble understand how to even access the seller keys in an array of an array.  Nor am I understand how to compare all of the items[seller] Or even insert into an array.



I am really confused :(

Posted: Thu Jan 25, 2007 10:04 am
by feyd

Code: Select all

$sellers = array();
foreach( $cartcontent as $item )
{
  $seller = $item['seller'];
  unset($item['seller']);
  if (!array_key_exists($seller, $sellers))
  {
    $sellers[$seller] = array();
  }
  $sellers[$seller][] = $item;
}

Posted: Thu Jan 25, 2007 10:16 am
by jayreed21
Thanks for the quick response feyd! That makes a lot more sense now... You are a rock star!!

Posted: Thu Jan 25, 2007 2:45 pm
by jayreed21
What is the purpose of the following line of code:

Code: Select all

unset($item["seller"]);

Posted: Thu Jan 25, 2007 3:01 pm
by feyd
To unset the value.

Posted: Thu Jan 25, 2007 3:04 pm
by jayreed21
yea but why?

Posted: Thu Jan 25, 2007 3:05 pm
by feyd
Because it doesn't need to be in the array anymore. The information is redundant at that point.

php arrays and smarty templates issue

Posted: Tue Jan 30, 2007 2:52 pm
by jayreed21
I placed

Code: Select all

$sellers = array();
foreach( $cartcontent as $item )
{
  $seller = $item['seller'];
  unset($item['seller']);
  if (!array_key_exists($seller, $sellers))
  {
    $sellers[$seller] = array();
  }
  $sellers[$seller][] = $item;
} 
inside of a function called get_order(); If I wanted to return an array called $order, Would It be possible to return the $seller array which we created above like so:

Code: Select all

$order['seller'] = $sellers;
return $order
Assuming that I am correct I should be able to get to the sellers array by calling
$order["seller"]. Correct?


Would I think be able to get access to this array by doing something along the lines of
echo $order["sellers"][0][0];

I am using smarty templates with our shopping site and assigned the following:
$smarty->assign( "seller", $order['sellers']);


I then attempt to iterate through the loop on the smarty page like so:


Code: Select all

<h3>{$smarty.const.STRING_ORDER_SHIPPING}</h3>
{section loop=$seller name=i }
<p>Item: <b>{$seller[i].name}</b></p>
<p>Cost: <b>{$seller[i].cost}</b></p>
<p>Seller: <b>{$seller[i].seller}</b></p>
<p>Quantity: <b>{$seller[i].quantity}</b></p>
{/section}
The problem is I am getting this to loop twice but It is not showing the values pulled from the $seller array? Does anyone have any idea why they are not showing up?