note 41901 deleted from ref.array by aidan

From: Date: Sun, 09 Oct 2005 09:15:02 +0000
Subject: note 41901 deleted from ref.array by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-96740@lists.php.net to get a copy of this message
Note Submitter: gigi at orsone dot com ---- Here is a small function to "convert" php arrays to javascript... <?php $menuz = array('Products', 'products.php', array('Fruit', null, array('Bananas', 'products.php?cat=bananas'), array('Apples', 'products.php?cat=apples'), array('Lemons', 'products.php?cat=lemons') ), array('Chocolate', 'products.php?cat=chocolate') ); function array_php2js($data) { foreach($data as $k=>$datum) { if(is_null($datum)) $data[$k] = 'null'; if(is_string($datum)) $data[$k] = "'" . $datum . "'"; if(is_array($datum)) $data[$k] = php2js($datum); } return "[" . implode(', ', $data) . "]"; } print(array_php2js($menuz)); ?> And you get: ['Products', 'products.php', ['Fruit', null, ['Bananas', 'products.php?cat=bananas'], ['Apples', 'products.php?cat=apples'], ['Lemons', 'products.php?cat=lemons']], ['Chocolate', 'products.php?cat=chocolate']] I find it useful in many situation... ;)

« previous php.notes (#96740) next »