1

我有这个数组

$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
    )
);

我需要看起来像这样

$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
    )
);

我试图通过定义预期的数组结构并引入一个虚拟数组来做到这一点。然后我将数组分块并将每个块合并到数组格式中,我计划最终取消设置虚拟对象,我只剩下我想要的数组了我想要的顺序。

$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>';

问题是array_chunk()不包括数组的键,所以我得到

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 )

什么时候我print_r($secondMergedArray);可以做些什么array_chunk()来包含数组键,或者是否有任何其他数组函数可以帮助我获得包含键的单个数组?

4

2 回答 2

2

就如何对元素进行排序而言,很难说出您想要什么。你这个问题不是很清楚。数组中必须有一些你知道它需要什么顺序的东西。

在没有任何线索的情况下,我将假设您想要手动指定数组键的顺序。

因此,当前数组是array('one'=>... , 'two'=>... , 'six'=>... )并且您希望按照您想要手动指定的顺序对这些键进行排序。

解决方案是使用该uksort()函数以及指定排序顺序的单独数组:

$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);

以“二”、“一”、“六”的顺序输出您的数组。根据需要更改$sortOrder阵列。

希望有帮助。

注意:我上面提供的语法仅适用于 PHP 5.3 及更高版本。(如果您使用的是旧版本,则需要升级)

于 2012-09-18T16:37:01.260 回答
1

用于uksort()多维数组的自定义顺序

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

于 2012-09-18T13:00:04.913 回答