简体   繁体   English

PHP 对无限数组进行排序并保留索引字符串

[英]PHP sort endless array and keep the index strings

I want o sort a multidimensional array (which I don't know the length) and keep the index strings.我想对多维数组(我不知道长度)进行排序并保留索引字符串。 The data of this multidimensional array is from a directory which can have unlimited folders & files.这个多维数组的数据来自一个可以有无限文件夹和文件的目录。

I have successfully grabbed the folder structure into an array using this List all the files and folders in a Directory with PHP recursive function and I can sort the array with a recursive function like this https://stackoverflow.com/a/4501406/3355243 . I have successfully grabbed the folder structure into an array using this List all the files and folders in a Directory with PHP recursive function and I can sort the array with a recursive function like this https://stackoverflow.com/a/4501406/3355243 .

Problem问题

When sorting, all the array indexs are replaced with numbers.排序时,所有数组索引都替换为数字。 I'd like to keep the array index as strings because it holds the folder name.我想将数组索引保留为字符串,因为它包含文件夹名称。

Input输入

Folder A => Array
(
    file.php,
    file1.php,
    Folder B => Array 
    (
        file.php,
        file1.php, 
        Folder A => Array 
        (
            file.php,
            file1.php, 
        )
        something.php
    ),
    something.php
),
Folder B => Array 
(
    other.php,
    other2.php 
),
Folder C => Array 
(
    Folder A => Array 
    (
        a.php,
        b.php
    ),
    something.php
)

Expected output预期 output

Folder A => Array
(
    file.php,
    file1.php,
    something.php,
    Folder B => Array 
    (
        file.php,
        file1.php, 
        something.php,
        Folder A => Array 
        (
            file.php,
            file1.php, 
        )       
    ),  
),
Folder B => Array 
(
    other.php,
    other2.php 
),
Folder C => Array 
(
    something.php
    Folder A => Array 
    (
        a.php,
        b.php
    ),  
)

Current output当前 output

0 => Array
(
    file.php,
    file1.php,
    something.php,
    0 => Array 
    (
        file.php,
        file1.php, 
        something.php,
        0 => Array 
        (
            file.php,
            file1.php, 
        )       
    ),  
),
1 => Array 
(
    other.php,
    other2.php 
),
2 => Array 
(
    something.php
    0 => Array 
    (
        a.php,
        b.php
    ),  
)

Final goal最终目标

To show the array / folder structure into a select box with Groups & Options.将数组/文件夹结构显示到带有组和选项的 select 框中。

在此处输入图像描述

Solved .解决了

The solution of the link Order multidimensional array recursively at each level in PHP uses ksort to order the array and the solution, in my case, I had to use the asort . 在 PHP 中的每个级别递归排序多维数组链接的解决方案使用ksort对数组和解决方案进行排序,在我的情况下,我不得不使用asort

https://www.php.net/manual/en/function.asort.php https://www.php.net/manual/en/function.asort.php

asort — Sort an array and maintain index association asort — 对数组进行排序并保持索引关联

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM