如何在获取日期期间避免数组错误

时间:2015-02-28 10:44:21

标签: php arrays string

如何在获取日期期间避免数组错误

 <?php

     $str = ' a:1:{i:1;a:7:{i:1;a:3:{s:5:"month";s:1:"1";s:3:"day";s:1:"3";s:4:"year";s:4:"1954";}i:2;s:9:"FULL TIME";i:3;s:21:"COMPUTER APPLICATION ";i:4;s:17:"MADRAS UNIVERSITY";i:5;a:3:{s:5:"month";s:1:"1";s:3:"day";s:1:"1";s:4:"year";s:4:"1958";}i:6;s:6:"APRIL ";i:7;s:21:"Title of Dissertation";}}';

        $str_array = unserialize($str);  
        foreach($str_array as $values) {
            foreach($values as $value) {
                echo $value . '<br/>';
            }
            echo '<br/>';
        }
?>

输出会是这样的,如何避免数组错误:

Array
FULL TIME
COMPUTER APPLICATION
MADRAS UNIVERSITY
Array
APRIL
Title of Dissertation

但我的实际输出是这样的:

01/03/1954
FULL TIME
COMPUTER APPLICATION
MADRAS UNIVERSITY
01/01/1958
APRIL
Title of Dissertation

1 个答案:

答案 0 :(得分:0)

这应该适合你:

<?php

    $str = 'a:1:{i:1;a:7:{i:1;a:3:{s:5:"month";s:1:"1";s:3:"day";s:1:"3";s:4:"year";s:4:"1954";}i:2;s:9:"FULL TIME";i:3;s:21:"COMPUTER APPLICATION ";i:4;s:17:"MADRAS UNIVERSITY";i:5;a:3:{s:5:"month";s:1:"1";s:3:"day";s:1:"1";s:4:"year";s:4:"1958";}i:6;s:6:"APRIL ";i:7;s:21:"Title of Dissertation";}}';

    $str_array = unserialize($str); 

    foreach($str_array as $values) {
        foreach($values as $value) {
            if(is_array($value))
                echo implode("/", $value) . "<br />";
            else
                echo $value . "<br />";
        }
        echo '<br/>';
    }

?>

输出:

1/3/1954
FULL TIME
COMPUTER APPLICATION 
MADRAS UNIVERSITY
1/1/1958
APRIL 
Title of Dissertation