我有入站退货数据 - > Php数组格式的http://php.net/manual/en/function.json-decode.php。文件数据是相同的类型。这只是前一周期的结果。
$result = api_query("mytrades", array("marketid" => $id));
如何将$ result数组与$ file数组进行比较,然后用$ result数据写入FILE? 换句话说,FILE及其包含的数据不断使用$ result
进行更新比较 - >覆盖 - >下次执行时重复。
我试过array_diff,但它不喜欢我的数据类型,我找不到解决方法。 注意:.db文件在第一个循环时为空,但在第一次写入时会填充。
带有Array to string转换错误的示例代码:
<?php
$id = 155;
require_once('phpPlay.php');
$result = api_query("mytrades", array("marketid" => $id));
$lines = file("myDB.db");
$arrayDiffresult = array_diff ( $result, $lines);
var_dump($result);
file_put_contents('myDB.db', print_r($result, true));
?>
答案 0 :(得分:0)
我认为,你正在寻找一些序列化,例如json_encoding。
$result = array(
'return' => array(
array(
"tradeid" =>"74038377",
"tradetype" =>"Sell",
"datetime" =>"2014-11-12 16:05:32",
"tradeprice" =>"0.00675000",
"quantity" =>"22.18670000",
"fee" =>"-0.00007488",
"total" =>"0.14976023",
"initiate_ordertype" =>"Buy",
"order_id" =>"197009493",
),
array(
"tradeid" =>"2",
"tradetype" =>"Sell",
"datetime" =>"2014-11-12 16:05:32",
"tradeprice" =>"0.00675000",
"quantity" =>"22.18670000",
"fee" =>"-0.00007488",
"total" =>"0.14976023",
"initiate_ordertype" =>"Buy",
"order_id" =>"2",
)
)
);
function getdiff($new, $old)
{
//implement right logical diff here
$diff = array();
$old_serialized = array();
foreach ($old as $item) {
$old_serialized[] = json_encode($item);
}
foreach ($new as $item) {
if (in_array(json_encode($item), $old_serialized)) {
continue;
}
$diff[] = $item;
}
return $diff;
}
$old = file_exists('1.db') ? json_decode(file_get_contents('1.db'), 1) : array();
$arrayDiffresult = getdiff($result['return'], $old);
file_put_contents('1.db', json_encode($result['return']));
print_r($arrayDiffresult);