簡體   English   中英

遍歷平面數組並在PHP中構建json

[英]Iterating over flat array and building json in PHP

我有一個扁平的文件數組,這些文件是根據目錄結構動態構建的,可以在此處看到此類輸出的示例。

Array (
    [0] => filename-in-root.png
    [1] => another-filename-in-root.png
    [directory.subdirectory.0] => logo-of-some-sort.jpeg
    [directory.subdirectory.1] => image-of-queen.jpg
    [anotherdirectory.0] => a-screenshot-png
    [anotherdir.0] => a-lovely-picture.png
    [nextdirectory.0] => me-giving-the-bird.png
)

然后,即時通訊正在將這個輸出饋送到另一個函數以產生json輸出,我可以將其插入到我的javascript組件中。

它對於根目錄中的文件非常有用,並且如果目錄/子目錄中只有1個文件,但是將多個文件添加到目錄和子目錄時,將導致目錄結構再次輸出,如下所示;

[
    [0,"/","/",false,1],
    [1,"/filename-in-root.png","filename-in-root.png",true,1],
    [2,"/another-filename-in-root.png","another-filename-in-root.png",true,1],
    [3,"/directory","Directory",false,1],
    [4,"/directory/subdirectory","Subdirectory",false,2],
    [5,"/directory/subdirectory/logo-of-some-sort.jpeg","logo-of-some-sort.jpeg",true,3],
    [6,"/directory","Directory",false,1],
    [7,"/directory/subdirectory","Subdirectory",false,2],
    [8,"/directory/subdirectory/image-of-queen.jpg","image-of-queen.jpg",true,3],
    and so on.........
]

如您所見,第6項和第7項不應該存在,因為第8項已經在正確的目錄中標記了,末尾的數字表示應該有多少個縮進。

im用於生成此代碼的代碼如下:

function generateJson($flatArray)
{
    $arrayId = 1;
    $final_array = [[0, '/', '/', false, 1]];
    ksort($flatArray, SORT_STRING);

    foreach ($flatArray as $key => $val) {
        $exploded_key = explode('.', $key);
        foreach ($exploded_key as $k => $v) {
            if ($k == sizeof($exploded_key)-1) {
                $count = 1;
                for ($i = 0; $i < count(array_slice($exploded_key, 0, $k)); $i++) {
                    $count++;
                }
                $final_array[] = [$arrayId, '/' . join('/', array_slice($exploded_key, 0, $k)) . '/' . $val, $val, true, $count];
                $arrayId++;
            } else {
                $count = 1;
                for ($i = 0; $i < count(array_slice($exploded_key, 0, $k)); $i++) {
                    $count++;
                }
                $final_array[] = [$arrayId, '/' . join('/', array_slice($exploded_key, 0, $k + 1)), ucfirst($v), false, $count];
                $arrayId++;
            }
        }
    }

    return json_encode($final_array);
}

我對此非常執着...任何幫助將不勝感激。

在將任何元素附加到$final_array (即json_encode d)之前,我們可以檢查它是否已經存在以避免重復。

我們也可以減少代碼中的重復:

function generateJson($flatArray)
{
    $arrayId = 1;
    $final_array = [[0, '/', '/', false, 1]];
    $final_paths = []; // New array to store paths
    ksort($flatArray, SORT_STRING);
    foreach ($flatArray as $key => $val) {
        $exploded_key = explode('.', $key);
        foreach ($exploded_key as $k => $v) {
            $count = 1;
            // Changes start from here (in addition to initializing $final_paths array above)
            $end = count(array_slice($exploded_key, 0, $k));
            for ($i = 0; $i < $end; $i++) {
                $count++;
            }
            // The only statement changed is wrapped with the if-statement
            if ($k == sizeof($exploded_key)-1) {
                $new_element = [$arrayId, '/' . join('/', array_slice($exploded_key, 0, $k)) . '/' . $val, $val, true, $count];
            } else {
                $new_element = [$arrayId, '/' . join('/', array_slice($exploded_key, 0, $k + 1)), ucfirst($v), false, $count];
            }
            // *This is it*: append the element ONLY IF it is not in the array
            if(!in_array($new_element[1], $final_paths)) {
                $final_paths[] = $new_element[1];
                $final_array[] = $new_element;
                $arrayId++;
            }
        }
    }
    return json_encode($final_array);
}

也許不是最佳的解決方案(從運行時的角度來看),但它應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM