开发者

Help with how to handle Json/php array data

I have the following code

$getchats = $api->call("getLatestChats");
echo '<pre>';
print_r(array_values($getchats));
echo '</pre>';

which gives me the following results..

Array
(
[0] => success
[1] => getLatestChats
[2] => Array
    (
        [0] => Array
            (
                [message] => type /ch g
                [time] => 1311806596
                [player] => mattsnod
            )

        [1] => Array
            (
                [message] => im good
                [time] => 1311806601
                [player] => lueroi01
            )
开发者_高级运维
        [2] => Array
            (
                [message] => im good
                [time] => 1311806606
                [player] => lueroi01
            )

I know I can use

print_r(var_dump($getchats));

to get it into a PHP array, but im having trouble figuring out how to work with this data better.

in the php array, its like this

array(3) {
  ["result"]=>
  string(7) "success"
  ["source"]=>
  string(14) "getLatestChats"
  ["success"]=>
  array(50) {
[0]=>
array(3) {
  ["message"]=>
  string(4) "yes?"
  ["time"]=>
  int(1311806791)
  ["player"]=>
  string(8) "mattsnod"
}
[1]=>
array(3) {
  ["message"]=>
  string(10) "um hold on"
  ["time"]=>
  int(1311806796)
  ["player"]=>
  string(8) "mattsnod"
}

This is a very general question, but how could I use a foreach or while to work with this data as Im not having much luck. Im sure Im overlooking something, but my head is hurting now. Ive been googling and reading all morning.

after some more playing, this is close to what im wanting..

foreach($getchats as $chat_on=>$data){
//echo "<b>$chat_on</b><br>";
foreach($data as $key=>$value){
    //echo "$key - $value<br>";
    foreach($value as $key2=>$value2){
        echo "$key2 - $value2<br>";

    }
}
}

now to figure out how to manipulate these individually or call as $key2[message] so that i can convert the time stamp and format differently.

message - rlly? time - 1311809089 player - m0terb0nd

Ultimately, this accomplished what I wanted..

foreach($getchats as $chat_on=>$data){
    foreach($data as $key=>$value){
            $date = date("h:i a", $value['time']);
            echo "($date) $value[player]: $value[message]<BR><BR>";
    }
}


foreach($getchats->success as $chat) {
   echo $chat['message'] . "<br>";
   echo $chat['time'] . "<br>";
   echo $chat['player'] . "<br>";    
}

Some reference on foreach can be found here - http://www.php.net/manual/en/control-structures.foreach.php


It's an array... access the data like you would in any other array.

echo $getchats[2][0]['msg']; // produces: "type /ch g"
echo $getchats[0]; // produces "success"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜