PHP-获取返回的json的本地数组

PHP-获取返回的json的本地数组,php,arrays,Php,Arrays,我有一个返回数组的json请求 $json = $api->get(100); 这将返回一个数组,我可以对其上的每个循环等执行此操作。但是,如果我没有internet连接,我想将我实例化的数组样本保存到一个变量中 基本上: $offline_array = array(......); 我可以用什么方式在浏览器中复制粘贴json数组,然后使用它来实例化数组 我试过: var_dump($x); print_r($x); 不断获取语法错误: $offline = Array

我有一个返回数组的
json
请求

$json = $api->get(100);
这将返回一个数组,我可以对其上的每个循环等执行此操作。但是,如果我没有internet连接,我想将我实例化的数组样本保存到一个变量中

基本上:

$offline_array = array(......);

我可以用什么方式在浏览器中复制粘贴json数组,然后使用它来实例化数组

我试过:

var_dump($x);
print_r($x);
不断获取语法错误:

    $offline = Array ( 
            [99] => Array ( 
                    [id] => 238142 
                    [price1] => 10100.00 
                    [price2] => 10107.00 
                    [created_at] => 2018-02-24 09:27:02 
                    [updated_at] => 2018-02-24 09:27:02 
            ) 
            [98] => Array ( 
                    [id] => 238143 
                    [price1] => 10074.00 
                    [price2] => 10107.00 
                    [created_at] => 2018-02-24 09:28:01 
                    [updated_at] => 2018-02-24 09:28:01 
                    ) 
            [97] => Array ( 
                    [id] => 238144 
                    [price1] => 10084.03 
                    [price2] => 10107.00 
                    [created_at] => 2018-02-24 09:29:02 
                    [updated_at] => 2018-02-24 09:29:02 
                    ) 
             . . . 
在使用
true
参数对API结果进行处理以获得数组后,可以使用获取变量的文字表示:

<?php
$json = '{"test": 123}';
$arr = json_decode($json, true);
echo var_export($arr);

作为替代方案,您可以将json存储在一个文件中,然后从该文件中读取它

例如:

如果允许使用,则可以读取文件,然后使用


使用
true
作为
json\u decode
返回关联数组的第二个参数。

您可以使用
var\u export()
进行此操作

-输出或返回变量的可解析字符串表示形式

示例代码,将
var\u export()
的输出保存到一个文件中供以后使用

<?php
$offline = [
        ['id' => 238142,
         'price1' => 10100.00,
         'price2' => 10107.00,
         'created_at' => '2018-02-24 09:27:02',
         'updated_at' => '2018-02-24 09:27:02',
        ],
        ['id' => 238143,
         'price1' => 10074.00,
         'price2' => 10107.00,
         'created_at' => '2018-02-24 09:28:01',
         'updated_at' => '2018-02-24 09:28:01',
         ]
];

$save = var_export($offline);

file_put_contents('offline.txt', $save);

$new = file_get_contents('offline.txt');
print_r($new);
<?php
$offline = [
        ['id' => 238142,
         'price1' => 10100.00,
         'price2' => 10107.00,
         'created_at' => '2018-02-24 09:27:02',
         'updated_at' => '2018-02-24 09:27:02',
        ],
        ['id' => 238143,
         'price1' => 10074.00,
         'price2' => 10107.00,
         'created_at' => '2018-02-24 09:28:01',
         'updated_at' => '2018-02-24 09:28:01',
         ]
];

$save = var_export($offline);

file_put_contents('offline.txt', $save);

$new = file_get_contents('offline.txt');
print_r($new);
array (
  0 =>
  array (
    'id' => 238142,
    'price1' => 10100.0,
    'price2' => 10107.0,
    'created_at' => '2018-02-24 09:27:02',
    'updated_at' => '2018-02-24 09:27:02',
  ),
  1 =>
  array (
    'id' => 238143,
    'price1' => 10074.0,
    'price2' => 10107.0,
    'created_at' => '2018-02-24 09:28:01',
    'updated_at' => '2018-02-24 09:28:01',
  ),
)