Stuck accessing array with integer primary key in PHP

Stuck accessing array with integer primary key in PHP

alexharrisdcjalexharrisdcj Posts: 2Questions: 1Answers: 0

So, I've reached a sticking point in my little project. I'm trying to access the array properties, however I can't seem to access the array that contains the data that I need. Here's what the array looks like that I'm trying to get into...

 Array
 (
     [action] => remove
     [data] => Array
         (
             [110] => Array
                 (
                     [comp] => 4
                     [dept] => 301 
                     [emp] =>  658
                     [name] => "..."
                     [date] => 02/27/17
                     [clockin] => 1:54AM 
                     [clockout] => 08:30PM  
                     [hours] =>  8.6
                     [changedtime] => 2017-03-01 13:16:55
                     [changedaction] => NULL
                     [DT_RowId] => 110
                 )
 
         )
 
 )

I can access "$_POST['data'] fine, however I can't do "$_POST['data'][0]" to get into the actual array by index.

I feel like this is a stupid question and I'm missing something simple.

Any help/advice would be greatly appreciated.

Answers

  • alexharrisdcjalexharrisdcj Posts: 2Questions: 1Answers: 0

    It looks like I can get into it by using a foreach loop. Is this the typical procedure?

        $posteddata = $_POST['data'];
        foreach ($posteddata as &$value) {
            print $value['dept'];
        };
    
  • allanallan Posts: 63,356Questions: 1Answers: 10,444 Site admin

    Yes - exactly like that. It could effectively be seen as a spare array, but really its just an object key / value set, so foreach is exactly the right way to do it.

    Allan

This discussion has been closed.