Forum Moderators: coopster

Message Too Old, No Replies

Unique-ing a multidimensional array in $_SESSION

multidimensional arrays unique

         

chriscortese

2:31 am on Sep 30, 2005 (gmt 0)

10+ Year Member



I am storing a 3-dimensional array in $_SESSION. The important data has this form:

$_SESSION["items"][item_type][item_subtype].

I need to make sure there are no duplicates, where duplicate means the item type and subtype match.

I have looked at the comments on php.net for array_unique, and there are multiple posts that claim to provide code for unique-ing a multidimensional array, but I could not get any of them to work. I am wondering if it has something to do with the fact that I'm using $_SESSION.(?).

What I want to do (pseudocode) is:
array_unique($_SESSION["items"]).

Can anybody help?

Thanks,
Chris

coopster

8:32 pm on Sep 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, chriscortese.

The way you have it shown here you won't have duplicates. The type and subtype are both shown as indexes. Let me give you an example using $array just like you are using $_SESSION (and no, it doesn't matter that it is $_SESSION, it is still an array within the $_SESSION supgerglobal):

$array = array ( 
'items' => array(
'item_type1' => array(
'item1_subtype1' => 'value1-1',
'item1_subtype2' => 'value1-2',
'item1_subtype3' => 'value1-3'
),
'item_type2' => array(
'item2_subtype1' => 'value2-1',
'item2_subtype2' => 'value2-2',
'item2_subtype3' => 'value2-3'
)
)
);
print '<pre>';
print_r($array);
print '</pre>';
$array['items']['item_type1']['item1_subtype2'] = 'myValue';
print '<pre>';
print_r($array);
print '</pre>';

See what happens when you try to use the same index? The value under the existing index is overwritten. You won't have duplicate indexes.

chriscortese

2:31 pm on Oct 3, 2005 (gmt 0)

10+ Year Member



Coopster--thanks

I think I have a better handle on it now. Another thing that kind of threw me is that I was trying to treat one index as if it were numerical. I guess I did not show that in the previous post but their should have been another index 'item_num'. When I unset one of these values, the value becomes array(0), and my 'item_num' is no longer in sequential order.

At the moment I'm solving this (removal of an item) by unset-ing and then sliding each element down one. Is there a better way? I'm wondering if I should not have attempted to create a numerical index in the first place?

Thanks,
Chris

coopster

12:38 am on Oct 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Well, if you start off an array with an associative index and then don't use an associative index to set a value, PHP will automagically assign that value the next numerical index (zero if none has been used yet).

You may have to show us a small piece of how you are setting your array up as it isn't quite clear in the context of plain text. Can you offer a small snippet of code that describes how you are setting the array up?