Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Parsing arrays within arrays?
Old 08-16-2009, 10:37 AM Parsing arrays within arrays?
Junior Talker

Posts: 2
Trades: 0
Hello there everyone.

Some recent code I've been working on connects to an online web service. This code (after using json_decode($events, true)) displays the following:

Code:
Array
(
    [0] => Array
        (
            [0] => connected
        )

    [1] => Array
        (
            [0] => typing
        )

    [2] => Array
        (
            [0] => gotMessage
            [1] => Hello!
        )

    [3] => Array
        (
            [0] => typing
        )

    [4] => Array
        (
            [0] => stoppedTyping
        )

)
My question is, how would I go about accessing this data, seeing as it's a bunch of arrays inside of an array?

I understand the basics of accessing arrays in the method of $array_name[number] and $array_name['identifier'].

Lets assume the array code I posted above was the variable $events. If I do

Code:
print_r($events[4]);
That does not return the desired result (often outputting nothing at all). What am I doing wrong here? Can anyone set me on the right track?

Thank you in advance for your time & help.
DodNetter is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-16-2009, 11:51 AM Re: Parsing arrays within arrays?
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Perhaps you are missing something else, because if the variable $events looks like the above output, then print_r($events[4]); should indeed output only that part of the array. Multi dimentional arrays are accessed by simply adding more brackets.

PHP Code:
$a = array(array(array("some value")));
echo 
$a[0][0][0]; // Will output "some value" 
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 08-16-2009, 12:28 PM Re: Parsing arrays within arrays?
Junior Talker

Posts: 2
Trades: 0
Thank you for your reply Lizciz, it was most useful to me. Using the code you provided, I managed to track down some more errors which lead me to the (seemingly simple) root cause of my problems; the web service I'm accessing is, for whatever reason, outputting their array values as one array element per character.

I've contacted them and they've confirmed this is an error on their end.

Thank you for all your help, hopefully solving this problem should get a fair bit easier now
DodNetter is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Parsing arrays within arrays?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13491 seconds with 12 queries