Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Output value of nested arrays?

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 39 total)
  • Author
    Posts
  • #41983
    chrisburton
    Participant

    Is there a simple way to output a value that’s nested inside multiple arrays?

    Array ouput:

    Array
    (
    [auth] => Array
    (
    [uid] => 50799167
    [info] => Array
    (
    [name] => Chris Burton
    [nickname] => chrisburton
    [urls] => Array
    (
    [twitter] => http://twitter.com/chrisburton
    [website] => http://chrisburton.me
    )

    [location] => New York
    [description] => Letterer & Typographer
    http://dribbble.com/chrisburton
    [image] => http://a0.twimg.com/profile_images/3106804659/f100a994784f6e8059bb15125f286667_normal.png
    )

    [credentials] => Array
    (
    [token] => Token Removed
    [secret] => Secret Removed
    )

    [raw] => Array
    (
    [id] => 50799167
    [profile_sidebar_fill_color] => DDEEF6
    [created_at] => Thu Jun 25 22:48:35 +0000 2009
    [contributors_enabled] => 0
    )

    #120981
    __
    Participant

    Do you know which value you need?

    #120984
    chrisburton
    Participant

    @traq Well, let’s say I need multiple values.

    [id]
    [screen_name]
    [image]

    #120985
    chrisburton
    Participant

    I was able to get it by doing this

    echo $auth;

    Just thought they’re might be a better way. Maybe something like:

    echo $raw->screen_name;

    #120986
    __
    Participant

    If this is something you’ll be doing regularly (i.e., *more than once, ever*), I’d suggest writing a function to extract them into a more useful format. Something like

    // $array is your array from twitter
    function twitter_user_info( array $array ){

    // check that each item exists in $array
    $id = isset( $array )?
    $array:
    null;
    $nick = isset( $array )?
    $array:
    null;
    $img = isset( $array )?
    $array:
    null;

    // if any are missing, something went wrong
    if( $id === null || $nick === null || $img === null ){
    return false;
    }

    // array is both numeric and assoc
    // so you can access it by key
    // or by index # – e.g., using
    // `list( $id,$name,$img ) = twitter_user_info()`
    $user_info = array(
    $id
    ,$nick
    ,$image
    ,’id’ => $id
    ,’screen_name’ => $nick
    ,’image’ => $image
    );

    return $user_info;
    }

    ****
    If you want an object (as in your last comment), use the same function as above but replace `$user_info` with this:

    $user_info = (object)array(
    ‘id’ => $id
    ,’screen_name’ => $nick
    ,’image’ => $image
    );

    Then you can access it like so:

    $chrisburton = twitter_user_info( $twitter_array );

    echo $chrisburton->screen_name;

    #120968
    chrisburton
    Participant

    @traq Couldn’t I just use a foreach to return the values as variables?

    http://davidwalsh.name/convert-key-value-arrays-standard-variables-php

    #120969
    __
    Participant

    If you wanted all of them, sure.

    If you only wanted a few, you’d be creating a lot of extra variables for no reason (which would then be floating around, wasting memory, waiting to collide with another var by the same name). I wouldn’t recommend it, at least not outside of a function scope.

    If that’s really what you want, you can just use [extract()](http://php.net/extract), which does exactly the same thing (and offers a *bit* of control via flags).

    Also, consider that you’d have to make extract()/your function/construct/whatever recursive, or you’d still have arrays:

    foreach( $array as $k=>$v ){
    ${$key} = $v;
    }
    echo $nickname;
    // error – $nickname does not exist
    // $auth exists – but it’s still an array,
    // and all those values are still nested inside it.

    All things considered, maybe you’d like a user class, that could take the array and organize all the values for you. I’ll write up a quick example.

    #120971
    chrisburton
    Participant

    I just want something that would be secure but also easy for me to understand at the same time.

    #120957
    __
    Participant

    I think [this](https://gist.github.com/4536162) will do what you want. Try it out (see demo at the bottom).

    #120958
    hotpink
    Member

    @chrisburton
    I wanted to learn more about PHP arrays so I played with your example code.

    Maybe you will find it useful.

    http://phpfiddle.org/main/code/9gz-f2n

    #120959
    chrisburton
    Participant

    @traq @hotpink Oh, wow. I really appreciate you both taking the time on this.


    @traq
    I’m receiving this: http://cloud.chrisburton.me/image/420t0i2a3v1B

    #120960
    __
    Participant

    @chrisburton

    `$twitter_array` needs to be the variable that contains the array you receive from twitter. Are you calling it something else in your code?

    also, does the demo below the class work for you?

    *****

    *to explain further, `undefined variable` means that `$twitter_array` doesn’t exist in your code before you try to use it in `new twitter_user( $twitter_array )` – which is also why twitter_user is throwing an exception.*

    #120962
    chrisburton
    Participant

    @traq Yes, I am. `$response`

    Now I am getting

    ( ! ) Parse error: syntax error, unexpected ‘[‘ in C:UsersChrisDesktopwampwwwauthexamplecallback.php on line 186

    Line 186: `$response = [`

    #120963
    __
    Participant

    > Now I am getting `( ! ) Parse error: syntax error, unexpected ‘[‘ …`

    Sorry. You’re not using PHP 5.4, are you?

    #120964
    __
    Participant

    Get rid of my demo code below the class.

    instead, use:

    $cb = new twitter_user( $response );

    (assuming `$response` is your response from twitter).

Viewing 15 posts - 1 through 15 (of 39 total)
  • The forum ‘Back End’ is closed to new topics and replies.