2011-04-28 84 views
0

的結構,我有以下結構:遍歷PHP

Array 
(
    [signupbasic] => Array 
     (
      [0] => Array 
       (
        [name] => Enter your first name or username 
        [email] => Enter your email address 
        [password] => Enter your password 
        [phone] => Enter your mobile number 
        [message1] => A text will be send to your phone - reply to validate 
        [gender] => Array 
         (
          [label] => Gender 
          [values] => Array 
           (
            [0] => Array 
             (
              [Female] => 1 
             ) 

            [1] => Array 
             (
              [Male] => 2 
             ) 

           ) 

         ) 

        [birthday] => Array 
         (
          [label] => Date of birth 
          [values] => date 
         ) 

        [message2] => By clicking on "Continue" below you agree to Terms of Service and Privacy Policy 
       ) 

     ) 

) 

正如你可以看到性別&生日是不同大小的陣列。由於性別和生日的大小不同,我不知道如何穿過上述結構。

任何機構可以幫助我實現這一目標嗎?

+0

你用什麼代碼來解碼'JSON'?因爲自從5.2版本以來PHP有一個名爲'json_decode'的內建函數。看看:http://php.net/manual/en/function.json-decode.php – Kevin 2011-04-28 06:52:01

+0

嗨凱文。我有使用json_decode.My問題是「性別」和「生日」是不同維度的數組,所以我是試圖找出如何解析它 – user469999 2011-04-28 06:55:59

+0

我不太清楚我明白你在說什麼。什麼**是你的問題?爲什麼你不能遍歷對象? – Kevin 2011-04-28 06:58:03

回答

0

對於PHP瞭解JSON,它需要專門的格式

{ 
    "property1": "value1", 
    "property2": "value2", 
    "property3": "value3", 
    "property4": "value4" 
} 

看到php.net json_decode()功能

1

更好explenation你爲什麼不只是單獨訪問這些價值觀?就像你有可能完成name,emailpassword

$o是JSON,然後做沿着此線的東西:

$data = &$o['signupbasic'][0]; 

$genderObj = $data['gender']; 
$birthdayObj = $data['birthday']; 

我不太清楚你要完成,你可以張貼一些PHP代碼是什麼?

1

我不知道我是否明白你的意思,但會是這樣的嗎?

foreach($json['signupbasic'][0]['gender']['values'] as $genderValue) { 
    foreach($genderValue as $genderKey => $genderSubVal) { 
    echo $genderKey." ".$genderSubVal."\n\r"; 
    } 
} 

foreach($json['signupbasic'][0]['birthday'] as $bdKey => $bdValue) { 
    echo $bdKey." ".$bdValue."\n\r"; 
} 

假設$ json是json_decode調用的返回值。

+0

中傳播上述json。謝謝大家的幫助。我會嘗試一下 – user469999 2011-04-28 08:41:00