如何为不同的密钥存储多个数据

时间:2016-09-13 10:42:52

标签: php arrays multidimensional-array associative-array

我有与每个不同键相关联的大数据。我打算在php中以这种格式存储数据

$item = array(
(
        "key" : key-value;
        "features" : array-of-features;
        "small_images" : array-of-small_images;
    ),

    (
        "key" : key-value;
        "features" : array-of-features;
        "small_images" : array-of-small_images;
    ),

    (
        "key" : key-value;
        "features" : array-of-features;
        "small_images" : array-of-small_images;
    )
);

任何想法如何创建如上所述的数据结构以及如何在PHP中访问其值。如果还有其他任何方式,那么请提及。

编辑:请注意,此处的数据是数组中的数据是array-of-features,array-of-small_images是一个关联数组,其中包含与main main相同的键,value作为其值。< / p>

1 个答案:

答案 0 :(得分:1)

如果密钥是唯一的,则关联数组是理想的解决方案:

$items['key1']['features'] = array-of-features;
$items['key1']['small_images'] = array-of-small_images;

$items['key2']['features'] = array-of-features;
$items['key2']['small_images'] = array-of-small_images;

...

然后,您可以使用foreach循环项目:

foreach($items as $key => $item) {
  echo "\n<pre>$key => " . print_r($item, 1) . "</pre>";
}