简体   繁体   English

PHP 关联数组有些只有一个键

[英]PHP associative array some will only have a key

If I need an associative array, but I only need the key for most of the values.如果我需要一个关联数组,但我只需要大多数值的键。 Do I need to still need to set a value for the things I wont be using for example:我是否还需要为我不会使用的东西设置一个值,例如:

$car_info = array(
  'toyota' => array(
    'description' => 'toyota Description',
   ),
  'ford' => array(
   'description' => 'ford Description',
  ),
  'bmw' => array(
  ),
  'subaru' => array(
  )
)

Can I leave 'description' out or should I set it to an empty value?我可以省略“描述”还是应该将其设置为空值?

It's not clear what you intend to do with the array once it's assigned but in terms of correct PHP programming, eg no error reported, you can definitely leave it as it is.不清楚分配数组后您打算对它做什么,但就正确的 PHP 编程而言,例如没有报告错误,您绝对可以保持原样。 You could even assign an empty string or null to the keys you want, although I'm not sure what would be the point of it.您甚至可以为您想要的键分配一个空字符串或 null,尽管我不确定它的意义何在。 Something like:就像是:

$car_info = array(
    'toyota' => array(
        'description' => 'toyota Description',
    ),
    'ford' => array(
        'description' => 'ford Description',
    ),
    'bmw' => null, // or ''
    'subaru' => null, // or ''
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM