Tyler
Tyler

Reputation: 874

Reading Multidimensional PHP Array

I've ceated a PHP Array which output looks like the following:

Array (
    [1429645] => Array ( 
        [name]          => John Smith
        [days_employed] => 15
        [wage]          => 25000
    )
    [1183240] => Array ( 
        [name]          => Sarah Smith
        [days_employed] => 65
        [wage]          => 30000
    )
)

I am going to be looping through the data to create a table, however I'm trying to understand what I have and print some data of like so;

<?PHP echo print_r($employeeData[0][wage]); ?>

The above intent was; first employee => wage => value

After several attempts, several dozen pages being looked at, trying with and without speech emphasis, nothing appears to have given any output apart from at one point I saw 1 dispite that not being a value I could relate to.

Have I created a standard PHP array and how can I read values correctly?

Upvotes: 0

Views: 44

Answers (2)

Dave
Dave

Reputation: 5191

Your employee number, from the looks at it, is your key.

$employees[1429645] = ['name'          => 'John Smith' ,
                       'days_employed' => 15 ,
                       'wage'          => 25000];

foreach ($employees as $details) {

    echo $details['name'] . ' gets paid ' . 
         $details['wage'] . ' and has been employed for ' . 
         $details['days_employed'] . ' days';

}

Upvotes: 1

ambrous
ambrous

Reputation: 194

Tim!

You don't have zero index in your array, this must work:

<?php echo $employeeData[1429645]['wage']; ?>

You can null indeces of your array by $employeeData = array_values($employeeData); and then you could use your current code.

But more correct would be so:

foreach ($employeeData as $data) {
        echo $data['wage'];
}

Upvotes: 1

Related Questions