Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous asked in Computers & InternetProgramming & Design · 1 month ago

How do I use looping for segments of a value in multi dimensional arrays PHP?

I have been trying to figure out how to approach this problem (attached in the picture) but am not sure how to start considering I need to use parts of one value to find parts of other values in the array.  Any help would be greatly appreciated.

Attachment image

1 Answer

  • Chris
    Lv 7
    1 month ago
    Favorite Answer

    When you iterate over a suitable array of employees, you can check if the current employee satisfies the condition, then proceed accordingly. For instance *if* their lastname is "L", push their salary into the $avg_salary[] array.

    You can do this for all four parts a. to d. inside a single loop.

    Once the loops ends, you can use the intermediate arrays to calculate the average, or output the phone numbers, etc.

    Also, I don't think you necessarily need to use "name" => "John Doe" in your array items; I don't see why you can't use "fname" => "John", "lname" => "Doe" instead. This makes it easier to test the current employee.

Still have questions? Get your answers by asking now.