Jump to content

[SOLVED] sorting an array and print out a JS array


albertdumb

Recommended Posts

Hi All.

 

I have this array like so....

 

Array

(

    [ABZ] => Array

        (

            [0] => AGP

        )

 

    [ACE] => Array

        (

            [0] => BHX

            [1] => LGW

            [2] => LTN

            [3] => MAN

        )

)

 

Now i want to loop through this array to print out a javascript array in the format like >> g_routes['ACE'] = new Array('BHX','LGW','LTN','MAN');

 

I dont mind just using an echo or assign it a php variable.

 

Can anyone please help?

 

Cheers

Link to comment
Share on other sites

I am using a foreach and i got most it done except the last bit of the array: This is my code>>

 

foreach ($listSort as $value => $var){

print 'g_routes[\''.$value.'\']= new Array(\''.$var[0] .'\',\'LGW\');';

}

 

I am having problem with the new array as i need to loop trough to get the next level down but i am not sure how to do it. So this is what i have now:

 

g_routes['ABZ']= new Array(' STILL NEED TO GET','STILL NEED TO GET');

 

Any help?

 

 

 

Link to comment
Share on other sites

I made something for you:

<?php
function makeJSArray($name, $array, $parent='') {
if (empty($parent))
	$r = "var $name = new Array();\n";
else
	$r = '';
foreach ($array as $i => $s) {
	if (!is_numeric($i))
		$i = "'$i'";
	if (is_array($s)) {
		$r .= "$name$parent"."[$i] = new Array();\n";
		$r .= makeJSArray($name, $s, $parent."[$i]");
	}else {
		if (!is_numeric($s))
			$s = '"'.str_replace("\n", '\\n', addslashes($s)).'"';
		$r .= "$name$parent"."[$i] = $s;\n";
	}
}
return $r;
}
?>

Use it like that:

<?php
   $a = array('test', 'blubb', 'hui' => array('yeah', 'hmm', 'foo' => 'bar', array('nana')));
   echo makeJSArray('myjsarray', $a);
?>

Link to comment
Share on other sites

Thanks Lumio..

 

But the problem with that code is that the araay which is created has every key value iterated like so:

g_routes['ACE'][0] = "BHX";

g_routes['ACE'][1] = "LGW";

g_routes['ACE'][2] = "LTN";

g_routes['ACE'][3] = "MAN";

 

But i want it like:

 

g_routes['ACE'] = new Array('BHX','LGW','LTN','MAN');

 

i just dont know how to loop through the last dimension of the array and list it comma seperated, if you know what i mean.

 

 

Link to comment
Share on other sites

I have worked it out and it wasnt as complicated as i thought. Found the implode function. Heres the code i use

 

foreach ($this->listSort as $value => $var){

		$spiltVar= implode("','",$var);

		print 'g_routes[\''.$value.'\']= new Array(\''.$spiltVar .'\');';

	}

 

Thanks very much for your help tho.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.