Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

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.

share|improve this question
2  
An array of 6 arrays is as good as 6 arrays, isn't it? :) –  Ja͢ck Oct 15 '14 at 4:46
    
I suppose it is. Except, can I sort the nested array only and not the entire outermost array? also, I will look over my project again and see if I can just use the master array differently (no point in making an array of 6 arrays since its already done in the master, different structure than I made in mine but all the info is the same). I still want to know how to make a new, different array from a variable array name in my foreach loop. –  user3762169 Oct 16 '14 at 14:40
    
Sure, an array element can be used just like a regular variable. –  Ja͢ck Oct 16 '14 at 14:41
    
While it is possibly to do this using a variable variable (with the $$ syntax), I would suggest not doing it and using an array of arrays instead, as Jack suggests. –  Erik Oct 16 '14 at 14:51

2 Answers 2

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
} 
share|improve this answer
    
Not sure I need the iterator. The foreach iterates for me. unless I can use it to change the $my_variable_array_name. Do I need to declare my value in my Categories array as an array object? I thought php did all that. –  user3762169 Oct 16 '14 at 14:48

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);
share|improve this answer
    
I used your code and I see some ways to implement it in other areas but im not sure it is the best thing here (but it may just be the perfect thing, need to get a better grasp on it). First, I was not able to access the keys as variables. I tried to echo $name; and echo $arr => $name; and the first did nothing and the second errored out. –  user3762169 Oct 17 '14 at 3:07
    
Also, when I print_r $arr I only get one item in the last category so i guess it is an append issue (can probably figure that one out). The main reason I wanted to create separate arrays for each category is so I could sort them by price(I didn't include that key/val pair only because there are actually allot of items and what they are is not really relevant, we could just sort by id for the purpose of this post. btw, I use different category names in the app, my client just doesn't want them public. Thank you for any insight. –  user3762169 Oct 17 '14 at 3:07
    
well, the reason I used this $arr approach is because if you go for the $$ approach, which creates a variable with the name of the value, it won't work and there would be errors, as there would be scope issue because that variable would have been created and initialized inside the if condition of the forreach loop. I was assuming that $my_variable_array_name[$Category.'_Entry'][$Curr_Item['name']] would be something unique. Isn't it? –  Basit Nizami Oct 17 '14 at 5:31
    
The $my_variable_array_name will be unique because I want it to come from the $Catagories array. I would like to create a new separate array with the name of the current category each time the foreach moves to the next category name. Ultimately I want to sort the categories by price so when I output them to my menu page they are in desc order by price. Also, each category has a different section on the outputted page so I thought it may be easier to be able to work with them separately. –  user3762169 Oct 17 '14 at 5:45
    
I am also wondering how do I use the the variables created by the extract? I tried echoing the $name variable but wasn't able to do it. –  user3762169 Oct 17 '14 at 5:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.