Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8137507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:08:56+00:00 2026-06-06T11:08:56+00:00

I have an array with academic years: $arrayAcademicYears = array( array(8889, 1988-1989), array(8990, 1989-1990),

  • 0

I have an array with academic years:

$arrayAcademicYears = array( 
           array("8889", "1988-1989"),
           array("8990", "1989-1990"),
           array("9091", "1990-1991"),
           array("0405", "2004-2005"),
           array("1112", "2011-2012"),
           array("1213", "2012-2013")
         );
         foreach($arrayAcademicYears as $k => $v) {
             echo "KEY: ";
             echo $v[0];
             echo " -> VALUE: ";
             echo $v[1];
             echo "<br>";
         }

This prints:

KEY: 8889 -> VALUE: 1988-1989
KEY: 8990 -> VALUE: 1989-1990
KEY: 9091 -> VALUE: 1990-1991
KEY: 0405 -> VALUE: 2004-2005
KEY: 1112 -> VALUE: 2011-2012
KEY: 1213 -> VALUE: 2012-2013

Then I have the current academic year in a variable like this:

$currentAcademicYear="1112";

How can I change the code to print:

KEY: 1112 -> VALUE: 2011-2012
KEY: 1213 -> VALUE: 2012-2013
KEY: 0405 -> VALUE: 2004-2005
KEY: 9091 -> VALUE: 1990-1991
KEY: 8990 -> VALUE: 1989-1990
KEY: 8889 -> VALUE: 1988-1989

Please note that this is just an example, I’m not asking for a solution to any kind of academic course exercise. Just a self taught PHP learner here.

Thanks a lot! (.. for helping me becoming a better PHP programmer :))

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:08:59+00:00Added an answer on June 6, 2026 at 11:08 am

    This will reverse your array, displaying the newest first:

    $arrayAcademicYears = array_reverse($arrayAcademicYears);
    

    Place it just above:

    foreach($arrayAcademicYears as $k => $v) {
    

    Array_reverse will for some reason change the key’s on all your items to 0 increment, except for the 04-05 year. Use the following function which is updated and will achieve the correct sorting.

        function array_mtf(&$arr, $keytom, $reverse = false){
            $tmp = array();
            foreach($arr as $key => $value){
                if($key == $keytom){
                    $tmp[$key] = $value;
                    unset($arr[$key]);
                }
            }
    
                        if($reverse)
                arsort($arr);
    
            $arr = $tmp + $arr;
        }
    

    Usage would be as:

    array_mtf($array_to_sort, $key_to_move_first, true);    
    

    So add the function block to your code, and then replace:

    $arrayAcademicYears = array_reverse($arrayAcademicYears);
    

    With

    array_mtf($arrayAcademicYears, $currentAcademicYear);   
    

    The resulting array would look like:

    Array
    (
    [1112] => 2011-2012
    [1213] => 2012-2013
    [0405] => 2004-2005
    [9091] => 1990-1991
    [8990] => 1989-1990
    [8889] => 1988-1989
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array INPUTFILES with n files INPUTFILES=( file_0 ... files_n-1 ) And i
I have array of hashes: @array = [{:id => 1, :status=>R}, {:id => 1,
I have array of select tag. <select id='uniqueID' name=status> <option value=1>Present</option> <option value=2>Absent</option> </select>
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have array of strings, String[] data and it's 10 elements has value P
Say we have array of arrays: tree_limbs = Array.new tree_limbs << %w(1 2 3
When I have array of objects, when I type the name of variable in
if i have array array[0] = jack; array[1] = jill; array[2] = lisa; array[2]
The problem is like this: I have an array of 500 pointers which point

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.