1
$products = array(
  'paper' => "Paper Section" => array
  (
    'copier' => "Copier and Multipurpose",
    'inkjet' => "Inkjet Printer",
  ),
  'pens' => "Pen Section" => array
  (
    'ball' => "Ballpoint Pens",
    'hilite' => "Highlighters"
  ),
  'misc' => "Miscellaneous Section" => array
  (
    'tape' => "Sticky Tape",
    'glue' => "Adhesive"
  )
);

echo "<pre>";
foreach ($products as $section => $items)
  foreach ($items as $key => $value)
    echo "$section:\t$key\t($value)<br />";
echo "</pre>";

Obviously what I'm trying to do here is assign indexes to the $section set, and I'm getting errors for trying to do that. Is there another way to do it, or is it just not possible in PHP?

3
  • 3
    Oh yeah, this question is the bomb. 2,500 views and -1 rating. hahaha Commented May 3, 2015 at 10:11
  • 23,000 views and only 3 upvotes means that this "how to declare a multidimensional array" question is not very helpful to LOOOOOADS of readers. Commented Nov 11, 2023 at 11:09
  • 1
    @mickmackusa I still use php, but not very often. I think people tend to use something like jquery, some sql variant, or xquery to store arrays like this, but I had some usage cases where I needed to have arrays solely in php (single-file-contained scripts with settings that could be modified from within the script). People rarely create settings files within binaries and even less often have self-modifying code so it isn't easy to find examples. This info can probably be found in a manual and people might be looking for other info. The community isn't very loving, though so, I expect few +1's Commented Dec 27, 2023 at 6:10

6 Answers 6

10
$products = array(
  'paper' => array(
    'title' => "Paper Section",
    'copier' => "Copier and Multipurpose",
    'inkjet' => "Inkjet Printer"
  )
);   

Something like the above, for instance. Another option is adding another dimension:

$products = array(
  'paper' => array(
    'meta' => array(
        'title' => "Paper Section"
    ),
    'data' => array(
        'copier' => "Copier and Multipurpose",
        'inkjet' => "Inkjet Printer"
    )
  )
);
Sign up to request clarification or add additional context in comments.

Comments

1

What you want to do is another dimension in your array. And that is the solution to your problem.

Comments

1
<?php
$products = array(
    'paper' => array(
    // --------^^^^^
        'Paper Section' => array(
            'copier' => 'Copier and Multipurpose',
            'inkjet' => 'Inkjet Printer',
        ),
    )
);
var_dump($products);

PS: It is easier when you format (and indent) your code better.

1 Comment

I like the way I indented it better because it's easier to read.
1

Not really understanding what you're trying to achieve, I'll still give it a shot. Below, I have restructured the data:

$products = array(
  'paper' => array(
    'Paper Section',
    array (
      'copier' => "Copier and Multipurpose",
      'inkjet' => "Inkjet Printer"
    )
  ),
  'pens' => array(
    'Pen Section',
    array (
      'ball' => "Ballpoint Pens",
      'hilite' => "Highlighters"
    )
  ),
  'misc' => array(
    'Miscellaneous Section',
    array (
      'tape' => "Sticky Tape",
      'glue' => "Adhesive"
    )
  )
);

foreach ($products as $sectionKey => $line) {
  foreach ($line[1] as $key => $value) {
    echo $sectionKey . ":\t" . $line[0] . ":\t$key\t($value)\n";
  }
}

You can see a demo at Ideone.

Comments

1
    $products = array(
      'paper' => array(
        "Paper Section" => array
          (
            'copier' => "Copier and Multipurpose",
            'inkjet' => "Inkjet Printer",
          )
       ),
      'pens' => array(
        "Pen Section" => array
        (
            'ball' => "Ballpoint Pens",
            'hilite' => "Highlighters"
          ),
      ),
      'misc' => array(
        "Miscellaneous Section" => array
        (
            'tape' => "Sticky Tape",
            'glue' => "Adhesive"
          )
      )
);

As the other guys have mentioned, you need to put wrap it in another associative array.

However, I get the feeling you're trying to assign the deepest associative array to two keys at once. If that is the case, you can't do it in the array declaration in PHP, you'll have to do something a bit more manually, like:

$products = array();
$products['Misc'] = $products['Miscellaneous Section'] = array(
      'tape' => "Sticky Tape",
      'glue' => "Adhesive"
);

var_dump($products);

1 Comment

The second code snippet returns an error: Parse error: syntax error, unexpected T_VARIABLE in /code/cUKR0g on line 3 codepad.viper-7.com/cUKR0g
1

Well, its far from obvious to me what you are trying to do, but your syntax is wrong: An array is build like 'key'=>'value', because it's a key/value pair You have:

'paper' => "Paper Section" => array()

key->value->value. That's not going to work.

also:

echo "
";

Might be:

echo "\n";

4 Comments

I guess that's because arrays are stored in the same namespace as strings or something? I was actually thinking value->key->value or key->value->subvalue.... Of course, I'm still learning. Anyway, thank you for the information.
It's giving me those blanks when I do <pre> and <br>
Ah. Fixed your code -> use the {} button on the wysiwig bar thingy when typing code, its the easiest way :)
Thanks. That's been bothering me so much. X// Glad to finally figure out how to get around it.

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.