1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Parse Array Results how?

Discussion in 'PHP' started by @nibb, Jul 11, 2013.

  1. #1
    Hi, I know, this may sound stupid, but im not a developer, so I could be testing for hours something that someone would probably laugh at.

    I have an API script, that when executed returns results in an array. I searched this and this seems to be on purpose, since I guess you can then take this values and use them in PHP.

    When I executed it in the browsers, and then look its source code you get something like this:
    {"success":true,"invoice_id":"258","call":"addInvoice","server_time":132172500}
    How can I use this values inside the same PHP code instead of printing them just in the browser like that?
    Currently the script shows this because its outputs the results like this:
    print_r($return);
    My questions, is how would I actually just use this values in PHP?
    For example something like this:
    result0 = true
    result1 = 258
    etc
    Basically I just want to parse the results so I can use them again.
    Any tips?
    I need basically the invoice id like:
    invoice_id = 258
    This is all I actually need from the result. All the calls output results similar to the above, so this means its some type of array output and probably PHP developers use them again. Well, I just need to use one value again but I canĀ“t find how to parse this results. I search PHP parse results in Google and there is nothing in particular helpful but rather most talk about how to make arrays not parse them.
    Help would be appreciated guys.
    Solved! View solution.
    @nibb, Jul 11, 2013 IP
  2. @nibb

    @nibb Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    Solved.

    It seems there is a PHP function for this called extract which puts the results in variable.

    How simple :D
    @nibb, Jul 11, 2013 IP
  3. jackderips

    jackderips Active Member

    Messages:
    215
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    50
    #3
    jackderips, Jul 11, 2013 IP
  4. cochisetm

    cochisetm Member

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #4
    cochisetm, Jul 11, 2013 IP
    jackderips likes this.
  5. cochisetm

    cochisetm Member

    Messages:
    80
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #5
    Ha! We posted at virtually the exact same time!
    cochisetm, Jul 11, 2013 IP
  6. @nibb

    @nibb Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    Yes this looks like JSON, reason why I was not finding the answer. Once I searched in Google as JSON I found the result with the PHP extract function.
    @nibb, Jul 11, 2013 IP
  7. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,101
    Likes Received:
    98
    Best Answers:
    1
    Trophy Points:
    205
    #7
    Okay, here are some tips. If you want to download content from a remote location, let's say from your API, you would use the following:
    PHP:
    1. $result = file_get_contents('http://blabla');
    Now $result will contain exactly what you see in your browser when you open that URL. However, I see that your API returns the results in JSON format, which is basucally a way to represent a collection of data as a single line (with lots and lots of nested brackets).

    There are built-in functions in PHP that allow you to encode/decode JSON data. In your case you would use:
    PHP:
    1. $newResult = json_decode($result);
    Note that now $newResult contains a stdObject, which is basically an object, similar to the ones you see in every other programming language. You may access its properties in the following manner object->propertyName.

    If I take your above example data, after using the above 2 code examples I have provided, you will easily get access to the invoice number like this:
    PHP:
    1. $invoiceNumber = $newResult->invoice_id;
    If you want to display it in the browser, use "echo":
    PHP:
    1. echo $invoiceNumber;
    Also, as with any PHP code, don't forget the opening and closing PHP syntax before and after your PHP code:
    PHP:
    1. <?php
    2. // Your PHP code here
    3. ?>
    ColorWP.com, Jul 11, 2013 IP
    ryan_uk likes this.
  8. @nibb

    @nibb Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    Thanks, yes I used file_get_contents,

    Is there any benefit between this or fopen or curl?

    Also, what difference would be between using your described method json decode and the one I used "extract".

    I seems that all I had to add to my php file at the end of the code was this:
    extract($result);

    Absolutely nothing more. That alone lets me reuse the resuls as variable like
    $invoice_number

    In the same file which is what I wanted.

    That looks even more simple than trying to decode the json result which seems to require more code and lines to do the same? Because all results can be used in variables with "extract", at least in my test, it seems this feature converts all results directly into variables.
    @nibb, Jul 11, 2013 IP
  9. #9
    The function extract() accepts an array as its first parameter, so you are definitely decoding the input string to an array somewhere - be it with json_decode or with some library. Simply said, you can not just pass extract() the variable $result if that variable is not an array.

    As for why would you prefer extract() over simply looping through the array and assigning its values to variables - it's a matter of choice and depends on what you are working on. Also, remember that extract() will overwrite any previous variables with the same name on collision. So in your case if you had a variable named $invoice_number with some content before calling extract(), it would have been overwritten. Another disadvantage is security. If you do not properly control the input array you are providing, a malicious user may easily inject his custom variable names and variable values there to overwrite your own variables in code.

    An example of this concept:

    PHP:
    1. // Input array 1 (safe)
    2. $arr = array('invoice_id'=>234);
    3. $logged_in_user = 10;
    4. extract($arr);
    5. if($logged_in_user == 999){
    6.   echo 'You are an admin. Here is some confidential data: ...';
    7. }
    8.  
    9. // Input array 2 (unsafe, overwritten by hacker)
    10. $arr = array('invoice_id'=>234, 'logged_in_user'=>999);
    11. $logged_in_user = 10;
    12. extract($arr);
    13. if($logged_in_user == 999){
    14.   echo 'You are an admin. Here is some confidential data: ...';
    15. }
    As you can see in the second example, if you don't control the elements in the input array, you may give the user admin permissions.

    Another disadvantage of extract() is that you can't use nested associative arrays easily.

    There are, however, advantages to using extract(). For example, it will work across different types of data. For example, if in the future you decide to add 100 more properties (besides invoice_id; like data_of_issue, issuer, invoice_amount, invoice_email, etc), you wouldn't have to make a lot of code changes, since extract() will "extract" these new values as well. However, I don't think that is much of an advantage and I prefer to loop through my arrays:

    PHP:
    1. foreach($arr as $key=>$value){
    2.     switch($key){
    3.     case 'invoice_id':
    4.         $invoice_id = $value;
    5.         break;
    6.     case 'invoice_email':
    7.         $email = $value;
    8.         break;
    9.     }
    10. }
    Also, if I have helped you a bit, don't forget to mark my post as accepted answer. ;)
    ColorWP.com, Jul 12, 2013 IP