2012-01-08 61 views
1

因此,在一开始,send_sms.php加载之前PHP的烦恼,我有此JSON存储在数据库中:从JSON转换为stdClass的,进行更改,然后再转换回JSON

{ 
"chats": { 
    "chat": [{ 
     "id": "1", 
     "name": "Ethan Wilberforce", 
     "messages": { 
      "message": [{ 
       "id": "1", 
       "name": "Ethan Wilberforce", 
       "text": "Hello how are you doing", 
       "time": "4:41" 
      }, { 
       "id": "2", 
       "name": "Qasim Iqbal", 
       "text": "Not bad. How about you?", 
       "time": "4:42" 
      }, { 
       "id": "3", 
       "name": "Ethan Wilberforce", 
       "text": "I'm not too bad myself.", 
       "time": "4:43" 
      }] 
     } 
    }, { 
     "id": "2", 
     "name": "Geoff Vahaaho", 
     "messages": { 
      "message": [{ 
       "id": "1", 
       "name": "Geoff Vahaaho", 
       "text": "Hello how are you doing", 
       "time": "4:41" 
      }, { 
       "id": "2", 
       "name": "Qasim Iqbal", 
       "text": "Not bad. How about you?", 
       "time": "4:42" 
      }, { 
       "id": "3", 
       "name": "Geoff Vahaaho", 
       "text": "I'm not too bad myself.", 
       "time": "4:43" 
      }, { 
       "id": "4", 
       "name": "Qasim Iqbal", 
       "text": "Nice.", 
       "time": "4:43" 
      }] 
     } 
    }] 
} 
} 

Json是完全有效的,没有错误。它存储两条聊天消息,其中包含消息。

现在,这里是PHP代码,改变了JSON:

$data = $user->data; 
    $parsed_data = json_decode($data); 

     ... 

    for($i = 0, $size = sizeof($parsed_data->chats->chat); $i < $size; ++$i) { 
     if($parsed_data->chats->chat[$i]->name == $to) { 
      $found = true; 
      $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)] = new stdClass; 
      $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->id = sizeof($parsed_data->chats->chat[$i]->messages->message); 
      $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->name = $user->name; 
      $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->text = $message; 
      $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->time = $time; 
      echo "done. "; 
      break; 
     } 
    } 

我打算这是什么做的是,在聊天的“消息”数组中添加其他stdClass的对象。所以我基本上就是这样做的,希望它能奏效。

现在,它的工作原理,种,但这里是新的JSON后,我们json_encode它:

{ 
"chats": { 
    "chat": [{ 
     "id": "1", 
     "name": "Ethan Wilberforce", 
     "messages": { 
      "message": [{ 
       "id": "1", 
       "name": "Ethan Wilberforce", 
       "text": "Hello how are you doing", 
       "time": "4:41" 
      }, { 
       "id": "2", 
       "name": "Qasim Iqbal", 
       "text": "Not bad. How about you?", 
       "time": "4:42" 
      }, { 
       "id": "3", 
       "name": "Ethan Wilberforce", 
       "text": "I'm not too bad myself.", 
       "time": "4:43" 
      }, {}, { 
       "id": 4 
      }, { 
       "name": "Qasim Iqbal" 
      }, { 
       "text": "Hello i am testing" 
      }, { 
       "time": 1326066200 
      }] 
     } 
    }, { 
     "id": "2", 
     "name": "Geoff Vahaaho", 
     "messages": { 
      "message": [{ 
       "id": "1", 
       "name": "Geoff Vahaaho", 
       "text": "Hello how are you doing", 
       "time": "4:41" 
      }, { 
       "id": "2", 
       "name": "Qasim Iqbal", 
       "text": "Not bad. How about you?", 
       "time": "4:42" 
      }, { 
       "id": "3", 
       "name": "Geoff Vahaaho", 
       "text": "I'm not too bad myself.", 
       "time": "4:43" 
      }, { 
       "id": "4", 
       "name": "Qasim Iqbal", 
       "text": "Nice.", 
       "time": "4:43" 
      }] 
     } 
    }] 
} 
} 

你会发现它的“伊桑·威尔伯福斯”聊天确实添加,但在每个字符串stdClass在“消息”数组中转换为它自己的数组项。我怎样才能解决这个问题?非常感谢。

+0

我认为你需要循环遍历每个$ parsed_data-> chats-> chat [$ i] - > messages->消息,然后在循环内循环并添加数据。虽然我不确定你想要做什么。你能告诉我们你期望看到的结果吗? – Ignas 2012-01-09 00:04:45

回答

3

你的问题是这样的:

 $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)] = new stdClass; 
     $parsed_data->chats->chat[$i]->messages->message[sizeof($parsed_data->chats->chat[$i]->messages->message)]->id = sizeof($parsed_data->chats->chat[$i]->messages->message); 

它基本上相当于:

$array[$last] = new stdClass(); 
$array[$last+1] = "id"; 
$array[$last+2] = "name"; 

你一直追加新的阵列/对象,因为你用sizeof(...)总是变得比前一行较大的一个。这不是最后一个索引,而是大小。这是$lastindex+1

什么,你应该做的,无论如何,没有使用一个对象,只是追加数组,其所有属性的一次:

$parsed_data->chats->chat[$i]->messages->message[] = array(
     "id" => ..., 
     "name" => ..., 
     "time" => ..., 
); 

当你编码关联数组回JSON它也将成为正常的{...} JSON对象组。

+0

发布这个问题后不久,我终于明白了这一点。谢谢! :)使用'(object)$ array',我将创建的数组转换为一个对象,然后消息[sizeof()]等于转换后的数组。 – Qasim 2012-01-09 00:08:16