Forum

Thread tagged as: Problem

Issue with Jsonencode - Perch collection

Hi there ,

i have been trying to use jquery's autocomplete function for a website . As a result , i tried to create an array of locations from the xml using the below code .

global $result;
$xml=simplexml_load_file("https://shades.mycrm.com/feed/privatesite/0744a0be5fac33c3b3e69c6b26e97d1e") or die("Error: Cannot create object");
foreach($xml->children() as $propertylisting) { 
    $location[] = $propertylisting->community->__toString(); 
}
$result1 = array_unique($location);
$result = array_values($result1);

==========================================

  <script>
    var availableTags = <?php echo json_encode($result); ?>;
    $( "#search" ).autocomplete({
      source: availableTags
    });
  </script>

The above code works perfectly fine with no issues at all . When i try the same via Perch collection , it doesn't generate a similar array and as a result the json_encode , fails .

PFB the code :

<?php
global $result;
$array[] = perch_collection('property', [
    'skip-template' => true,
]);

foreach($array as $item){
    foreach ($item as $items){
        $location[] = $items['sub-location'];
    }
}
$result = array_unique($location);
echo json_encode($result);
?>

What am i missing here ?

Also PFA the result of json encode for both codes .

From XML = >

["Palm Jumeirah","Downtown Dubai","DAMAC Hills (Akoya by DAMAC)","Serena","Jumeirah Lake Towers","Dubai South (Dubai World Central)","Town Square","Dubai Marina","Business Bay","Deira","Reem","Al Taawun","Dragon City","Al Furjan","Khalifa City","Old Town","DIFC","Al Khail Heights","Jumeirah Park","Culture Village","International City","Jumeirah Village Circle","Al Quoz","Jumeirah Golf Estates","Dubai Production City (IMPZ)","Dubai Industrial Park","Al Badaa","Karama","Bur Dubai","Dubai Sports City","Muroor Area","Al Rahba","Sheikh Zayed Road","Mohammed Bin Rashid City","Yas Island","Al Raha Beach"]     

From Perch Collection Method

 {"0":"Palm Jumeirah","1":"International City","2":"Jumeirah Village Circle","3":"DIFC","6":"Dubai Industrial Park","7":"Downtown Dubai","8":"Deira","9":"Dubai Marina","13":"Old Town","16":"Al Quoz","21":"Jumeirah Lake Towers","22":"Culture Village","23":"Jumeirah Golf Estates","28":"Mohammed Bin Rashid City","31":"Sheikh Zayed Road","34":"Al Furjan","40":"Dubai Sports City","43":"DAMAC Hills (Akoya by DAMAC)","44":"Serena","48":"Dubai South (Dubai World Central)","55":"Town Square","58":"Business Bay","60":"Karama","64":"Bur Dubai","66":"Al Badaa","72":"Reem","76":"Al Taawun","79":"Dragon City","102":"Muroor Area","103":"Al Rahba","107":"Khalifa City","114":"Dubai Production City (IMPZ)","139":"Al Khail Heights","143":"Al Raha Beach","144":"Yas Island","178":"Jumeirah Park"} 
Titus Saju

Titus Saju 0 points

  • 2 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think the main difference is you're using array_values() on one and not on the other.

This isn't really a Perch question though!