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 8433677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:27:15+00:00 2026-06-10T06:27:15+00:00

I am trying to figure out how I can move an array element to

  • 0

I am trying to figure out how I can move an array element to another spot. Is this possible?

Here is my example the var_dump array:

array
     'person' =>
       array
            'first_name' =>
              array
                   '...'
            'last_name' =>
              array
                   '...'
            'rank' =>
              array
                   '...'
            'score' =>
              array
                   '...'
            'item' =>
              array
                   '...'
     'work' =>
       array
            'company' =>
              array
                   '...'
            'phone' =>
              array
                   '...'

And of course there are values in the ‘…’, but just to simplify it. So I need to move “score” before “rank”, so the output will show score first before rank, is that possible?

Now I know the array push/pop/shift/unshift but none of those would help me here I think.

Please note, I have no control of this array…I am receiving it as is…

basically it is coming from a WordPress plugin and it has a filter for these fields so I am using this to catch it.

add_filters( 'work_rank_fields', 'custom_order');
function custom_order($fields) {
 var_dump($fields); //what you see on top
}
  • 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-10T06:27:17+00:00Added an answer on June 10, 2026 at 6:27 am

    Using a sample array like you gave us, you could try something like this.

    $sample = array(
      'person' => array(
        'first_name' => array('first'),
        'last_name' => array('last'),
        'rank' => array('rank'),
        'score' => array('score'),
        'item' => array('item')
        ),
      'work' => array(
        'company' => array('company'),
        'phone' => array('phone')
        )
      );
    
    function reorder_person( $sample )
    {
      extract( $sample['person'] );
      // the desired order below for the keys
      $sample['person'] = compact('first_name','last_name','score','rank','item');
      return $sample;
    }
    
    $sample = reorder_person( $sample );
    

    Now your var_dump of $sample should display score before rank

    array(2) {
      'person' =>
      array(5) {
        'first_name' =>
        array(1) {
          [0] =>
          string(5) "first"
        }
        'last_name' =>
        array(1) {
          [0] =>
          string(4) "last"
        }
        'score' =>
        array(1) {
          [0] =>
          string(5) "score"
        }
        'rank' =>
        array(1) {
          [0] =>
          string(4) "rank"
        }
        'item' =>
        array(1) {
          [0] =>
          string(4) "item"
        }
      }
      'work' =>
      array(2) {
        'company' =>
        array(1) {
          [0] =>
          string(7) "company"
        }
        'phone' =>
        array(1) {
          [0] =>
          string(5) "phone"
        }
      }
    }
    

    A little clumsy but, your wordpress filter custom_order function then might look like:

    function custom_order( $fields ) {
    
      $a = array();
      foreach( $fields['person'] as $key => $value )
      {
        if ( $key == 'rank' ) continue; // wait until we get score first
    
        if ( $key == 'score' )
        {
          $a['score'] = $value; // add score first, then rank
          $a['rank']  = $fields['person']['rank'];
          continue;
        }
    
        $a[$key] = $value;
      }
    
      $fields['person'] = $a;
    
      return $fields;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to move to PDO can't figure out what is wrong with this $dbh
Trying to figure out how I can do this properly. The print_r looks like
I am trying to figure out how to move specified System.Windows.Forms.Form onto another than
I am trying to figure out how can I update the petevents table with
I trying to figure out how I can delete from multiple tables in SQL
I'm trying to figure out how I can listen to the Cancel button that
I'm trying to figure out why I can't I use super in a generic
I am trying to figure out how I can tell, in MATLAB, how many
I'm trying to figure out how I can download a particular tag of a
I am trying to figure out how I can update my sql query dynamically.

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.