Gandalf
Gandalf

Reputation: 13693

Applying a custom order to a multi-dimensional array

I have this array

$arr = array(
    'one' => array(
    'slidertitle' => 'lorem ipsum',
    'sliderlocation' => 'http://localhost/images/1.jpg',
    'sliderdescription' => 'this is a good lorem ipsum image',
    'sliderposition' => 1
    ),
    'two' => array(
    'slidertitle' => 'second slider',
    'sliderlocation' => 'http://localhost/images/2.jpg',
    'sliderdescription' => 'this space was reserved for a link source code here',
    'sliderposition' => 2
    ),
    'six' => array(
    'slidertitle' => 'sixth slider',
    'sliderlocation' => 'http://localhost/images/6.jpg',
    'sliderdescription' => 'this is the sixth slider,like,really!',
    'sliderposition' => 6
    )
);

which i need to look like this

$arr = array(

     'two' => array(
    'slidertitle' => 'second slider',
    'sliderlocation' => 'http://localhost/images/2.jpg',
    'sliderdescription' => 'this space was reserved for a link source code here',
    'sliderposition' => 2
    ),

    'six' => array(
    'slidertitle' => 'sixth slider',
    'sliderlocation' => 'http://localhost/images/6.jpg',
    'sliderdescription' => 'this is the sixth slider,like,really!',
    'sliderposition' => 6
    ),
      'one' => array(
    'slidertitle' => 'lorem ipsum',
    'sliderlocation' => 'http://localhost/images/1.jpg',
    'sliderdescription' => 'this is a good lorem ipsum image',
    'sliderposition' => 1
    )
);

I am attempting to do that by defining the expected array structure and introducing a dummy array.I then chunk the array and merge each chunk to the array format and i plan to finally unset the dummy and i am left with the array i want and in the order i want.

$arrayFormat = array(
   'dummy' => array(
    'slidertitle' => 'xxxx',
    'sliderlocation' => 'xxxxxxx',
    'sliderdescription' => 'xxxxxx',
    'sliderposition' => 0
    )
);
$arrayLength = count($arr);
$afterChunk = array_chunk($arr,$arrayLength);
$one = $afterChunk[0][0];
$two = $afterChunk[0][1];
$mergedArray = array_merge($arrayFormat,$one);
$secondMergedArray = array_merge($mergedArray,$two);
echo '<pre>';
print_r($secondMergedArray);
echo '</pre>';

The problem is array_chunk() does not include the key of the array so i am getting

Array (
    [dummy] => Array
        (
            [slidertitle] => xxxx
            [sliderlocation] => xxxxxxx
            [sliderdescription] => xxxxxx
            [sliderposition] => 0
        )

    [slidertitle] => second slider
    [sliderlocation] => http://localhost/images/2.jpg
    [sliderdescription] => this space was reserved for a link source code here
    [sliderposition] => 2 )

when i print_r($secondMergedArray);.is there something that can be done to array_chunk() to include the array key or is there any other array function that can help me get individual array inclusive of the key?.

Upvotes: 1

Views: 752

Answers (2)

SDC
SDC

Reputation: 14222

It's really hard to tell what you're wanting in terms of how to sort the elements. You've not been very clear in the question. There has to be something in the array that you know what order it needs to be.

In the absence of any clues as to what that is, I'm going to assume you want to specify the order of the array keys manually.

So, the current array is array('one'=>... , 'two'=>... , 'six'=>... ) and you want to sort those keys in an order you want to specify manually.

The solution is to use the uksort() function, along with a separate array specifying your sort order:

$arr = ... //input array as specified in the question
$sortOrder = array('two','one','six');

uksort($arr, function ($a, $b) use ($sortOrder) {
    $sortMe = array_flip($sortOrder);
    if ($sortMe[$a] == $sortMe[$b]) { return 0; }
    return ($sortMe[$a] < $sortMe[$b]) ? -1 : 1;
});

print_r($arr);

Outputs your array in 'two','one','six' order. Change the $sortOrder array as required.

Hope that helps.

Note: the syntax I've provided above only works in PHP 5.3 and above. (if you're using an older version, you need to upgrade)

Upvotes: 2

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

use uksort() for custom order for multidimensional array

http://php.net/manual/en/function.uksort.php

Upvotes: 1

Related Questions