Convert PHP Array into equivalent Javascript

Latitudes
Latitudes used Ask the Experts™
on
How would I convert the following code from PHP into Javascript?  


<?php

// Each sponsor is an element of the $sponsors array:

$sponsors = array(
	array('facebook','The biggest social network in the world.','http://www.facebook.com/'),
	array('adobe','The leading software developer targeted at web designers and developers.','http://www.adobe.com/'),
	array('microsoft','One of the top software companies of the world.','http://www.microsoft.com/'),
	array('sony','A global multibillion electronics and entertainment company ','http://www.sony.com/'),
	array('dell','One of the biggest computer developers and assemblers.','http://www.dell.com/'),
	array('ebay','The biggest online auction and shopping websites.','http://www.ebay.com/'),
	array('digg','One of the most popular web 2.0 social networks.','http://www.digg.com/'),
	array('google','The company that redefined web search.','http://www.google.com/'),
	array('ea','The biggest computer game manufacturer.','http://www.ea.com/'),
	array('mysql','The most popular open source database engine.','http://www.mysql.com/'),
	array('hp','One of the biggest computer manufacturers.','http://www.hp.com/'),
	array('yahoo','The most popular network of social media portals and services.','http://www.yahoo.com/'),
	array('cisco','The biggest networking and communications technology manufacturer.','http://www.cisco.com/'),
	array('vimeo','A popular video-centric social networking site.','http://www.vimeo.com/'),
	array('canon','Imaging and optical technology manufacturer.','http://www.canon.com/')
);


// Randomizing the order of sponsors:

shuffle($sponsors);

?>



<div id="main">

	<div class="sponsorListHolder">

		
        <?php
			
			// Looping through the array:
			
			foreach($sponsors as $company)
			{
				echo'
				<div class="sponsor" title="Click to flip">
					<div class="sponsorFlip">
						<img src="img/sponsors/'.$company[0].'.png" alt="More about '.$company[0].'" />
					</div>
					
					<div class="sponsorData">
						<div class="sponsorDescription">
							'.$company[1].'
						</div>
						<div class="sponsorURL">
							<a href="'.$company[2].'">'.$company[2].'</a>
						</div>
					</div>
				</div>
				
				';
			}
		
		?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
if you want to convert a php array to a JavaScript equivalent this would be json so you would use http://php.net/manual/en/function.json-encode.php
First convert the PHP array into JSON using json_encode() function.

Then in JS use eval() function to read the JSON.

For Ex:

<?php

// Each sponsor is an element of the $sponsors array:

$sponsors = array(
      array('facebook','The biggest social network in the world.','http://www.facebook.com/'),
      array('adobe','The leading software developer targeted at web designers and developers.','http://www.adobe.com/'),
      array('microsoft','One of the top software companies of the world.','http://www.microsoft.com/'),
      array('sony','A global multibillion electronics and entertainment company ','http://www.sony.com/'),
      array('dell','One of the biggest computer developers and assemblers.','http://www.dell.com/'),
      array('ebay','The biggest online auction and shopping websites.','http://www.ebay.com/'),
      array('digg','One of the most popular web 2.0 social networks.','http://www.digg.com/'),
      array('google','The company that redefined web search.','http://www.google.com/'),
      array('ea','The biggest computer game manufacturer.','http://www.ea.com/'),
      array('mysql','The most popular open source database engine.','http://www.mysql.com/'),
      array('hp','One of the biggest computer manufacturers.','http://www.hp.com/'),
      array('yahoo','The most popular network of social media portals and services.','http://www.yahoo.com/'),
      array('cisco','The biggest networking and communications technology manufacturer.','http://www.cisco.com/'),
      array('vimeo','A popular video-centric social networking site.','http://www.vimeo.com/'),
      array('canon','Imaging and optical technology manufacturer.','http://www.canon.com/')
);


// Randomizing the order of sponsors:

shuffle($sponsors);

?>



<div id="main">

      <div class="sponsorListHolder">

            
        <?php
                  
                  // Looping through the array:
                  
                  foreach($sponsors as $company)
                  {
                        echo'
                        <div class="sponsor" title="Click to flip">
                              <div class="sponsorFlip">
                                    <img src="img/sponsors/'.$company[0].'.png" alt="More about '.$company[0].'" />
                              </div>
                              
                              <div class="sponsorData">
                                    <div class="sponsorDescription">
                                          '.$company[1].'
                                    </div>
                                    <div class="sponsorURL">
                                          <a href="'.$company[2].'">'.$company[2].'</a>
                                    </div>
                              </div>
                        </div>
                        
                        ';
                  }
            
            ?>
            
            <script type="text/javascript" language="javascript" src="js/jquery.js"></script>
            <script type="text/javascript" language="javascript">
                  
                  var json_sponsors = '<?php echo json_encode($sponsors);?>';
                  
                  eval("sponsors="+json_sponsors);
                  
                  $(sponsors).each(function()
                  {
                        alert(this);
                  });
            </script>
greetings  Latitudes, , , not sure what you may want, you do not really say what the purpose of the "Conversion from PHP" may be for, , I will guess you want to do a javascript  sponsor random display without any PHP, here is a pure JS to do that - -

<html><head><title>Java Script Array convert</title>
<script>
var sponsors = [
	['facebook','The biggest social network in the world.','http://www.facebook.com/'],
	['adobe','The leading software developer targeted at web designers and developers.','http://www.adobe.com/'],
	['microsoft','One of the top software companies of the world.','http://www.microsoft.com/'],
	['sony','A global multibillion electronics and entertainment company ','http://www.sony.com/'],
	['dell','One of the biggest computer developers and assemblers.','http://www.dell.com/'],
	['ebay','The biggest online auction and shopping websites.','http://www.ebay.com/'],
	['digg','One of the most popular web 2.0 social networks.','http://www.digg.com/'],
	['google','The company that redefined web search.','http://www.google.com/'],
	['ea','The biggest computer game manufacturer.','http://www.ea.com/'],
	['mysql','The most popular open source database engine.','http://www.mysql.com/'],
	['hp','One of the biggest computer manufacturers.','http://www.hp.com/'],
	['yahoo','The most popular network of social media portals and services.','http://www.yahoo.com/'],
	['cisco','The biggest networking and communications technology manufacturer.','http://www.cisco.com/'],
	['vimeo','A popular video-centric social networking site.','http://www.vimeo.com/'],
	['canon','Imaging and optical technology manufacturer.','http://www.canon.com/']
	];
	
 var i = sponsors.length, j, t;
 while( i ) {
	j = Math.floor(( i-- )*Math.random());
	t = sponsors[i];
	sponsors[i] = sponsors[j];
	sponsors[j] = t;
	}
</script></head>
<body bgcolor="#D3E7F2"><h3>Java Script Array convert</h3><p>
<script>
for (i = 0; i < sponsors.length; ++i) {
  document.write('<div class="sponsor" title="Click to flip"><div class="sponsorFlip">'+
  '<img src="img/sponsors/'+sponsors[i][0]+'.png" alt="More about '+sponsors[i][0]+'" /></div>'+
  '<div class="sponsorData"><div class="sponsorDescription">'+sponsors[i][1]+
  '</div>\n<div class="sponsorURL"><a href="'+sponsors[i][2]+'">'+sponsors[i][2]+'</a></div></div></div>');
  }
</script></p></body></html>

Open in new window

@ Latitudes  , please let me know with a post if this works for you, or you have any questions.

Author

Commented:
This is exactly what I'm looking for!  Thanks a ton!  

I was curious about the conversion and I was also looking to use the concept for another project.  

Thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial