- This topic is empty.
-
AuthorPosts
-
January 14, 2013 at 6:28 pm #41983
chrisburton
ParticipantIs 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
)January 14, 2013 at 9:37 pm #120981__
ParticipantDo you know which value you need?
January 14, 2013 at 9:42 pm #120984chrisburton
Participant@traq Well, let’s say I need multiple values.
[id]
[screen_name]
[image]January 14, 2013 at 10:08 pm #120985chrisburton
ParticipantI 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 #120986__
ParticipantIf 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;
January 14, 2013 at 10:17 pm #120968chrisburton
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
January 14, 2013 at 10:32 pm #120969__
ParticipantIf 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.
January 14, 2013 at 10:39 pm #120971chrisburton
ParticipantI 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 #120957__
ParticipantI think [this](https://gist.github.com/4536162) will do what you want. Try it out (see demo at the bottom).
January 14, 2013 at 11:56 pm #120958hotpink
Member@chrisburton
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 #120959chrisburton
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/420t0i2a3v1BJanuary 15, 2013 at 12:10 am #120960__
Participant`$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 #120962chrisburton
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 = [`
January 15, 2013 at 12:23 am #120963__
Participant> 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 #120964__
ParticipantGet rid of my demo code below the class.
instead, use:
$cb = new twitter_user( $response );
(assuming `$response` is your response from twitter).
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.