Php 将自定义顺序应用于多维数组
我有这个数组Php 将自定义顺序应用于多维数组,php,Php,我有这个数组 $arr = array( 'one' => array( 'slidertitle' => 'lorem ipsum', 'sliderlocation' => 'http://localhost/images/1.jpg', 'sliderdescription' => 'this is a good lorem ipsum image', 'sliderposition' => 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>';
当我打印时($secondMergedArray)
。是否可以对array\u chunk()
执行一些操作以包含数组键,或者是否有任何其他数组函数可以帮助我获取包含该键的单个数组?对于多维数组的自定义顺序,请使用uksort()
很难说出您想要的元素排序方式。你对这个问题不是很清楚。数组中必须有某种东西,你知道它的顺序 在没有任何关于这是什么的线索的情况下,我假设您想要手动指定数组键的顺序 因此,当前数组是
array('one'=>…,'two'=>…,'six'=>…)
,您希望按照手动指定的顺序对这些键进行排序
解决方案是使用uksort()
函数以及指定排序顺序的单独数组:
$arr=//问题中指定的输入数组
$sortOrder=array('two','one','six');
uksort($arr,函数($a,$b)use($sortOrder){
$sortMe=array\u flip($sortOrder);
if($sortMe[$a]==$sortMe[$b]){返回0;}
返回($sortMe[$a]<$sortMe[$b])?-1:1;
});
印刷费($arr);
按“二”、“一”、“六”顺序输出数组。根据需要更改$sortOrder
数组
希望有帮助
注意:我上面提供的语法只适用于PHP5.3及以上版本。(如果您使用的是较旧的版本,则需要升级)因此您希望根据键
滑块对阵列进行排序
?我可能没有看到,但前后有什么区别?(除了上帝知道的排序)排序顺序是什么?在你想要的输出中是否有一个序列?或者它只是一个任意洗牌?@JvdBerg在第一个数组中,键的顺序是1、2、6,在第二个数组中是2、6、1。@SDC我没有像asc或desc这样的排序顺序,也不是任意洗牌。我想确定什么数组出现在什么索引中。像这样模糊(低值)的提示最好放在问题下面作为注释。
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 )
$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);