[英]How to create this multidimensional array in PHP
I want to fill an array in a while loop. 我想在while循环中填充数组。
I want to display this array like this : 我想这样显示此数组:
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 ........
Here's my current code in my while loop 这是我的while循环中的当前代码
$array_cat[] = array(
array(
'category' => $cat,
array(
'company' => array(
'name' => $name,
'city' => $city,
'CEO' => $ceo
)
)
)
);
My code WHen I display it 我的代码显示了
foreach ($array_cat as $item) {
foreach ($array_cat['category'] as $company_display) {
echo $company_display['company']['name'][];
}
}
Thanks for the help ;) 谢谢您的帮助 ;)
try this: 尝试这个:
$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>";
}
}
Problem is in your inner foreach loop 问题出在你的内在foreach循环中
There is a problem in inner foreach loop and echo line. 内部foreach循环和回显线存在问题。
Replace $array_cat by $item and in echo line there is an error: Cannot use [ ] for reading 由$项替换$ array_cat和回声线有错误:无法使用[]阅读
By following way, you can achieve your goal. 通过以下方式,您可以实现自己的目标。
foreach ($array_cat as $item) {
foreach ($item as $company_display) {
echo $company_display['category']['company']['name'];
}
}
How to create this multidimensional array in PHP
如何在PHP中创建此多维数组
If I would design an array for that, i would do something like this: 如果我要为此设计一个数组,我会做这样的事情:
$array = array(
//Category 1, nameless i assume?
array(
//Companies
"Company A" => array(
//Company properties
"name" => "My A company",
"city" => "Some city that starts with an A",
"CEO" => "Some CEO that starts with an A"
),
"Company B" => array(
//Company properties
"name" => "My B company",
"city" => "Some city that starts with an B",
"CEO" => "Some CEO that starts with an B"
),
),
//Category two, nameless i assume
array(
//Companies
"Company C" => array(
//Company properties
"name" => "My C company",
"city" => "Some city that starts with an C",
"CEO" => "Some CEO that starts with an C"
),
"Company D" => array(
//Company properties
"name" => "My D company",
"city" => "Some city that starts with an D",
"CEO" => "Some CEO that starts with an D"
),
),
);
Then, if i wanted to get some data out of it, i could do something like this: 然后,如果我想从中获取一些数据,我可以这样做:
$companyAcity = $array[0]['Company A']['city'];
echo $companyAcity;
If I wanted to loop in the array, i could so something like this: 如果我想在数组中循环,我可以这样做:
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'];
}
}
I want to fill an array in a while loop.
我想在while循环中填充数组。
If you want to add data to the array while looping in it, you can do something like this: 如果要在循环中向数组添加数据,可以执行以下操作:
for($categoryID = 0; categoryID < count($array); $i++) {
$array[$categoryID][] = $categoryID +1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.