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,810 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 subscriber has a blog too: Read the blog: 57 Shoutbox: 103 reviews: 153
Visitors: 10,810
Overall visits to all the topics: 4,962,473
Daily average (Calculated from the website subscription day): 2,059.97
Optional sorting commands:
Normal order: click here
Order by amount of visits: click here
Order by category: Programming Php
Current order: by category: Critical Reviews and Essays
Other categories available for this author (Limited data report: 100):
Advice: Martial Arts and Self Defense(4), Books(236), Critical Reviews and Essays(4), Dictionaries(1), Emergency Care(4), Epistles Letters and Advice(5), Fantasy Epics and Fables(1), History and Documents(5), Humor and Jokes(2), Methematics(2), Music and Lyrics(6), News Digests and Press Reviews(1), Novels Poetry and Stories(3), Philosophy Reviews(7), Poetry(1), Programming(4), Programming Ajax(2), Programming Javascript(82), Programming Php(52), Psychology(1), Quotes(1), Religion Esoterica and Spirituality(12), Scientifical Reviews(4), Self Improvement(1), Sport Activities and Apparels(2), Tarots(1)
Showing topics: 1, 10
Available total: 448
View only a list of the snippets by this author: click here.
Other topics available for this author: click on any title below to view the complete item:
Capire Il Piduismo E La P2: Che Cosa E' Il Deviazionismo Massonico? Identification Number: 214 Visitors: 14,837 Simbologie Massoniche: Tavola Di Istruzione Rapida Con Cenni Storici Insoliti Identification Number: 191 Visitors: 32,065 The Cop For All Seasons: The Metaphysical Problem Of The Policeman And The Army Identification Number: 189 Visitors: 18,137 A Perspective On The American Role Leadership And Legacy In Universal History Identification Number: 184 Visitors: 13,705
External services
This page of this subscriber uses external services: Hide