2

I want to copy a PHP array into javascript array. The javascript is being used in the PHP file. It is echoed in the PHP code so when i tried to create a new array in javascripy and just assign the PHP array onto it, it was giving me an error. Array to string conversion. Could some one help me with this.

 mysql_select_db("cerebra", $con);
 $sql="select name from details order by download desc limit 20";
 if (!mysql_query($sql,$con))`enter code here`
  {
  die('Error: ' . mysql_error());
  }
 $query=mysql_query($sql,$con);
 $names=array();
 while($row=mysql_fetch_array($query))
   {
$names[]=$row[0];
   }

Here $names is the array i want to use in javascript

then i echo my javascript inside the php code itself..:

   function makeRequest() {
   var d = document.getElementsByName(\"d\")[0].value;
   var request = gapi.client.drive.files.list();
var newnames = new Array();
    newnames=$names;
  request.execute(function(resp)

 {        
    for (i=0; i<resp.items.length; i++) {
        var titulo = resp.items[i].title;
        var fechaUpd = resp.items[i].modifiedDate;
        var userUpd = resp.items[i].lastModifyingUserName;
        var userEmbed = resp.items[i].embedLink;
        var userAltLink = resp.items[i].alternateLink;
        var download = resp.items[i].webContentLink;
        var hold=\"Download\";
        var flag=0;

         <!-- var fileInfo = document.createElement('li');
      <!--  fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo + ' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd +'  url:  ' + hold.link(download)));                
       <!-- document.getElementById('content').appendChild(fileInfo);
        <!--for (var i=0;i<newnames.length;i++){
    <!--    if(newnames[i].toString() == titulo){
        document.write(titulo + \"&nbsp;\");
        document.write(hold.link(download) + \"<br>\");
        <!--flag=1;
    <!--    }
        <!--}
        }
        <!--if(flag!=1){
        <!--document.write(\"not found!\");
        <!--}
});    
}

Here in the javascript i am trying to convert the php array into javascript array but its not working

  • 3
    `` – DCoder Feb 20 '13 at 06:35
  • and also, the most common way to do that, is to exchange JSON or XML strings and then parse them. – Yang Feb 20 '13 at 06:36
  • check out this link..http://stackoverflow.com/questions/5618925/convert-php-array-to-javascript-array – sasi Feb 20 '13 at 06:39
  • json_encode should be enough IF javascript objects is ok, if not then as look at the link that @sasi posted above. – iiro Feb 20 '13 at 06:43
  • JavaScript Object Notation (JSON). I actually didn't even know that until like a few months ago, so I'm not trying to be smug. It's just a good thing to know. – Vinay Feb 20 '13 at 06:53
  • guys am echoing the whole javascript inside the PHP code is there any way i can do it inside the echo itself??? –  Feb 20 '13 at 06:58

3 Answers3

8

Example-1:

<script type='text/javascript'>
<?php
$php_array = array('he','hi','hello');
$js_array = json_encode($php_array);
echo "var javascript_array = ". $js_array . ";\n";
?>
</script>

NOTE: You can convert php arrays into javascript using php's json_encode() function.

Check out this convert php array to java script array for more information.

Example-2:

PHP Code:

$arr = array(1,2,3,4,5,6,7);
$script = '<script>var newArr = new Array(' . implode(',', $arr) . ');</script>';
echo $script; 

HTML Code:

<script>
function doOnchange(arr){
    alert(arr.length);
}
</script>
<form name="frm">
    <select name="sel1" onchange="doOnchange(newArr);">
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>
</form>

Checkout some more links,

How to Convert PHP Multidimensional Array to Javascript Object (using jQuery)

array2json() - Convert PHP arrays to JSON

json_encode pass php array to javascript

may this help you.

Community
  • 1
  • 1
Tony Stark
  • 8,064
  • 8
  • 44
  • 63
  • guys am echoing the whole javascript inside the PHP code is there any way i can do it inside the echo itself??? –  Feb 20 '13 at 06:56
  • @user2038580 if you try any code so post it which help us for better understanding. – Tony Stark Feb 20 '13 at 07:01
0

Have a look at the json_encode() function. Modified example from the site:

<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

echo 'var js_object = '.json_encode($arr).';';
// outputs: var js_object = {"a":1,"b":2,"c":3,"d":4,"e":5};
?>
Imperative
  • 3,138
  • 2
  • 25
  • 40
-1

If its a single or simple array you can manually construct the javascript array from PHP array. But its not a good way. I would suggest the json or xml way.

 <?php 
    $myArr = array(1,2,3,4,5);
  ?>
 <script>
  var myArr = new Array();
  <?php 
    for($i=0;$<count($myArr);$i++) { ?>
     myarr[<?php echo $i ?>] = <?php echo $myArr[$i] ?>;
    <?php } ?>
  ?>
 </script>   
Samy
  • 632
  • 4
  • 14
  • guys am echoing the whole javascript inside the PHP code is there any way i can do it inside the echo itself??? –  Feb 20 '13 at 06:58