我有一个像这个例子的对象数组:
[
{"idroom":"3","idoption":"1","included":"0"},
{"idroom":"3","idoption":"2","included":"0"},
{"idroom":"3","idoption":"6","included":"1"},
{"idroom":"3","idoption":"7","included":"1"},
{"idroom":"3","idoption":"16","included":"0"},
{"idroom":"3","idoption":"17","included":"0"}
]
但如果我做了
foreach ($array_of_objects as $single) {
...
fwrite( $single->idroom)
}
我得到一个空文件......为什么?
修改
为了获得那个对象数组,我从javascript
开始,如下所示:
var dataAssociates = {};
var associatesRecords = [];
dataAssociates = { "idroom": dataViewRooms.getItem(gridRooms.getSelectedRows()).roomId,
"idoption": $(this).find('input[name="option_name"]').attr('id'),
"included": (
$(this).find('input[name="enabled_default"]').prop('checked')
? '1' : '0')
};
associatesRecords.push(dataAssociates);
...
$.ajax({.... , 'element' : JSON.stringify(associatesRecords) ,... });
答案 0 :(得分:0)
$obj = new stdClass();
$obj->idroom = "3";
$obj->idoption = "1";
$obj->included = "0";
$array_of_objects[] = $obj;
$obj->idoption = "2";
$obj->included = "0";
$array_of_objects[] = $obj;
$obj->idoption = "6";
$obj->included = "1";
$array_of_objects[] = $obj;
$obj->idoption = "7";
$obj->included = "1";
$array_of_objects[] = $obj;
$obj->idoption = "16";
$obj->included = "0";
$array_of_objects[] = $obj;
$obj->idoption = "17";
$obj->included = "0";
$array_of_objects[] = $obj;
$filename = 'test.txt';
$handle = fopen($filename, 'w');
if (!$handle) {
echo "Cannot open file ($filename)";
exit;
}
foreach ($array_of_objects as $single) {
fwrite($handle, $single->idroom);
}
fclose($handle);
答案 1 :(得分:0)
没关系,我现在想出了自己......我只是想做:
$data = json_decode(stripslashes($_REQUEST['element']));
foreach ($data as $single) {
$sql = "INSERT INTO .... VALUES ( $single->idroom, $single->idoption, $single->included )";
...
}
所以它有效! 无论如何,感谢大家!
干杯! 路易