如何在PHP中创建这个多维数组

如何在PHP中创建这个多维数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我想在while循环中填充一个数组 我希望按如下方式显示此阵列: category 1 => Company A => 'name', 'city', 'CEO', Company B => 'name', 'city', 'CEO' category 2 = Company A => 'name', 'city', 'CEO', Company B => 'name', 'city', 'CEO' ec

我想在while循环中填充一个数组

我希望按如下方式显示此阵列:

category 1 => Company A => 'name', 'city', 'CEO',
              Company B => 'name', 'city', 'CEO'

category 2 =  Company A => 'name', 'city', 'CEO',
              Company B => 'name', 'city', 'CEO'

ect ........
这是我在while循环中的当前代码

$array_cat[] = array(
                 array(
                  'category' => $cat,
                      array(
                        'company' => array(

                            'name' => $name,
                            'city' => $city,
                            'CEO' => $ceo

                        )
                    )
                )
            );
当我显示它时,我的代码

foreach ($array_cat as $item) {
    foreach ($array_cat['category'] as $company_display) {
        echo $company_display['company']['name'][];
    }
}

谢谢你的帮助;)

内部foreach环路和回波线存在问题

$item替换$array\u cat,回音行中出现错误: 无法使用[]进行读取

通过以下方法,你可以实现你的目标

foreach ($array_cat as $item) {
    foreach ($item as $company_display) {
       echo $company_display['category']['company']['name'];
    }
}
试试这个:

$array1 = array('category1' => 
                array('Company A' => 
                        array('name', 'city', 'CEO')), 
                'category2' => 
                array('Company B' => 
                        array('name', 'city', 'CEO')));

foreach ($array1 as $key => $value) 
{
    foreach ($value as $key1 => $value1) 
    {
        echo "<pre>";
        print_r($value1);
        echo "</pre>";
    }
}
$array1=array('category1'=>
数组('公司A'=>
数组('name','city','CEO'),
'类别2'=>
数组('公司B'=>
数组('name','city','CEO');
foreach($array1作为$key=>$value)
{
foreach($key1=>$value1的值)
{
回声“;
印刷品(价值1美元);
回声“;
}
}
问题在于您的内部foreach循环

如何在PHP中创建这个多维数组

如果我要为此设计一个数组,我会这样做:

$companyAcity = $array[0]['Company  A']['city'];

echo $companyAcity;
for($categoryID = 0; categoryID < count($array); $i++) {
   //Prints data for each category it loops through.
   echo $array[$categoryID];

   //Loops through the companies contained in the current category where it's looping through
   foreach($array[$categoryID] as $companyName => $companyData) {
      echo $companyName;
      echo $companyData['name'];
   }
}
for($categoryID = 0; categoryID < count($array); $i++) {
   $array[$categoryID][] = $categoryID +1;
}
然后,如果我想从中获取一些数据,我可以这样做:

$companyAcity = $array[0]['Company  A']['city'];

echo $companyAcity;
for($categoryID = 0; categoryID < count($array); $i++) {
   //Prints data for each category it loops through.
   echo $array[$categoryID];

   //Loops through the companies contained in the current category where it's looping through
   foreach($array[$categoryID] as $companyName => $companyData) {
      echo $companyName;
      echo $companyData['name'];
   }
}
for($categoryID = 0; categoryID < count($array); $i++) {
   $array[$categoryID][] = $categoryID +1;
}
如果我想在数组中循环,我可以这样做:

$companyAcity = $array[0]['Company  A']['city'];

echo $companyAcity;
for($categoryID = 0; categoryID < count($array); $i++) {
   //Prints data for each category it loops through.
   echo $array[$categoryID];

   //Loops through the companies contained in the current category where it's looping through
   foreach($array[$categoryID] as $companyName => $companyData) {
      echo $companyName;
      echo $companyData['name'];
   }
}
for($categoryID = 0; categoryID < count($array); $i++) {
   $array[$categoryID][] = $categoryID +1;
}
($categoryID=0;categoryID{ //打印循环通过的每个类别的数据。 echo$array[$categoryID]; //循环浏览当前类别中包含的公司 foreach($array[$categoryID]作为$companyName=>$companyData){ echo$companyName; echo$companyData['name']; } } 我想在while循环中填充一个数组

如果要在数组中循环时向数组中添加数据,可以执行以下操作:

$companyAcity = $array[0]['Company  A']['city'];

echo $companyAcity;
for($categoryID = 0; categoryID < count($array); $i++) {
   //Prints data for each category it loops through.
   echo $array[$categoryID];

   //Loops through the companies contained in the current category where it's looping through
   foreach($array[$categoryID] as $companyName => $companyData) {
      echo $companyName;
      echo $companyData['name'];
   }
}
for($categoryID = 0; categoryID < count($array); $i++) {
   $array[$categoryID][] = $categoryID +1;
}
($categoryID=0;categoryID{ $array[$categoryID][]=$categoryID+1; }
问题是什么?问题出在哪里?我想显示它,但它不是我想要的方式。这是用于显示的代码?该代码正在创建数组