json_encode()

jsonを使って書き直し

<?php
//declare data
$data = &#91;
    'Taro'=>['age'=>30, 'hobby'=>['Guitar','Piano']],
    'Takeshi'=>['age'=>18, 'hobby'=>['Reading']],
    'Arisa'=>['age'=>16, 'hobby'=>['Walking','Tea']],
    'Sara'=>['age'=>22, 'hobby'=>['Sleeping']]
    ];

// file pass to preserve
    $file = "serialize-test.json";

//serialize
    $str = json_encode($data, JSON_PRENTY_PRINT);

//preserve
    file_put_contents($file, $str);

//recover data from a file
    $str2 = file_get_contents($file);
    $data2 = json_decode($str2, true);
// display Arisa hobby
    print_r($data2['Arisa']['hobby']);