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
}
11
  • 2
    er, why? There's probably a better way Commented Aug 24, 2012 at 3:20
  • Even it is possible, why do you need to do this? Commented Aug 24, 2012 at 3:21
  • If you care about the order of the items, use index which will point to an associative sub-array with your values: Array( [0]=>(Array ('first_name' => array '...')) Commented Aug 24, 2012 at 3:21
  • Why don't you just change the way you make the array? If you are pulling it out of a database, just change the order of the SQL that gets it, your assoc array will pop out correctly. Commented Aug 24, 2012 at 3:24
  • Ahhh- I should add that I have no control of this array... Commented Aug 24, 2012 at 3:25

2 Answers 2

1

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

8 Comments

thanks for this however is it possible to make it more dynamic? For example your compact function has fixed keys. Is there a way to get the keys first then pass it into compact for the reorder?
maybe, but won't you already have to know the keys, in order to know what order you want them to be in? as mentioned in the op, it could be better to approach this where the output will be and not in the source array.
the fields rank and score will always be there but the other fields can vary...unfortunately i can't control the output either, I can only sort it and pass it back as that is how filters work in Wordpress.
thanks again and it seems to have sorted the score before rank however, the value of the key score is NULL...so close...hehe..
could the value of score be NULL already? I added the custom_order() function, please update and test.
|
0

I'm not sure which is the order criteria but I guess one of this functions can help you. Take a look particularly to last three. You just have to create the appropriate comparison function

Comments

Your Answer

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