0

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?

1
  • 3
    You can't use [0] as no element exists with 0 as a key. You could try echo reset($arr)['wage']; Commented Sep 27, 2019 at 16:02

2 Answers 2

1

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'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

So without knowing the ID, I cannot use the position which is what I was trying to do?
How could I also print the current ID within this foreach loop please?
For that you should loop so: foreach ($employeeData as $key => $data). Just if you don't need thet index you can omit $key
1

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';

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.