Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Posts
    131
    Thanks
    21
    Thanked 0 Times in 0 Posts

    Question Parsing struggles

    Hi,

    Im getting all confused with parsing yet again, and wondering if someone could put me out of my misery.

    Heres a simplified sample of what im getting (with some basic formatting to make it a little easier to read)
    Code:
    {
    "apikey":"12345678","info":{"user":{"label_01":"1","label_02":"2","label_03":"3"}
    },
    
    "subinfo":[
    {"label":"test1","info":"test","info2":"test"},
    {"label":"test2","info":"test","info2":"test"},
    {"label":"test3","info":"test","info2":"test"},]
    }
    Its account info, then multiple entries within that.
    I cant change the search results i pull, so I need to be able to make use of the bits of information i want and then i'll be able to present it how i wish.

    PHP Code:
    $apiData file_get_contents{"http://url.com");
    $pieces explode(","$apiData);

    foreach (
    $pieces as $strData
        {
            
    $replace = array('"','{''}'"'"'*removed*''['']');
            
    $strData str_replace($replace""$strData); 
            
    $strData explode(":"$strData); 
            
    $strArray = array($strData[0] => $strData[1]); 

            
    $config_stat $strData[0]."=".$strData[1];
            
    $array explode('=',$config_stat,2);
            
    $strData[0] = trim($array[0]);
            
    $strData[1] = trim($array[1]);
            ${
    $strData[0]} = $strData[1];        
        } 
    Note: I've had to remove ''' as it auto converts to ' and screws the php formatting up, making it look like i've put ''' in there. If someone could explain how i'd add the removal of ' from the output, i'd appreciate it.

    So that just basically dumps everything out and creates a string in the name of the label, so if i ask for $apikey then it'd return 12345678.
    However when it comes to the sub-info, they all have the same label name, so i cant use $label because it'd just be the last entry.

    It seems like i need multiple runs at breaking it down, but just getting what ive got already is quite a (pathetic) achievement for me, to have to break it up into 3 parts is probably a couple of steps too far.

    Im guessing it'd start by using a different explode value, so instead of getting individual bits of info it pulls out the subinfo to be dealt with separately, and then break it down bit by bit like that? Im certain thats not quite the answer, but probably not a million miles away?

    Any suggestions, even if its to say which php functions to go look up and i'll figure it out for myself, doesnt need to be the full solution as long as i can get there in the end.

    Thx
    Last edited by PaulC2K; 07-12-2013 at 04:33 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    What you're grabbing from the external site (using the api) sort of looks like the format of the data is JSON. Can you verify if it is? Maybe you can find some further docs that mention JSON. Or someone else can back me on my hunch that it's JSON. If so, you can parse it much easier.

    I've only used JSON in simpler forms ... your data seems like something "nested".

    Let's see what others here think about it.

  • Users who have thanked mlseim for this post:

    PaulC2K (07-12-2013)

  • #3
    Regular Coder
    Join Date
    Jul 2005
    Posts
    131
    Thanks
    21
    Thanked 0 Times in 0 Posts
    Yeah, im pretty sure it is JSON, wouldnt know how to do it any other way than ive started tbh.

    I'll google it and see if i can find something useful though

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Start here:
    http://www.google.com/search?q=php+d...al&btnG=Search

    There are PHP decode/encode functions.
    And probably some examples as well.

  • #5
    Regular Coder
    Join Date
    Jul 2005
    Posts
    131
    Thanks
    21
    Thanked 0 Times in 0 Posts

    Thumbs up

    Yeah, i've managed to convert it into a standard looking array, its just in terms of the tiers.

    Mentioning 'nested' at least gave me another term to search for as i hadnt a clue what its correct term might be (i have this issue with most things, i know roughly what i want just not what the correct term for it would be, which makes it hard searching or asking for help), and i quickly found a simple tutorial for multi-dimension arrays, and from that ive been able to figure it out step by step and produce the sort of results I need in order to then do something with the data.

    So hopefully im all set now, thanks for your assistance.

    Edit - Updated thread to 'Resolved' status.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •