Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers 3

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

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

share|improve this answer
    
+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 '09 at 16:16
    
@Gumbo: That'd just be a minor fix, though... to something like $indexes = array_keys($whatever['items'][0]); –  Powerlord May 6 '09 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 '09 at 16:41
add comment
$result = array_keys($input['items'][0]);
share|improve this answer
add comment
foreach($items[0] as $idx => $val)
   {
   $indexes[] = $idx;
   }

Or:

$indexes = array_keys($items[0]);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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