[英]How can i get the updated cart session array and display it in another php file AJAX
[英]How can I write an updated array into a PHP file
我正在用PHP構建一些東西,該東西從一個PHP文件讀取,如下所示:
<?php
return [
'name' => 'John Smith'
];
在我的代碼中,我正在獲取文件的內容並正在讀取它。 但這是我的問題。 當我想向該陣列中添加項目時,可以但我不確定如何將其保存回該陣列文件中。
我已經嘗試過這些方法($ array是更新后的數組)
file_put_contents('file.php', var_export($array, true));
file_put_contents('file.php', print_r($array, true));
使用var_export()
將數組放回文件中,如下所示:
file_put_contents('file.php', '<?php return ' . var_export($array, true) . ';');
如我所見,@ misorude已經建議您需要將var_export
參數2設置為true,如下所示
$ids = ['t1'=>10, 't2'=> 12, 't3' => 5];
$str = '<?php ' . PHP_EOL
. 'return '
. var_export($ids, 1) . ';' ;
echo $str;
結果:
<?php
return array (
't1' => 10,
't2' => 12,
't3' => 5,
);
所以只需將$ str寫入文件即可
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.