0

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

3
  • 1
    What is the question? Where is the problem? Commented May 28, 2015 at 9:04
  • I want to display it but it's not in the way I want Commented May 28, 2015 at 9:05
  • Where is your code for displaying? That code is creating the array Commented May 28, 2015 at 9:07

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.