Jump to content

How to get value from array/string/.../something


rakdoll

Recommended Posts

Hi

 

I need to know how to arrange this thing looking something like an array to be able to find values from it. The problem is, I don't really even know what I am dealing with here. Wonder if you could help me out. :)

 

I have the following mess stored in a $messages variable:

 

{"messages":[{"message_id":2314446,"from":"441234568790","timestamp":1280703181,"text":"first text"},{"message_id":2315147,"from":"123456789014","timestamp":1280755014,"text":"another text"}],"unread":0}

 

and I don't know how to do the following:

 

I need is to get only the message text from any message that matches my wanted phone number. For example: I want to get the text value from message which from is 123456789014.

 

/Henry

Link to comment
Share on other sites

That is a JSON formated response. In concept it is an array just formatted differently. Its mostly used through JavaScript (or at least by me it is, but I don't speak for everyone)

 

I have not yet met a need where I need to handle my JSON queries through php but if I were to start I would start here most likely

http://php.net/manual/en/function.json-decode.php

From what I understand of that php function is that it takes a JSON formated array and converts it to a php based array where if you can work with arrays that are php based then you will be all set to handle it how you want handled.

Link to comment
Share on other sites

That is a JSON formated response. In concept it is an array just formatted differently. Its mostly used through JavaScript (or at least by me it is, but I don't speak for everyone)

 

I have not yet met a need where I need to handle my JSON queries through php but if I were to start I would start here most likely

http://php.net/manual/en/function.json-decode.php

From what I understand of that php function is that it takes a JSON formated array and converts it to a php based array where if you can work with arrays that are php based then you will be all set to handle it how you want handled.

 

Thank you Sir! :) Now I know how to proceed.

Link to comment
Share on other sites

One more issue. I managed to convert this from json to array, but I am unable to echo any values from the array.

 

Here is the array $messages:

 

array(2) { ["messages"]=>  array(2) { [0]=>  array(4) { ["message_id"]=>  int(2314446) ["from"]=>  string(12) "358222222222" ["timestamp"]=>  int(1280703181) ["text"]=>  string(33) "This is a message" } [1]=>  array(4) { ["message_id"]=>  int(2315147) ["from"]=>  string(12) "358111111111" ["timestamp"]=>  int(1280755014) ["text"]=>  string(12) "This is another message" } } ["unread"]=>  int(0) } 

 

How would I go about getting my page echo both the "text" values?

 

echo $messages["text"];  wont do the trick.. :(

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.