Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print array formatting 1

Status
Not open for further replies.

stormbind

Technical User
Joined
Mar 6, 2003
Messages
1,165
Location
GB
Hi all,

My simple desire is to print the contents of an array in PHP for use in Javascript. Something like this:

<script language="text/javascript">
var import = { <?php print_f($export) ?> };
</script>

The problem is that the values aren't being printed the way Javascript expects them, which is { "one", "two", "three" } etc.

Please post suggestions. Thank you! :-)

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
if you use
Code:
$jsarr = print_r($phparr, true);

print_r will return its output to a string which you can then manipulate. but in fact this won't work for js either.

something like this might though
Code:
function arraytojs($array){
 $str = "";
 foreach ($array as $val):
   $str .=  "'".$val."',";
 endforeach;
 $str = rtrim($str, ",");
 return $str;
}
<script>
  var jsarray = array(<?=arraytojs($arraytoconvert)?>);
</script>
 
oh ok, thanks very much! :)

--Glen :)

Memoria mihi benigna erit qui eam perscribam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top