The Identification Number and Password fields need to be filled in. Moreover, you cannot send messages devoid of contents Adding further data is not allowed because this topic has been closed Print You provided no password: you have to write it in the textfield beside the command delete for the topic you want to delete Are you sure you want to delete this comment? You did not cast a vote. You need to cast a vote by selecting a radio button. Impossible to proceed Provide the project number Insufficient form parameters: you forgot to fill in some field/s
 
member picmember pic
 
Snippets of A
 
info Below you can find the text of the snippet you want to read, and the list of the other snippets by this author if available.
What are snippets?
Share on MySpace

PHP Pass To Javascript Php Array Associative Array Or Matrix

Snippet: Identification Number »   Snippet: Inclusion syntax »
Visitors: 10,846 Tagged by its author as: Programming Php Characters (in origin): 3,959 (pages: ~ 1)
Author: Em@il Permalink Cast your vote for this topic Printable version
OBJECTIVE: given a php array, either numerically indexed or associative, or a matrix combining both (therefore we have three distinct procedures), get its values and rebuild it as a symmetrical javascript valid array or matrix.
<?php

/******************************************/
/************ NUMERICAL ARRAY ************/
/******************************************/
$genreArray=array('a', 'b', "'c'", 'd');//invented array

/* PROCEDURE BEGINS:*/
$toPrint=array();

for($i=0; $i<sizeof($genreArray); $i++){
$toPrint[]="'".str_replace("'", "\\'", $genreArray[$i])."'";
}
$toPrint='var genreArray=['.implode(", " , $toPrint).'];';
/* PROCEDURE ENDS*/

//print and test results:
echo '<script>';
echo $toPrint;
echo 'alert(genreArray[1]);';
echo '</script>';

/*prints:
<script>var genreArray=['a', 'b', '\'c\'', 'd'];alert(genreArray[1]);</script>
*/

/******************************************/
/************ ASSOCIATIVE ARRAY ************/
/******************************************/
$genreArray=array('a'=>'char a', 'b'=>'char b', 'c is'=>'c\'s here');//invented array

/* PROCEDURE BEGINS:*/
$toPrint=array();

foreach($genreArray as $k=>$v){
$k2=str_replace("'", "\\'", $k); $v2=str_replace("'", "\\'", $v);/*escape single quotes*/
$toPrint[]="'$k2': '$v2'";
}
$toPrint='var genreArray={'.implode(", " , $toPrint).'};';
/* PROCEDURE ENDS*/

//print and test results:
echo '<script>';
echo $toPrint;
echo 'alert(genreArray["c is"]);';
echo '</script>';

/*prints:
<script>var genreArray={'a': 'char a', 'b': 'char b', 'c is': 'c\'s here'};alert(genreArray["c is"]);</script>
*/
/******************************************/
/***************** MATRIX *****************/
/******************************************/
$genreArray=array(
	array('categoryName'=>'movie', 'genreType'=>'Default', 'id'=>'1', 'title'=>'She said "I love you"'),
	array('categoryName'=>'movie', 'genreType'=>'Thriller', 'id'=>'2'),
	array('categoryName'=>'documentary', 'genreType'=>'Adventure', 'id'=>'1809', 'title'=>'I don\'t know'),
);//invented matrix

/* PROCEDURE BEGINS:*/
$toPrint=array();

//arrange a numerically indexed array for javascript
for($i=0; $i<sizeof($genreArray); $i++){
$toPrint2=array();
	//arrange an associative (string indexed) array for javascript
	foreach($genreArray[$i] as $k=>$v){
	$k2=str_replace("'", "\\'", $k); $v2=str_replace("'", "\\'", $v);/*escape single quotes*/
	$toPrint2[]="'$k2': '$v2'";
	}
	//ends arranged associative
$toPrint[]='{'.implode(", " , $toPrint2).'}';
}
/* PROCEDURE ENDS*/

//print and test results:
echo '<script>';
echo "var genreArray=new Array(".implode(",", $toPrint).");";
echo 'alert(genreArray[1]["categoryName"]);';
echo '</script>';

/*prints:
<script>var genreArray=new Array({'categoryName': 'movie', 'genreType': 'Default', 'id': '1', 'title': 'She said "I love you"'},{'categoryName': 'movie', 'genreType': 'Thriller', 'id': '2'},{'categoryName': 'documentary', 'genreType': 'Adventure', 'id': '1809', 'title': 'I don\'t know'});alert(genreArray[1]["categoryName"]);</script>
*/
?>
Remove colors  
 
INPUTS: an array.  
 
RETURNS: nothing being arranged as a procedure and not as a function; however a function would return a string representing the valid javascript syntax that reproduces in javascript the php data structure. These procedures echo such a string.  
 
Caveats: since in our javascript strings will be represented in the arrays as single quotes sorrounded texts, the procedures need to escape single quotes that may be present in the php data otherwise printing them unescaped would determine a premature closing of the javascript strings.  
The same would apply in case you would use double quotes to sorround javascript strings, yet in such case you'd escape dobule quotes.  
 
It assigns to the output javascript array the arbitrary name genreArray.
Printable version To exclude hyperlinks from the print, check the checkbox »
Rss

Cast your vote for this topic

To perform this operation it is not necessary to be Full Poster members.
«Negative Positive»
Click here to save your vote (Current average: 0.00, Voters: 0)
Are you the author of this topic and do you want to append quickly more text? click here
Other topics
This list is currently empty or its edge has been surpassed. Back to snippets.
External services
This page of this subscriber uses external services: Hide