vote up 1 vote down star

OK here is what I would like to do. I have an array. What i want to do is make another array of the index values from the first one. Take the below I want to create and array from this :

   Array ( 
    [identifier] => ID 
    [label] => HouseNum 
    [items] => Array ( 
                      [0] => Array ( 
                                    [ID] => 1 
                                    [HouseNum] => 17  
                                    [AptNum] => 
                                    [Street] => birch glen dr 
                                    [City] => Clifton Park 
                                    [State] => NY [Zip5] =>   
                                    [EID] => E083223 
                                    [RequestDate] => 02/05/09 
                                    [Status] => In-Qeue 
                                    [DateCompleted] => 
                                    [CompletedBy] =>  
                                    [ContactName] => Suzy Q 
                                    [ContactNumber] => 555-867-5309 
                                    [ContactTime] => 9-9 )
                       )
    );

That will end up looking like this :

Array(
      [0] => [ID] 
      [1] => [HouseNum] 
      [2] => [AptNum]
      [3] => [Street] 
      [4] => [City]
      [5] => [State] 
      [6] => [Zip5]  
      [7] => [EID] 
      [8] => [RequestDate]
      [9] => [Status]
      [10] => [DateCompleted]
      [11] => [CompletedBy]  
      [12] => [ContactName] 
      [13] => [ContactNumber]
      [14] => [ContactTime]
      );

Any thoughts on how to achieve this ? I mostly need to know how to get just the index values.

flag

3 Answers

vote up 9 vote down check
$indexes = array_keys($whatever['items'][0]);

http://us.php.net/manual/en/function.array-keys.php

link|flag
+1 for using a native function. But one little mistake: items is just an element of the given variable and not the variable itself. – Gumbo May 6 at 16:16
@Gumbo: That'd just be a minor fix, though... to something like $indexes = array_keys($whatever['items'][0]); – R. Bemrose May 6 at 16:21
Thank you very much ^_^ of course this will teach me to not delve into the manual a little deeper. – Arasoi May 6 at 16:41
vote up 0 vote down
foreach($items[0] as $idx => $val)
   {
   $indexes[] = $idx;
   }

Or:

$indexes = array_keys($items[0]);
link|flag
vote up 0 vote down
$result = array_keys($input['items'][0]);
link|flag

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.