I want to create a new array for each category each time I iterate through my Categories
array. My data comes from a larger array. I want to create a unique array with the name of the appropriate $Categories
array value each time I cycle through the outermost foreach. I may not have explained it very well so let me know if there is something I need to elaborate on. Here is a sample of the code (I have simplified it to save space but only omitted key/value pairs where not needed for this example. This is probably not the most efficient way of doing it as I force it to cycle through all the menu items each time it runs for each category, but I will focus on that issue later).
//$menuItems['menu_items']
) is the name of the master array from which I am getting all my information
$my_variable_array_name
is the array that I wanted to create with a variable so that I can create a new one with a different name each time it runs through the ($Categories as $Category
) foreach loop.
$Categories = array('0Cat', '1Cat', '2Cat', '3Cat', '4Cat', '5Cat');
foreach ($Categories as $Category)
{
foreach($menuItems['menu_items'] as $Curr_Item)
{
if($Curr_Item['category']==$Category)
{
$my_variable_array_name[$Category.'_Entry'][$Curr_Item['name']] =
array('name'=>$Curr_Item['name'],
'id'=>$Curr_Item['id'],
'amount'=>$Curr_Item['amount'],
'on_hold'=>$Curr_Item['on_hold'],
'category'=>$Curr_Item['category'],
'Measurement'=>$Curr_Item['Measurement'],);
}
}
}
So to sum up and explain it differently, I want to only have to write this chuck one time and each time it runs (for each category entry) it will make an new (different) array with a unique name (a derivative of the category value ie: "1Cat"
etc.) So I will end up with 6 different arrays holding the appropriate information.
I tried to use the $Category
as the name of the array but it erred out. The code works fine if I give the array a name but it writes it all the the one array, I want each category in a new array.
Your question sounds really confusing, but I think you are just looking for an iterator that you can use.
foreach ($categories as $i => $category) {
// $i starts at 0
}
Can you try this. I have declared a temporary array here which contains all that data which you are populating in your loop. After the loop completes, I'm using php's builtin function extract which opens up the array and all of the items in that array become a separate variable.
So you can later directly use them.
P.S try to use a category name which starts with an alphabet. Because $0Cat
will give you syntax errors.
$Categories = array('0Cat', '1Cat', '2Cat', '3Cat', '4Cat', '5Cat');
$arr = array();
foreach ($Categories as $Category)
{
foreach($menuItems['menu_items'] as $Curr_Item)
{
if($Curr_Item['category']==$Category)
{
$tmp = $my_variable_array_name[$Category.'_Entry'][$Curr_Item['name']];
$arr[$tmp] =
array('name'=>$Curr_Item['name'],
'id'=>$Curr_Item['id'],
'amount'=>$Curr_Item['amount'],
'on_hold'=>$Curr_Item['on_hold'],
'category'=>$Curr_Item['category'],
'Measurement'=>$Curr_Item['Measurement'],);
}
}
}
extract($arr);