-1

I have a set of PHP arrays coming from an outside service. I only want the most recent however, how can I get this in PHP.

The Array is like this:

{"responseCode": 200, "message": "success"}

I am splitting it apart like this:

foreach ($submissions as $submissions) {
      print "<p><b>" . $submissions["message"] . "</b><br>";
    }

The problem is every time I do this, it returns the Message and Response Code from all of the arrays returned by my code. This is obviously due to the fact I am loping it, "for each" but how can I set this to only loop once?

4

3 回答 3

1
print "<p><b>" . $submissions[0]["message"] . "</b><br>";

without the foreach...

于 2013-09-08T14:15:37.743 回答
0

You don't actually need a loop if you're just trying to print the response code and message for the given JSON string:

$array = json_decode('{"responseCode": 200, "message": "success"}', true);
$resp_code = $array['responseCode'];
$message = $array['message'];

The problem is every time I do this, it returns the Message and Response Code from all of the arrays returned by my code.

However, if your JSON string contains more instances of the same response code and message and you only need the first one, you can simply use a break statement.

Your foreach syntax looks rather incorrect, and it should be something like:

foreach($array as $value) {

    //echo the values

    break; //end execution
}

Learn more about foreach loop here.

于 2013-09-08T14:13:11.783 回答
0

why you need to loop, when you need a particular value only.

print "<p><b>" . $submissions[0]["message"] . "</b><br>";

you can easily do it like above

于 2013-09-08T14:16:44.950 回答