开发者

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 />";
e开发者_开发技巧cho "</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?


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


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


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


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜