Forum Moderators: coopster
$_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
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>';
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
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?