Wolfpack'08
Wolfpack'08

Reputation: 4128

PHP: Multidimensional array, multidimensional keys?

$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?

Upvotes: 1

Views: 24700

Answers (6)

hoppa
hoppa

Reputation: 3041

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

Upvotes: 10

Nanne
Nanne

Reputation: 64419

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

Upvotes: 1

fromz
fromz

Reputation: 81

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

Upvotes: 1

jensgram
jensgram

Reputation: 31498

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.

Upvotes: 1

Purple Coder
Purple Coder

Reputation: 319

<?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.

Upvotes: 1

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

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

Upvotes: 1

Related Questions