Array contents and keys to different arrays [resolved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dan.lugg
Forum Newbie
Posts: 4
Joined: Thu Dec 03, 2009 4:15 pm

Array contents and keys to different arrays [resolved]

Post by dan.lugg »

Ahoy all.

Quick and simple one, what do you think the best method for solving this is?

I have an associative array, let's call it $person, with values such as;

Code: Select all

 
"name" => "Bob"
"sex" => "male"
"weight" => 185
     .
     .
     .
 
What would be the best way to dump the keys into a numerically indexed array, and the same with the values? resulting in $these_are_my_keys containing;

Code: Select all

 
0 => "name"
1 => "sex"
2 => "weight"
     .
     .
     .
 
and $these_are_my_values containing;

Code: Select all

 
0 => "Bob"
1 => "male"
2 => 185
     .
     .
     .
 
Thanks in advance :)
Last edited by dan.lugg on Thu Dec 03, 2009 4:30 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Array contents and keys to different arrays

Post by John Cartwright »

Code: Select all

$person = array(
   "name" => "Bob",
   "sex" => "male",
   "weight" => 185
);
 
$these_are_my_keys = array_keys($person);
$these_are_my_values = array_values($person);
 
:?:
dan.lugg
Forum Newbie
Posts: 4
Joined: Thu Dec 03, 2009 4:15 pm

Re: Array contents and keys to different arrays

Post by dan.lugg »

Thanks alot :)

I knew about array_keys() but neglected to read on about array_values(), silly me.

Thanks for the quick reply!
Post Reply