Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 映射所需属性的对象签名_Php_Arrays - Fatal编程技术网

Php 映射所需属性的对象签名

Php 映射所需属性的对象签名,php,arrays,Php,Arrays,我正试图想出一种最好的方法来创建一种存储对象(尤其是产品)签名的方法。将有几种类型的产品,每种产品都有自己的属性集,因此需要不同的属性 将对产品执行不同类型的操作,即验证等 目前,我能想到的最好的方法是一个关联数组,它具有属性,不管它是否为必需的属性以及它的数据类型,但肯定有更好的方法来做到这一点。在这种情况下,我能想到的最好的方法是一个关联数组,存储为JSON 因为我不知道你的数据来自哪里,我只是假装一下 $item['price'] = 123; $item['otherinfo'] = '

我正试图想出一种最好的方法来创建一种存储对象(尤其是产品)签名的方法。将有几种类型的产品,每种产品都有自己的属性集,因此需要不同的属性

将对产品执行不同类型的操作,即验证等


目前,我能想到的最好的方法是一个关联数组,它具有属性,不管它是否为必需的属性以及它的数据类型,但肯定有更好的方法来做到这一点。

在这种情况下,我能想到的最好的方法是一个关联数组,存储为JSON

因为我不知道你的数据来自哪里,我只是假装一下

$item['price'] = 123;
$item['otherinfo'] = 'stuff';

$data[] = $item;

// repeat for all data. foreach loop over it if you can
然后,根据您希望存储数据的方式(每次重写、附加到文件中等。打开文件的不同方式的信息位于):

然后,当您要加载数据时:

$string = file_get_contents( 'storage.txt' );
$data = json_decode( $string );

希望这能让你有一个好的开始

我不完全确定你在找什么,但你能用一个真实的物体吗?(虽然一次输入的信息可能有点太多)我正在寻找一种存储产品签名的方法,这样我就可以轻松地迭代产品的属性,如价格、订单号、颜色等。我还需要知道插入新产品等需要哪些字段。数据是否存储在数据库中?不,不是。我需要创建一个结构或文件来存储数据。这是一个相当大的问题。手工存储数据并不有趣。你想过使用MySQL吗?
$string = file_get_contents( 'storage.txt' );
$data = json_decode( $string );