简体   繁体   中英

How do I print a specific value within a 2D array in PHP, that's not entirely 2D?

I have an array in PHP which is established from my database, which will be formatted as such:

[  "Folder1", 
   ["Content1", "Content2", "Content3"], 
   "Folder2", 
   ["Content1", "Content2", "Content3"]
]

I have the current code for this process

<?php

        $sql = ("SELECT FlashCardFolderName, FlashCardSetName FROM FlashCardFolders, FlashCardSets WHERE FlashCardFolderUserID = " . $_SESSION["id"] . " AND FlashCardSetFlashCardFolderID = FlashCardFolderID ORDER BY FlashCardFolderName");
        $result = $db->get_con()->query($sql);
        if($result->num_rows > 0){
            $temp = "";
            $foldersAndSets = array();
            $tempSet = array();
            while ($row = $result->fetch_assoc()){
                if($temp===$row["FlashCardFolderName"]){
                    array_push($tempSet, $row["FlashCardSetName"]);
                } else{
                    array_push($foldersAndSets, $tempSet);
                    $tempSet = array();
                    array_push($foldersAndSets, $row["FlashCardFolderName"]);
                    array_push($tempSet, $row["FlashCardSetName"]);;
                    $temp = $row["FlashCardFolderName"];
                }
            }
            array_push($foldersAndSets, $tempSet);
            array_shift($foldersAndSets);
            echo json_encode($foldersAndSets);
        } else{
            echo "<h6>Looks like there's nothing here...</h6>";
        }

        $length = sizeof($foldersAndSets);
        for ($i = 0; $i < $length; $i++){

            $secondDimension = sizeof($foldersAndSets[$i+1]);
            for($j = 0; $j < $foldersAndSets; $j++) {

            echo "$foldersAndSets[$i][$j+1]";
            }
        } 

    ?>

But it seems it's not working the way I want it to. Any ideas as to what I could be doing wrong?

First of all I'm suggestion to you to make a useful format of your input data, I mean it would be better to reformat your input array. For example, with structure like Folder -> array of Contents :

$reformatted = [];
$tmp = '';

foreach($foldersAndSets as $ind=>$txt){
    if ($ind % 2 == 0){          // if this is a 1-st, 3-rd, 5-th etc value -> Folders
        $reformatted[$txt] = [];
        $tmp = $txt;
    } else {                     // if this is a 2-nd, 4-th etc value -> Content
        $reformatted[$tmp] = $txt;
    }
} 

This will create an array like:

Array
(
    [Folder1] => Array
        (
            [0] => Content1
            [1] => Content2
            [2] => Content3
        )

    [Folder2] => Array
        (
            [0] => Content1
            [1] => Content2
            [2] => Content3
        )

)

With this array you can work in further. Now you can get any value you need with help of a simple foreach loop :

foreach($reformatted as $fold=>$cont){ 
        echo $fold.PHP_EOL.PHP_EOL;
    foreach($cont as $item){
        echo $item.PHP_EOL;
    }
    echo PHP_EOL;
}

Output:

Folder1

Content1
Content2
Content3

Folder2

Content1
Content2
Content3

Demo

Note: you can replace your second part(from $length = sizeof($foldersAndSets); and below) of code with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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