美文网首页
php小技巧,把json转为可用的php数组代码

php小技巧,把json转为可用的php数组代码

作者: Chting | 来源:发表于2023-12-14 16:52 被阅读0次
function exportArray($array = [], $indentation = "")
{
    $phpCode = "[";
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $phpCode .= "\n" . $indentation . "    " . var_export($key, true) . " => " . $this->exportArray($value, $indentation . "    ") . ",";
        } else {
            $phpCode .= "\n" . $indentation . "    " . var_export($key, true) . " => " . var_export($value, true) . ",";
        }
    }
    $phpCode .= "\n" . $indentation . "]";
    return $phpCode;
}

$json = '{"name": "John", "age": 30, "city": "New York"}';
$array = json_decode($json, true);
$phpCode = exportArray($array);
file_put_contents(__FILE__ . 'Code.php', $phpCode, FILE_APPEND);


相关文章

网友评论

      本文标题:php小技巧,把json转为可用的php数组代码

      本文链接:https://www.haomeiwen.com/subject/jocwgdtx.html