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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jayreed21
Forum Newbie
Posts: 8
Joined: Thu Jan 25, 2007 9:26 am

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

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Use foreach() to iterate over cartcontent. Use the "seller" element as an index into a new array.
jayreed21
Forum Newbie
Posts: 8
Joined: Thu Jan 25, 2007 9:26 am

Post 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 :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;
}
jayreed21
Forum Newbie
Posts: 8
Joined: Thu Jan 25, 2007 9:26 am

Post by jayreed21 »

Thanks for the quick response feyd! That makes a lot more sense now... You are a rock star!!
jayreed21
Forum Newbie
Posts: 8
Joined: Thu Jan 25, 2007 9:26 am

Post by jayreed21 »

What is the purpose of the following line of code:

Code: Select all

unset($item["seller"]);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

To unset the value.
jayreed21
Forum Newbie
Posts: 8
Joined: Thu Jan 25, 2007 9:26 am

Post by jayreed21 »

yea but why?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Because it doesn't need to be in the array anymore. The information is redundant at that point.
jayreed21
Forum Newbie
Posts: 8
Joined: Thu Jan 25, 2007 9:26 am

php arrays and smarty templates issue

Post 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?
Post Reply