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

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Wolfpack'08
  • 3,982
  • 11
  • 46
  • 78

6 Answers6

9
$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"
    )
  )
);
Community
  • 1
  • 1
hoppa
  • 3,011
  • 18
  • 21
1

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

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59
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.

Purple Coder
  • 319
  • 1
  • 13
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.

jensgram
  • 31,109
  • 6
  • 81
  • 98
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);
fromz
  • 71
  • 6
  • The second code snippet returns an error: Parse error: syntax error, unexpected T_VARIABLE in /code/cUKR0g on line 3 http://codepad.viper-7.com/cUKR0g – Wolfpack'08 Jun 24 '11 at 04:58
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";
Nanne
  • 64,065
  • 16
  • 119
  • 163
  • 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. – Wolfpack'08 Jun 22 '11 at 09:40
  • It's giving me those blanks when I do
     and 
    – Wolfpack'08 Jun 22 '11 at 09:40
  • 1
    Ah. Fixed your code -> use the `{}` button on the wysiwig bar thingy when typing code, its the easiest way :) – Nanne Jun 22 '11 at 09:55
  • Thanks. That's been bothering me so much. X// Glad to finally figure out how to get around it. – Wolfpack'08 Jun 22 '11 at 10:52