0

i am working on a cakephp 2.x and i am sending data from android to my webapp in a json fromat

i am getting the data in my $data variable when i print_r the data

  Array (
     [sms] => [[+16063934284, hello, 1, 1372671157000]]
  )
  

i want to access each element of this array or want to save each element in a seperate variable.. for example i want to do something like this

   $number = ['sms'][0]

sorry i am week in arrays so dont how can i do this

1
  • most likely you are both submitting your data wrong, and not parsing it correctly - the Array-thing in the question is (probably, hard to tell as there are no quotes) a string. Commented Jul 1, 2013 at 13:01

5 Answers 5

1

This is not valid JSON, if your response is something like this:

[sms] => [[ '+16063934284', 'hello', 1, 1372671157000 ]]

Now you can use json_decode functon, so then you will have the following:

Array
(
    [0] => Array
        (
            [0] => 16063934284
            [1] => 123
            [2] => 1
            [3] => 1372671157000
        )

)

So later, you can use foreach() to iterate through the elements of the new array.

Here is a working example:

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

5 Comments

i am getting data in json .. when i decode the data then i get the following result which i have in my question ...
Array ( [sms] => [[+16063934284, hello, 1, 1372671157000]] )
Well, what is the output from: print_r( json_decode( $data['sms'] ) );
what i have done right now is i did this $data = json_decode($json, TRUE); $data = json_decode($data['sms']); pr($data) it prints only this "<pre></pre>"
I think you get this error, because your JSON is not well-formatted. You can test your JSON here: jsonlint.com
1

I don’t know how you’re structuring data, but you should be able to assign it to a list like this:

foreach ($data['sms'] as $row) {
    list($number, $message, $param3, $param4) = $row;
}

But for some reason you seem to have an array in an array (with the two opening square brackets) so it would actually look like this:

foreach ($data['sms'] as $row) {
    list($number, $message, $param3, $param4) = $row[0];
}

Either way, you can then just access the number in the loop via the variable name:

foreach ($data['sms'] as $row) {
    list($number, $message, $param3, $param4) = $row[0];
    printf('Number is %s', $number);
}

1 Comment

That’s because you weren’t suppose to use it verbatim. I don’t know what format your data’s coming from your Android app in.
0

Your json data inside $data['sms'] is not correct json. You need to manually parse this string or generate correct json in your Android app. If you want to parse your data use next code:

$sms = str_replace('[[', '', $data['sms']);
$sms = str_replace(']]', '', $sms);
$sms = explode(', ', $sms);
print_r($sms);

As you can see in output $sms is an array with all your attributes.

7 Comments

am getting data in json .. when i decode the data then i get the following result which i have in my question .should i have to decode again
can i do this foreach($data as $value) { print $value . "\n"; } ?
you have array of arrays. $data[0] is array. You can just set $data = $data[0] and then use foreach($data as $value)
what i have done right now is i did this $data = json_decode($json, TRUE); $data = json_decode($data['sms']); pr($data) it prints only this "<pre></pre>"
Because it's not correct json. You can generate correct json in your Android app or just use my solution. I have updated my answer. Check it.
|
0

You can use explode() function, to separate the values in array.

explode(Array_name);

Comments

0

If you send proper JSON from the app, you can simply use $array = json_decode($data['sms']) The correct JSON string would look like [["+16063934284", "hello", 1, 1372671157000]] Note that the " instead of ' (quote vs apostrophe) is important to JSON formatting, sorry Kristian Vitozev. Also note that using explode on ', ' will break if the SMS message contains a comma.

Here is a code example:

$sms_list = json_decode($data['sms']);
foreach($sms_list as $sms){
    list($phone_number, $message, $unknown_bool, $timestamp) = $sms;
    //...
}

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.