简体   繁体   English

如何在foreach循环中使用变量创建多个数组

[英]How to create multiple arrays using a variable in a foreach loop

I want to create a new array for each category each time I iterate through my Categories array. 我想在每次遍历Categories数组时为每个类别创建一个新数组。 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. 我想在每次循环通过最外面的foreach时使用适当的$Categories数组值的名称创建一个唯一的数组。 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 // $menuItems['menu_items'] )是从中获取所有信息的主数组的名称

$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. $my_variable_array_name是我要使用变量创建的数组,以便每次运行( $Categories as $Category )foreach循环时,都可以使用不同的名称创建一个新数组。

$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. 因此,以不同的方式进行总结和解释,我只需要编写一次该卡盘,并且每次运行(对于每个类别条目),它将创建一个具有唯一名称(类别的派生类)的新(不同)数组值,例如: "1Cat"等。)最后我将得到6个包含适当信息的不同数组。

I tried to use the $Category as the name of the array but it erred out. 我试图使用$Category作为数组的名称,但是它出错了。 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. 循环完成后,我正在使用php的内置函数摘录,该摘录打开了数组,该数组中的所有项目都变成一个单独的变量。

So you can later directly use them. 因此,您以后可以直接使用它们。

PS try to use a category name which starts with an alphabet. PS尝试使用以字母开头的类别名称。 Because $0Cat will give you syntax errors. 因为$0Cat将给您语法错误。

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM