Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array() to javascript array() [duplicate]

Tags:

I'm trying to convert a PHP array to a javascript array for jQuery's datetimepicker to disable some dates. But I can't seem to find the right answer on the internet. I'm using Zend Framework for my project.

<?php              $ConvertDateBack = Zend_Controller_Action_HelperBroker::getStaticHelper('ConvertDate');             $disabledDaysRange = array();             foreach($this->reservedDates as $dates) {                  $date = $ConvertDateBack->ConvertDateBack($dates->reservation_date);                  $disabledDaysRange[] = $date;             } ?> <script> var disabledDaysRange = $disabledDaysRange ???? Please Help; $(function() {     function disableRangeOfDays(d) {         for(var i = 0; i < disabledDaysRange.length; i++) {             if($.isArray(disabledDaysRange[i])) {                 for(var j = 0; j < disabledDaysRange[i].length; j++) {                     var r = disabledDaysRange[i][j].split(" to ");                     r[0] = r[0].split("-");                     r[1] = r[1].split("-");                     if(new Date(r[0][2], (r[0][0]-1), r[0][1]) <= d && d <= new Date(r[1][2], (r[1][0]-1), r[1][1])) {                         return [false];                     }                 }             }else{                 if(((d.getMonth()+1) + '-' + d.getDate() + '-' + d.getFullYear()) == disabledDaysRange[i]) {                     return [false];                 }             }         }         return [true];     }     $('#date').datepicker({         dateFormat: 'dd/mm/yy',         beforeShowDay: disableRangeOfDays         }); }); </script> 
like image 422
Daan Avatar asked Sep 13 '13 10:09

Daan


People also ask

How do I copy an array to another array in PHP?

The getArrayCopy() function of the ArrayObject class in PHP is used to create a copy of this ArrayObject. This function returns the copy of the array present in this ArrayObject.

How send data from array to JavaScript in PHP?

Passing PHP Arrays to JavaScript is very easy by using JavaScript Object Notation(JSON). Method 1: Using json_encode() function: The json_encode() function is used to return the JSON representation of a value or array. The function can take both single dimensional and multidimensional arrays.

How can I get common values from two arrays in PHP?

The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.

Which function is used to copy elements of an array into variables in PHP?

PHP | extract() Function. The extract() Function is an inbuilt function in PHP. The extract() function does array to variable conversion. That is it converts array keys into variable names and array values into variable value.


2 Answers

To convert you PHP array to JS , you can do it like this :

var js_array = [<?php echo '"'.implode('","',  $disabledDaysRange ).'"' ?>]; 

or using JSON_ENCODE :

var js_array =<?php echo json_encode($disabledDaysRange );?>; 

Example without JSON_ENCODE:

<script type='text/javascript'>     <?php     $php_array = array('abc','def','ghi');     ?>     var js_array = [<?php echo '"'.implode('","', $php_array).'"' ?>];     alert(js_array[0]); </script> 

Example with JSON_ENCODE :

<script type='text/javascript'>     <?php     $php_array = array('abc','def','ghi');     ?>     var js_array =<?php echo json_encode($disabledDaysRange );?>;     alert(js_array[0]); </script> 
like image 89
Charaf JRA Avatar answered Nov 08 '22 20:11

Charaf JRA


The PHP json_encode function translates the data passed to it to a JSON string which can then be output to a JavaScript variable.The PHP json_encode function returns a string containing the JSON equivalent of the value passed to it.

<?php $ar = array('apple', 'orange', 'banana', 'strawberry'); echo json_encode($ar); // ["apple","orange","banana","strawberry"] ?> 

You can pass the JSON string output by json_encode to a JavaScript variable as follows:

<script type="text/javascript"> // pass PHP variable declared above to JavaScript variable var ar = <?php echo json_encode($ar) ?>; </script> 

A numerically indexed PHP array is translated to an array literal in the JSON string. A JSON_FORCE_OBJECT option can be used if you want the array to be output as an object instead:

<?php echo json_encode($ar, JSON_FORCE_OBJECT); // {"0":"apple","1":"orange","2":"banana","3":"strawberry"}  ?> 

Associative Array Example:

<?php $book = array(     "title" => "JavaScript: The Definitive Guide",     "author" => "David Flanagan",     "edition" => 6 ); ?> <script type="text/javascript"> var book = <?php echo json_encode($book, JSON_PRETTY_PRINT) ?>; /* var book = {     "title": "JavaScript: The Definitive Guide",     "author": "David Flanagan",     "edition": 6 }; */ alert(book.title); </script> 

Notice that PHP's associative array becomes an object literal in JavaScript. We use the JSON_PRETTY_PRINT option as the second argument to json_encode to display the output in a readable format.

You can access object properties using dot syntax, as displayed with the alert included above, or square bracket syntax: book['title'].

here you can find more information and details.

like image 23
Reza Baradaran Gazorisangi Avatar answered Nov 08 '22 20:11

Reza Baradaran Gazorisangi