Page 1 of 1

Array contents and keys to different arrays [resolved]

Posted: Thu Dec 03, 2009 4:24 pm
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 :)

Re: Array contents and keys to different arrays

Posted: Thu Dec 03, 2009 4:28 pm
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);
 
:?:

Re: Array contents and keys to different arrays

Posted: Thu Dec 03, 2009 4:29 pm
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!