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

  • SEARCH
  • Home
  • 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 7537935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:58:55+00:00 2026-05-30T06:58:55+00:00

lets say we have the following array $myArray = array( foo => value1 ,

  • 0

lets say we have the following array

$myArray = array( 
                 "foo" => "value1"    , 
                "bar" => "value2"    , 
                "baz" => "value3"    ,
                "qux" => "value4"
    ); 

When looping through this and echoing the value for each one its always going to print foo first, bar second, baz third, and qux fourth. Lets say I want to show the value of qux after foo. What is the best way to do this. I was trying all the different usort,uasort, uksort and wasn’t getting what I wanted.

I saw a lot of good code for moving an index to the top using splice and what not, but I haven’t found the best way to say, how about lets move this index before this other index in the associative array, so that when we loop through it we hit the index we want before the other one.

  • 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-05-30T06:58:56+00:00Added an answer on May 30, 2026 at 6:58 am

    Not very elegant but it works:

    $arr = array( 
        "foo" => "value1"    , 
        "bar" => "value2"    , 
        "baz" => "value3"    ,
        "qux" => "value4"
    ); 
    
    $tmp = array();
    foreach($arr as $k=>$v) {
        $tmp[] = array($k=>$v);;
    }
    
    array_splice($tmp, 1, 0, array_slice($tmp, 3));
    array_splice($tmp, -1);
    $arr = array();
    foreach($tmp as $k => $v) {
        foreach($v as $k2 => $v2) {
            $arr[$k2] = $v2;
        }
    }
    
    print_r($arr);
    

    output:

    Array
    (
        [foo] => value1
        [qux] => value4
        [bar] => value2
        [baz] => value3
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have the following array in JavaScript: var skins = new Array('Light',
Lets say I have an array numbers that contains the following values: int numbers
Lets say I have the following: public class MyObject { [Bindable] public var foo:int
Lets say I have the following array: my_array = [1, 5, 8, 11, -6]
Lets say i have the following cstring char array[1000]; How i can convert it
Lets say I have the following code. double *return_array(void) { double foo[2]; foo[0] =
Lets say I have the following two PHP arrays that contain integers: $foo =
Lets say we have the following two class definitions. #include <iostream> #include <array> class
Lets say I have the following array: Array ( [1284487200] => Array ( [title]
What I want to do is following. Lets say we have array like that

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.