Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse multidimensional JSON array without key

I am trying to display the different provinces including their shops, which are inside a multidimensional array, on a webpage, but I can't manage to display the nested arrays that lack a key.

I've tried giving the array a key, tried "parse.Land.0", "parse.Land.i" and "parse.Land[0]" to get the information, but none of it seems to work. In the case of "parse.Land[0]", it would return the first letter of the country's name instead of the first result in the nested array. We've also tried to construct the array in several different ways, but none of it worked.

PHP Code to get the information from a database:

$Land = ['Land' => $_GET['data']];

$Nederland = array("Groningen", "Friesland", "Drenthe", "Overijssel", "Flevoland", "Gelderland", "Utrecht", "Noord-Holland", "Zuid-Holland", "Zeeland", "Noord-Brabant", "Limburg");
$Andere = array("Etc", "Etcetera");
switch ($_GET['data']) {
    case "Nederland":
        $country = $Nederland;
        $fieldID = 5;
        break;
    case "Andere":
        $country = $Andere;
        $fieldID = 18;
        break; 
}
    foreach($country as $data) {
        $locationData = [];
        $getProvince= $_db->prepare("SELECT user_id, value FROM wp_bp_xprofile_data WHERE field_id = ".$fieldID." AND value = '".$data."'");
        $getProvince->execute();

        $displayProvince = $getProvince->fetchAll(PDO::FETCH_ASSOC);
        foreach($displayProvince as $dataProvince) {
           $getName= $_db->prepare("SELECT value FROM wp_bp_xprofile_data WHERE field_id = 1 AND user_id = ".$dataProvince['user_id']);
           $getName->execute();
           $displayName = $getName->fetchAll(PDO::FETCH_ASSOC);
           foreach($displayName as $dataName) {
               $locationData[] = ['Info' => $dataName['value']];
           }
        }
        $Land[] = [$data => $locationData];
    }

echo json_encode($Land);

PHP code results to this JSON array:

{"Land":"Nederland","0":{"Groningen":[{"Info":""},{"Info":""}]},"1":{"Friesland":[]},"2":{"Drenthe":[]},"3":{"Overijssel":[]},"4":{"Flevoland":[]},"5":{"Gelderland":[]},"6":{"Utrecht":[]},"7":{"Noord-Holland":[{"Info":"Placeholder"},{"Info":"Test"},{"Info":"Anna Paulowna"},{"Info":"Den Helder"}]},"8":{"Zuid-Holland":[{"Info":"zuidholland"},{"Info":"zuidholland2"}]},"9":{"Zeeland":[{"Info":"Middelburg"}]},"10":{"Noord-Brabant":[]},"11":{"Limburg":[]}}

Ajax to display the information on a webpage:

function getData(land) {
        $.ajax({
            type: 'GET',
            url: '/GetData.php',
            data: {data: land},
            success: function (data) {

                var parse = JSON.parse(data);
                var object = Object.keys(parse).length;
                var element = document.getElementById('content');
                for (var i = 0; i < object; i++) {
                    var mainDiv = document.createElement('div');
                    mainDiv.setAttribute('class', 'accordion_holder');
                    element.appendChild(mainDiv);
                    var accordion = document.createElement('button');
                    accordion.setAttribute('class', 'accordion');
                    mainDiv.appendChild(accordion);
                    var icon = document.createElement('i');
                    icon.setAttribute('class', 'fas fa-angle-right');
                    accordion.appendChild(icon);
                    accordion.insertAdjacentHTML('afterbegin', parse.Land);
                }
                console.log(parse);
            }
        });

    var element = document.getElementById(land);

    element.classList.add('active');

    }

I want to be able to get the value of the inner array with the key "Info". Instead I either get an error saying that getData() is undefined, or just the first letter of the country. I expected to be able to do something along the lines of "parse.Land.7.Noord-Holland.Info" and for it to return the value "Placeholder".

Any help would be appreciated.

like image 638
X. Tamis Avatar asked May 28 '26 20:05

X. Tamis


1 Answers

parse[0] will compute to

{"Groningen":[{"Info":""},{"Info":""}]}

when you print this it will be [object Object], if you need the first element then

Object.keys(parse[0])[0]

should give you Groningen

like image 94
joyBlanks Avatar answered May 30 '26 09:05

joyBlanks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!