0

Hello i'm using PHP and I'm stuck trying to re-order an arrays with an array as per below i.e. I want contents of key[0] to move to key [1] and the contents of key [1] to move to key [0]. Anyone able to provide some PHP code for this ?

****Current Array containing Arrays****

Array([0] => Array (
          [name] => 1.jpeg, 
          [type] => image/jpeg, 
          [tmp_name] => C:\fakefolder\fakename1.tmp, 
          [error] => 0, 
          [size] => 1000
         ),
      [1] => Array (
          [name] => 2.jpeg, 
          [type] => image/jpeg, 
          [tmp_name] => C:\fakefolder\fakename2.tmp, 
          [error] => 0, 
          [size] => 2000
        )
     )

****Want to re-order arrays within the array like this****

Array( [0] => Array (
         [name] => 2.jpeg,
         [type] => image/jpeg,
         [tmp_name] => C:\fakefolder\fakename2.tmp,
         [error] => 0,
         [size] => 2000
       ),
      [1] => Array ( 
         [name] => 2.jpeg,
         [type] => image/jpeg, 
         [tmp_name] => C:\fakefolder\fakename2.tmp, 
         [error] => 0, 
         [size] => 2000
      )
   )
8
  • And what's the point in this? Commented Dec 13, 2019 at 18:22
  • 1
    You could do [$array[0], $array[1]] = [$array[1], $array[0]]; Commented Dec 13, 2019 at 18:24
  • array_reverse($arr, true); Commented Dec 13, 2019 at 18:29
  • 1
    What if there are 3 items in the array? Commented Dec 13, 2019 at 18:37
  • See this can help you. stackoverflow.com/questions/11282592/… Commented Dec 13, 2019 at 18:49

1 Answer 1

0

It's not clear from your question if you simply want to reverse the array key order? If so, you can just use array_reverse() for this.

Alternatively, if there's a pair of array keys you're trying to specifically swap, here's a function you can use that'll do exactly that for you:

/**
 * Swaps two array keys over in the provided array
 *
 * @param array $array
 * @param mixed $keyA
 * @param mixed $keyB
 */
function swapArrayKeys(array &$array, $keyA, $keyB)
{
    if (!isset($array[$keyA], $array[$keyB])) {
        throw new InvalidArgumentException('One or more keys are missing from your array');
    }

    $a = $array[$keyA];

    $array[$keyA] = $array[$keyB];
    $array[$keyB] = $a;
}

Example usage:

<?php

$inputArray = [
    [
        'name' => '1.jpeg',
        'type' => 'image/jpeg',
        'tmp_name' => 'C:\fakefolder\fakename1.tmp',
        'error' => 0,
        'size' => 1000
    ],
    [
        'name' => '2.jpeg',
        'type' => 'image/jpeg',
        'tmp_name' => 'C:\fakefolder\fakename2.tmp',
        'error' => 0,
        'size' => 2000
    ],
    [
        'name' => '3.jpeg',
        'type' => 'image/jpeg',
        'tmp_name' => 'C:\fakefolder\fakename3.tmp',
        'error' => 0,
        'size' => 3000
    ],
];

// Swap elements 1 and 3 in the array (keys 0 and 2 due to arrays being zero indexed)
swapArrayKeys($inputArray, 0, 2);

// Output the new array
print_r($inputArray);

/**
 * Swaps two array keys over in the provided array
 *
 * @param array $array
 * @param mixed $keyA
 * @param mixed $keyB
 */
function swapArrayKeys(array &$array, $keyA, $keyB)
{
    if (!isset($array[$keyA], $array[$keyB])) {
        throw new InvalidArgumentException('One or more keys are missing from your array');
    }

    $a = $array[$keyA];

    $array[$keyA] = $array[$keyB];
    $array[$keyB] = $a;
}

Console output:

Array
(
    [0] => Array
        (
            [name] => 3.jpeg
            [type] => image/jpeg
            [tmp_name] => C:\fakefolder\fakename3.tmp
            [error] => 0
            [size] => 3000
        )

    [1] => Array
        (
            [name] => 2.jpeg
            [type] => image/jpeg
            [tmp_name] => C:\fakefolder\fakename2.tmp
            [error] => 0
            [size] => 2000
        )

    [2] => Array
        (
            [name] => 1.jpeg
            [type] => image/jpeg
            [tmp_name] => C:\fakefolder\fakename1.tmp
            [error] => 0
            [size] => 1000
        )

)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.