0

I am using GCM(Google could messaging) for the very first time and i am stuck with some problem. Although the problem has nothing to do with the GCM.I have a JSON data

$data='{"multicast_id":7917175795873320166,"success":6,"failure":0,"canonical_ids":4,

"results":[

{"registration_id":"3","message_id":"m1"},
{"message_id":"m1"},
{"message_id":"m1"},
{"registration_id":"3","message_id":"m1"},

{"registration_id":"3","message_id":"m1"},

{"registration_id":"3","message_id":"m1"}]}';

$newData=json_decode($data);

Now what i want is the keys in the result array for which registration_id is set but i am unable to do so. I can access the registration_Id like $newData->results[0]->registration_id

I have found that array_keys() returns the keys in an array but how can i get the keys in the $newData->results array for which $newData->results[$index]->registration_id is set? The major issue is that i cant use the foreach loop for doing it. Hope i will get some help here.

2
  • 1
    why you cant use a foreach loop? Commented Jan 13, 2014 at 13:24
  • Because i can do it easily with a foreach loop. But i wan to know if i can achieve it with any inbuilt function also. Commented Jan 13, 2014 at 13:26

3 Answers 3

1

Sure. First off, use the second param of json_decode so you're actually working with an array and not an object. Then filter the array with the items you want (where registration_id is set) and get the keys.

$newData=json_decode($data, true);

$filteredResults = array_filter($newData['results'], function($item) {
    return isset($item['registration_id']);
});

print_r(array_keys($filteredResults));

Working example: http://3v4l.org/e8doL

Note this code assumes you are using PHP 5.3 or later. If you are on an earlier version, you'll need to define your array_filter callback function first and pass it in rather than using an anonymous function.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. i was unaware of the bo0lean parameter in json_decode(). Thanks for letting me know about it.
@Letmesee Sure thing. So does this provide what you were looking for?
Yes absolutely. I will mark it as an answer as soon as i will be be able to do so. Thanks again
1

The reason that you can only address the items in object mode, is due to the way the json_decode was called.

To return $data as an associative array, use:

$newData=json_decode($data, true)

1 Comment

This doesn't address the original question: how can i get the keys in the $newData->results array for which $newData->results[$index]->registration_id is set? .
1
$newData=json_decode($data, true)

if you're passing the second param true then it converts the json to assosiative array(an array with key val pair).Official doc Here is official documentation Json_decode Php Official

an example to show that

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

its output will be

object(stdClass)#1 (5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

array(5) {
    ["a"] => int(1)
    ["b"] => int(2)
    ["c"] => int(3)
    ["d"] => int(4)
    ["e"] => int(5)
}

Comments

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.