0

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>";
    }
}
2
  • How is success an array(50) if there are only two elements inside of it? Commented Jul 27, 2011 at 19:30
  • What are you trying to accomplish here? 'how could I use a foreach or while to work with this data' - you haven't said how you would like to work with the data. Are you trying to work with each element of success? Commented Jul 27, 2011 at 19:34

2 Answers 2

1
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

2
  • This produces nothing for me unfortunately, tried with and without json_decode on the var Commented Jul 27, 2011 at 19:09
  • If this produces nothing, why was it marked as correct? Does it work? Commented Jul 27, 2011 at 19:46
1

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"
2
  • Ideally, I want to run through the array. Like i would with a while statement pulling data from mysql. Commented Jul 27, 2011 at 19:10
  • I missed the array_values() you'd used in your print_r. That basically strips off one level of addressing in th eoutput. Try a plain-jane print_r($getchats) which will reveal the full array structure. Commented Jul 27, 2011 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.