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

显然,我在这里要做的是将索引分配给 $section 集,而我在尝试这样做时遇到了错误。有没有其他方法可以做到这一点,或者在 PHP 中是不可能的?

4

6 回答 6

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

例如,像上面这样的东西。另一种选择是添加另一个维度:

$products = array(
  'paper' => array(
    'meta' => array(
        'title' => "Paper Section"
    ),
    'data' => array(
        'copier' => "Copier and Multipurpose",
        'inkjet' => "Inkjet Printer"
    )
  )
);
于 2011-06-22T06:53:55.923 回答
1

您想要做的是数组中的另一个维度。这就是您问题的解决方案。

于 2011-06-22T06:51:46.373 回答
1
<?php
$products = array(
    'paper' => array(
    // --------^^^^^
        'Paper Section' => array(
            'copier' => 'Copier and Multipurpose',
            'inkjet' => 'Inkjet Printer',
        ),
    )
);
var_dump($products);

PS:更好地格式化(和缩进)代码会更容易。

于 2011-06-22T06:53:07.507 回答
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"
    )
  )
);

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

你可以在 Ideone看到一个演示

于 2011-06-22T06:57:26.197 回答
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"
          )
      )
);

正如其他人所提到的,您需要将其包装在另一个关联数组中。

但是,我感觉您正在尝试一次将最深的关联数组分配给两个键。如果是这种情况,您不能在 PHP 的数组声明中执行此操作,您必须手动执行一些操作,例如:

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

var_dump($products);
于 2011-06-22T07:01:20.160 回答
1

好吧,对我来说,您要尝试做的事情远非显而易见,但是您的语法错误:数组的构建方式类似于'key'=>'value',因为它是键/值对您有:

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

key->value->value. 那是行不通的。

还:

echo "
";

可能:

echo "\n";
于 2011-06-22T07:01:26.603 回答