Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #14964 > unrolled thread
| Started by | Adnan Shiekh <adnanshahi123@gmail.com> |
|---|---|
| First post | 2015-02-23 00:09 -0800 |
| Last post | 2015-02-24 07:47 -0500 |
| Articles | 13 — 4 participants |
Back to article view | Back to comp.lang.php
4 dimensional associative array access Adnan Shiekh <adnanshahi123@gmail.com> - 2015-02-23 00:09 -0800
Re: 4 dimensional associative array access Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-23 09:48 -0500
Re: 4 dimensional associative array access Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-02-23 15:52 +0100
Re: 4 dimensional associative array access Adnan Shiekh <adnanshahi123@gmail.com> - 2015-02-23 10:26 -0800
Re: 4 dimensional associative array access Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-23 15:44 -0500
Re: 4 dimensional associative array access Adnan Shiekh <adnanshahi123@gmail.com> - 2015-02-23 10:39 -0800
Re: 4 dimensional associative array access Adnan Shiekh <adnanshahi123@gmail.com> - 2015-02-23 13:31 -0800
Re: 4 dimensional associative array access Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-23 22:39 +0000
Re: 4 dimensional associative array access Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-24 00:13 +0000
Re: 4 dimensional associative array access Denis McMahon <denismfmcmahon@gmail.com> - 2015-02-24 01:49 +0000
Re: 4 dimensional associative array access Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-23 20:56 -0500
Re: 4 dimensional associative array access Adnan Shiekh <adnanshahi123@gmail.com> - 2015-02-23 23:07 -0800
Re: 4 dimensional associative array access Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-24 07:47 -0500
| From | Adnan Shiekh <adnanshahi123@gmail.com> |
|---|---|
| Date | 2015-02-23 00:09 -0800 |
| Subject | 4 dimensional associative array access |
| Message-ID | <f6d75dde-ec33-4c8e-8d77-b370152a72bd@googlegroups.com> |
Hi everyone,
i have a question how to access 4 dimesional array efficiently in php
<?php
$json = '{
"return": true,
"output": {
"total": 100,
"data": [
{
"name": "john",
"password": "hello",
}
},
{
"name": "jack",
"password": "hello2",
}
}
]
}
}
';
$result = json_decode ($json);
?>
i have convert this json and print like this
|name| password|
_______________
|john| hello |
|jack| hello2 |
i know this json convert in to associative array so that how can i access that?? thanks in advance...
[toc] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2015-02-23 09:48 -0500 |
| Message-ID | <mcfeim$mmm$1@dont-email.me> |
| In reply to | #14964 |
On 2/23/2015 3:09 AM, Adnan Shiekh wrote:
> Hi everyone,
>
> i have a question how to access 4 dimesional array efficiently in php
>
>
> <?php
> $json = '{
> "return": true,
> "output": {
> "total": 100,
> "data": [
> {
> "name": "john",
> "password": "hello",
> }
> },
> {
> "name": "jack",
> "password": "hello2",
> }
> }
> ]
> }
> }
> ';
> $result = json_decode ($json);
> ?>
>
> i have convert this json and print like this
>
> |name| password|
> _______________
> |john| hello |
> |jack| hello2 |
>
> i know this json convert in to associative array so that how can i access that?? thanks in advance...
>
First of all, this is a two dimensional array, not a four dimensional one.
The question is - how did you convert the json string to the array?
That will determine how you access it.
It often helps to use print_r() to display a variable; it provides a lot
of information on how the variable is constructed (array, object, etc.).
var_dump() does similar, but the format is a bit different.
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Erwin Moller <erwinmollerusenet@xs4all.nl> |
|---|---|
| Date | 2015-02-23 15:52 +0100 |
| Message-ID | <54eb3eaa$0$2859$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #14964 |
On 2/23/2015 9:09 AM, Adnan Shiekh wrote:
> Hi everyone,
>
> i have a question how to access 4 dimesional array efficiently in php
>
>
> <?php
> $json = '{
> "return": true,
> "output": {
> "total": 100,
> "data": [
> {
> "name": "john",
> "password": "hello",
> }
> },
> {
> "name": "jack",
> "password": "hello2",
> }
> }
> ]
> }
> }
> ';
> $result = json_decode ($json);
> ?>
>
> i have convert this json and print like this
>
> |name| password|
> _______________
> |john| hello |
> |jack| hello2 |
>
> i know this json convert in to associative array so that how can i access that?? thanks in advance...
>
Hi, first: your attempt to construct the JSON string didn't really succeed.
You have multiple , and } thrown in the code, that don't belong there.
I removed the "output" too because I have no idea what you wanted to do
with that.
Try this:
<?php
$json = '{
"return": true,
"total": 100,
"data": [
{"name": "john","password": "hello"},
{"name": "jack","password": "hello2"}
]
}';
$result = json_decode ($json);
echo "<pre>";
var_dump($result);
echo "</pre>";
?>
=================
It will create the following output:
object(stdClass)#1 (3) {
["return"]=>
bool(true)
["total"]=>
int(100)
["data"]=>
array(2) {
[0]=>
object(stdClass)#2 (2) {
["name"]=>
string(4) "john"
["password"]=>
string(5) "hello"
}
[1]=>
object(stdClass)#3 (2) {
["name"]=>
string(4) "jack"
["password"]=>
string(6) "hello2"
}
}
}
You can see that json_decode() returns an Object.
http://php.net/manual/en/function.json-decode.php
In that Object are properties (name/value pairs), like "total" and
"return" and "data".
If you want to use one, just add something like this:
$myData = $result->data;
foreach ($myData as $oneElement){
echo "name: ".$oneElement->name;
echo " has password: ";
echo $oneElement->password."<br>\n";
}
Does that help?
Regards,
Erwin Moller
PS: Your example wasn't a 4 dimensional array. It is an Object that
holds 4 properties.
--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
[toc] | [prev] | [next] | [standalone]
| From | Adnan Shiekh <adnanshahi123@gmail.com> |
|---|---|
| Date | 2015-02-23 10:26 -0800 |
| Message-ID | <0a046a55-b4d4-453d-8ae1-a41e1c1eaf7a@googlegroups.com> |
| In reply to | #14964 |
Sorry to all i'm ask question in wrong way basically my $result return some thing else but for long i am not show here.. i'm show the output after var_dump($result) is:
array(2) { ["response"]=> array(2) { ["success"]=> bool(true) ["result"]=> array(34) { [0]=> array(15) { ["id"]=> string(7) "1965684" ["device_id"]=> string(4) "6386" ["Email"]=> string(28) "Hello sir how do you feel..!" ["status"]=> string(7) "pending" ["send_at"]=> int(1424675520) ["queued_at"]=> int(0) ["sent_at"]=> int(0) ["delivered_at"]=> int(0) ["canceled_at"]=> int(0) ["failed_at"]=> int(0) ["received_at"]=> int(0) ["error"]=> string(0) "" ["created_at"]=> int(1424675520) ["contact"]=> array(3) { ["id"]=> string(6) "348244" ["name"]=> string(9) "My Number" ["number"]=> string(11) "56486464624" } } [1]=> array(15)
i'm try to ask how to retrieve some particular fields in this type of array.. sorry for wrong type question but i'm also thank full to reply my answer i hope you help me and guide me thanks.....
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2015-02-23 15:44 -0500 |
| Message-ID | <mcg3da$tbu$1@dont-email.me> |
| In reply to | #14967 |
On 2/23/2015 1:26 PM, Adnan Shiekh wrote:
> Sorry to all i'm ask question in wrong way basically my $result return some thing else but for long i am not show here.. i'm show the output after var_dump($result) is:
>
> array(2) { ["response"]=> array(2) { ["success"]=> bool(true) ["result"]=> array(34) { [0]=> array(15) { ["id"]=> string(7) "1965684" ["device_id"]=> string(4) "6386" ["Email"]=> string(28) "Hello sir how do you feel..!" ["status"]=> string(7) "pending" ["send_at"]=> int(1424675520) ["queued_at"]=> int(0) ["sent_at"]=> int(0) ["delivered_at"]=> int(0) ["canceled_at"]=> int(0) ["failed_at"]=> int(0) ["received_at"]=> int(0) ["error"]=> string(0) "" ["created_at"]=> int(1424675520) ["contact"]=> array(3) { ["id"]=> string(6) "348244" ["name"]=> string(9) "My Number" ["number"]=> string(11) "56486464624" } } [1]=> array(15)
>
>
> i'm try to ask how to retrieve some particular fields in this type of array.. sorry for wrong type question but i'm also thank full to reply my answer i hope you help me and guide me thanks.....
>
OK, this helps. You don't show the entire contents of $result (it is
quite large), but we can get a start.
Within your variable are nested arrays (multidimensional); some are
indexed and some are associative. That is:
The first array is an associative array and the first element of the
array is 'response', so it would be accessed as
$result['response']
This is an array with two associative members, 'success' and 'result'.
They would be accessed as
$result['response']['success'] and
$result['response']['result']
respectively.
The 'success' member is simply a boolean value, so the above accesses it
directly.
The 'result' member has as its contents an indexed array with 34
members. These would be accessed as
$result['response']['result'][0]
$result['response']['result'][1]
$result['response']['result'][2]
and so on, through
$result['response']['result'][33]
Each of these arrays is an associative array. The first has 15 members,
'id', 'device_id', etc., through 'contact', so they would be addressed as
$result['response']['result'][0]['id']
$result['response']['result'][0]['device_id']
...
$result['response']['result'][0]['contact']
The first 14 elements are simple values, and are accessed by the above.
The 'contact' element is an associative array with members 'id', 'name'
and 'number'. They would be accessed as
$result['response']['result'][0]['contact']['id']
$result['response']['result'][0]['contact']['name']
$result['response']['result'][0]['contact']['number']
This is all the farther I could take it because that's the end of the data.
I hope this helps. It's all a matter of breaking down the output of
var_dump() or print_r() (it helps if you have it formatted) and going
down the arrays one step at a time.
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Adnan Shiekh <adnanshahi123@gmail.com> |
|---|---|
| Date | 2015-02-23 10:39 -0800 |
| Message-ID | <f305c7e7-c442-4648-b34a-5765a9697428@googlegroups.com> |
| In reply to | #14964 |
Erwin i'm show firstly question in wrong way basically check my current post.. thanks in advance...
[toc] | [prev] | [next] | [standalone]
| From | Adnan Shiekh <adnanshahi123@gmail.com> |
|---|---|
| Date | 2015-02-23 13:31 -0800 |
| Message-ID | <06d9df7e-c58c-44f6-9492-bbb15bf70e7d@googlegroups.com> |
| In reply to | #14964 |
Thanks Jerry,
but i have used this:
for($i=0;$i<count($result);$i++){
echo['response']['result'][$i]['id'];
}
show error 'Undefined index'..
i thinks problem using loop.. please help in this problem..
thanks in advance..
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-02-23 22:39 +0000 |
| Message-ID | <mcga7o$gs7$2@dont-email.me> |
| In reply to | #14964 |
On Mon, 23 Feb 2015 00:09:40 -0800, Adnan Shiekh wrote:
> Hi everyone,
>
> i have a question how to access 4 dimesional array efficiently in php
>
>
> <?php $json = '{
> "return": true,
> "output": {
> "total": 100,
> "data": [
> {
> "name": "john",
> "password": "hello",
> }
> },
> {
> "name": "jack",
> "password": "hello2",
> }
> }
> ]
> }
> }
> ';
> $result = json_decode ($json);
> ?>
>
> i have convert this json and print like this
>
> |name| password|
> _______________
> |john| hello |
> |jack| hello2 |
>
> i know this json convert in to associative array so that how can i
> access that?? thanks in advance...
1) Look at the options to json_decode. If you want an associative array
instead of an object, you need to pass the option.
2) Fix your json string.
Try this code:
<?php
$arr = array(
'return' => true,
'output' =>
array(
'total' => 100,
'data' =>
array(
array('name' => 'john', 'password' => 'hello'),
array('name' => 'jack', 'password' => 'hello2')
)
)
);
$json = json_encode($arr);
if (json_last_error() !== JSON_ERROR_NONE) {
echo 'json error number ' . json_last_error() . "\n";
exit;
}
echo "\$json = '$json'\n";
$result = json_decode ($json, true);
if (json_last_error() !== JSON_ERROR_NONE)
echo 'json error number ' . json_last_error() . "\n";
else
print_r($result);
?>
'hello2' can be accessed as:
$result['output']['data'][1]['password']
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-02-24 00:13 +0000 |
| Message-ID | <mcgfmv$gs7$8@dont-email.me> |
| In reply to | #14964 |
On Mon, 23 Feb 2015 00:09:40 -0800, Adnan Shiekh wrote:
> Hi everyone,
>
> i have a question how to access 4 dimesional array efficiently in php
>
>
> <?php $json = '{
> "return": true,
> "output": {
> "total": 100,
> "data": [
> {
> "name": "john",
> "password": "hello",
> }
> },
> {
> "name": "jack",
> "password": "hello2",
> }
> }
> ]
> }
> }
> ';
> $result = json_decode ($json);
> ?>
>
> i have convert this json and print like this
>
> |name| password|
> _______________
> |john| hello |
> |jack| hello2 |
>
> i know this json convert in to associative array so that how can i
> access that?? thanks in advance...
1) Your json is broken.
2) If you want an associative array from the php json_decode function,
you need to set the appropriate flag. Read the php manual for json_decode.
3) use var_dump or print_r to view the structure of a multi dimensional
associative array. This will help you figure out that once the json is
fixed, you can use something like:
$result['output']['data'][1]['password']
to find the string 'hello2'
Rgds
Denis McMahon
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-02-24 01:49 +0000 |
| Message-ID | <mcglb6$uut$1@dont-email.me> |
| In reply to | #14964 |
On Mon, 23 Feb 2015 00:09:40 -0800, Adnan Shiekh wrote:
> i have a question how to access 4 dimesional array efficiently in php
> <?php $json = '{
I've replied twice, don't know what happened to them.
1) Your json is broken.
2) If you want php to decode json into an associative array, you need to
use the correct arguments. See the php manual for json_decode.
3) To access 'hello2' in your json (once you fix it) when it has been
converted into an associative array, use:
$result['output']['data'][1]['password']
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2015-02-23 20:56 -0500 |
| Message-ID | <mcglne$om$1@dont-email.me> |
| In reply to | #14973 |
On 2/23/2015 8:49 PM, Denis McMahon wrote:
> On Mon, 23 Feb 2015 00:09:40 -0800, Adnan Shiekh wrote:
>
>> i have a question how to access 4 dimesional array efficiently in php
>
>> <?php $json = '{
>
> I've replied twice, don't know what happened to them.
>
> 1) Your json is broken.
>
> 2) If you want php to decode json into an associative array, you need to
> use the correct arguments. See the php manual for json_decode.
>
> 3) To access 'hello2' in your json (once you fix it) when it has been
> converted into an associative array, use:
>
> $result['output']['data'][1]['password']
>
They showed up, Denis. Maybe eternal-september isn't updating as
quickly as it should (I've seen this happen before).
Looking at his var_dump(), does have a valid associative array. I
suspect it was sent from another site, and he's trying to duplicate the
information locally for testing/debugging.
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | Adnan Shiekh <adnanshahi123@gmail.com> |
|---|---|
| Date | 2015-02-23 23:07 -0800 |
| Message-ID | <88f1c0b5-5819-486e-a11e-ef663f0b42e5@googlegroups.com> |
| In reply to | #14964 |
yes, it's true data comes from another site and result return in $result,
foreach($result as $key=>$value){
foreach($value as $key1=>$value1){
foreach($value1 as $key2=>$value2){
foreach($value2 as $key3=>$value3){
echo $value3['id'];
}}}}
that loop is not working any body tell me what's problem in that
[toc] | [prev] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2015-02-24 07:47 -0500 |
| Message-ID | <mchrr9$n5j$1@dont-email.me> |
| In reply to | #14975 |
On 2/24/2015 2:07 AM, Adnan Shiekh wrote:
> yes, it's true data comes from another site and result return in $result,
>
>
> foreach($result as $key=>$value){
> foreach($value as $key1=>$value1){
> foreach($value1 as $key2=>$value2){
> foreach($value2 as $key3=>$value3){
> echo $value3['id'];
> }}}}
>
> that loop is not working any body tell me what's problem in that
>
It doesn't work because not every element of $result is an array. The
same with the other items.
You can only use foreach() with arrays.
P.S. Please learn how to properly respond to messages. Every response
you have made has been to the original message in the thread.
Additionally, please quote the appropriate text you're responding to.
Most of us use a real newsreader instead of google groups.
--
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web