Asked  8 Months ago    Answers:  5   Viewed   50 times

I have made a small application using jQuery's datepicker. I am setting unavailable dates to it from a JSON file which looks like this:

{ "dates": ["2013-12-11", "2013-12-10", "2013-12-07", "2013-12-04"] }

I would like to check if a date given is already in this list and remove it if so. My current code looks like this:

if (isset($_GET['date'])) //the date given
{
    if ($_GET['roomType'] == 2)
    {
        $myFile = "bookedDates2.json";
        $date = $_GET['date'];
        if (file_exists($myFile))
        {
            $arr = json_decode(file_get_contents($myFile), true);
            if (!in_array($date, $arr['dates']))
            {
                $arr['dates'][] = $_GET['date']; //adds the date into the file if it is not there already
            }
            else
            {
                foreach ($arr['dates'] as $key => $value)
                {
                    if (in_array($date, $arr['dates']))
                    {
                        unset($arr['dates'][$key]);
                        array_values($arr['dates']);
                    }
                }
            }
        }

        $arr = json_encode($arr);
        file_put_contents($myFile, $arr);
    }
}

My problem here is that after I encode the array again, it looks like this:

{ "dates": ["1":"2013-12-11", "2":"2013-12-10", "3":"2013-12-07", "4":"2013-12-04"] }

Is there a way to find the date match in the JSON file and remove it, without the keys appearing after the encode?

 Answers

100

Use array_values() for your issue:

$arr['dates'] = array_values($arr['dates']);
//..
$arr = json_encode($arr);

Why? Because you're unsetting array's key without re-ordering it. So after this the only way to keep that in JSON will be encode keys too. After applying array_values(), however, you'll get ordered keys (starting from 0) which can be encoded properly without including keys.

Wednesday, March 31, 2021
 
shin
answered 8 Months ago
71

I'm not sure exactly what's missing, but from the code you gave so far, I made a jsfiddle:

Demo

I modified some things to make it work (mostly appending stuff), because I don't know your original HTML file. But I also made some changes based on what you say you wanted. First of all I modified your $.getJSON call to be something like:

$.getJSON('http://radioplayer.bauerradio.com/schedule.php?callback=?', {
          name: stationName //stationName is from the argument passed
        }, function(json){...})

Which should give back the station based on what is passed to

LocalhostRadioStations.Schedule.ScheduleGen.init(twittiName);

To make it more interesting, I also added a bit of code that reads from the url. In this case if you go to the page with a domain.htm/page?Northern FM it will read the text after the ? and put it in twittiName.

var twittiName="Rock FM"; //default station

if(window.location.search){
   twittiName=window.location.search.substring(1)   //or window.location.hash
}

Tried to look for other stations that might be on your publics site, but so far I could only test with "?Rock+FM". But does mean you can show the errors, which your code can handle as it is.

  • ?Rock+FM
  • ?Random Name

So it seems that your code mostly works but do comment if I have missed anything.

Wednesday, March 31, 2021
 
skrilled
answered 8 Months ago
75

You can use:

alert(obj['a']);

See this question for more info.

Wednesday, March 31, 2021
 
EurekA
answered 8 Months ago
100

Am I doing that right, as far as iterating through the Arraylist goes?

No: by calling iterator twice in each iteration, you're getting new iterators all the time.

The easiest way to write this loop is using the for-each construct:

for (String s : arrayList)
    if (s.equals(value))
        // ...

As for

java.lang.ArrayIndexOutOfBoundsException: -1

You just tried to get element number -1 from an array. Counting starts at zero.

Sunday, June 27, 2021
 
o_flyer
answered 4 Months ago
99

Override getCount(). And return the value forms.size() in it.

Sunday, August 15, 2021
 
Eddy
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share