Corentin Branquet
Corentin Branquet

Reputation: 1576

How to create this multidimensional array in PHP

I want to fill an array in a while loop.

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

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

Upvotes: 0

Views: 84

Answers (3)

Bas
Bas

Reputation: 2210

How to create this multidimensional array in 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.

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;
}

Upvotes: 0

Raman Kant Vashisht
Raman Kant Vashisht

Reputation: 126

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

Upvotes: 2

Siddharth Jogia
Siddharth Jogia

Reputation: 76

There is a problem in inner foreach loop and echo line.

Replace $array_cat by $item and in echo line there is an error: Cannot use [ ] for reading

By following way, you can achieve your goal.

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

Upvotes: 0

Related Questions