Home  >  Article  >  Backend Development  >  一个数组按照另一个数组元素顺序排序

一个数组按照另一个数组元素顺序排序

WBOY
WBOYOriginal
2016-06-06 20:07:262816browse

文字总是那么的苍白,上图
一个数组按照另一个数组元素顺序排序

第一个数组是拿到的id数组,且已经是排序好的
但是下面拿到的数据键值顺序已经乱了。在不使用foreach的情况下如何优雅的进行排序,结果和第一个数组顺序一样
想要得到的结果类似这样

<code class="php">    [
        131 => array(),
        89 => array(),
        141 => array(),
        43 => array(),
        140 => array(),
    ]</code>

回复内容:

文字总是那么的苍白,上图
一个数组按照另一个数组元素顺序排序

第一个数组是拿到的id数组,且已经是排序好的
但是下面拿到的数据键值顺序已经乱了。在不使用foreach的情况下如何优雅的进行排序,结果和第一个数组顺序一样
想要得到的结果类似这样

<code class="php">    [
        131 => array(),
        89 => array(),
        141 => array(),
        43 => array(),
        140 => array(),
    ]</code>

用foreach不是很好吗,再说view里面不就是用foreach遍历么?你可以不处理的。例

<code>$this->view->assign('indexArray', $indexArray);
$this->view->assign('dataArray', $dataArray);
//--view
foreach ($indexArray as $id) {
    print_r($dataArray[$id]); //对不对?
}</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn