【问题标题】:PHP print array in other pages for use over multiple pages其他页面中的 PHP 打印数组以在多个页面上使用
【发布时间】:2015-09-13 09:28:05
【问题描述】:

我需要多个页面中的数组,所以我尝试制作一个打印数组的 php 文件。我用过:

file_put_contents('file.php',
    '<?php $order = ' . var_export($order1, true) . '; ?>');

我希望如果我将此文件包含在我需要数组的文件中,我可以将其用作$order,但我得到了一个错误。 我检查了file.php,一切看起来都井井有条。 有谁知道我做错了什么?

【问题讨论】:

    标签: php arrays include file-put-contents


    【解决方案1】:

    您不应在后端创建.php 文件。它不安全。 我建议您为此使用 JSON:

          file_put_contents('file.json',json_encode($order1));
          $order = json_decode(file_get_content($url), true);
    

    为了安全起见,您应该在 .htaccess 中或通过其他可用方法从网络上隐藏这些文件。

    【讨论】:

    • array $order1是一个包含对象的多维数组,对象解码后有没有可能变成数组?因为我已经记不起$idA = $order1[$Q][0]-&gt;id;了。
    • $order = json_decode(file_get_content($url), false); 但一切都是对象(json_decode 中的最后一个参数为 false)
    • 你也可以使用“序列化”而不是json_decode:file_put_contents('file.json', serialize($order1)); $order = unserialize(file_get_content($url));
    猜你喜欢
    • 2012-02-08
    • 2015-11-02
    • 2023-04-11
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多