Grow your CSS skills. Land your dream job.

Output value of nested arrays?

  • # January 14, 2013 at 6:28 pm

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

    Array ouput:

    [auth] => Array
    [uid] => 50799167
    [info] => Array
    [name] => Chris Burton
    [nickname] => chrisburton
    [urls] => Array
    [twitter] =>
    [website] =>

    [location] => New York
    [description] => Letterer & Typographer
    [image] =>

    [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

    # January 14, 2013 at 9:37 pm

    Do you know which value you need?

    # January 14, 2013 at 9:42 pm

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


    # January 14, 2013 at 10:08 pm

    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;

    # January 14, 2013 at 10:14 pm

    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

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

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

    // 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’ => $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;

    # January 14, 2013 at 10:17 pm

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

    # January 14, 2013 at 10:32 pm

    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()](, 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:

    < ?php
    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.

    # January 14, 2013 at 10:39 pm

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

    # January 14, 2013 at 11:51 pm

    I think [this]( will do what you want. Try it out (see demo at the bottom).

    # January 14, 2013 at 11:56 pm

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

    Maybe you will find it useful.

    # January 15, 2013 at 12:04 am

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

    @traq I’m receiving this:

    # January 15, 2013 at 12:10 am


    `$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.*

    # January 15, 2013 at 12:19 am

    @traq Yes, I am. `$response`

    Now I am getting

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

    Line 186: `$response = [`

    # January 15, 2013 at 12:23 am

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

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

    # January 15, 2013 at 12:24 am

    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)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".