Reply
Convert data to a javascript array
Old 11-03-2009, 01:41 PM Convert data to a javascript array
Average Talker

Posts: 19
Name: Meir Ech
Trades: 0
An old file of mine has data in it.
It's an old file, from which I want to extract
values to use for a javascript array variable.

The old file's data looks like this:

$elem[0][0] = 0;
$elem[0][1] = 1;
$elem[0][2] = 1;
$elem[0][3] = "John likes fishing on Sundays";
$elem[0][4] = 7;

$elem[1][0] = 6;
$elem[1][1] = 1;
$elem[1][2] = 3;
$elem[1][3] = "Be careful of porcupines";
$elem[1][4] = 12;
....



I'd like to turn it into this:

var newArray =
[
$elem[0] = [0, 1, 1, "Jack climbed the beanstock", 7] ;
$elem[1] = [6, 1, 3, "Be careful of porcupines", 12] ;
...
]


Can you help this newbie? Thank you.
ruffy is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 11-03-2009, 02:45 PM Re: Convert data to a javascript array
wayfarer07's Avatar
NYE-KEE

Posts: 3,150
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
PHP Code:
<script>
var newArray = [];
<?php
foreach($elem as $key => $inner) {
    echo 
'newArray.push(['.$inner[0].','.$inner[1].','.$inner[2].','.$inner[3].','.$inner[4].']);';
}
?>
</script>
__________________
Wayfarer | jQuery Tooltip Plugin | Mapbox: the jQuery Map
Freelance Jobs Available
If Google is the Coca-Cola of Web search, Bing is RC Cola

Last edited by wayfarer07; 11-03-2009 at 02:48 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-03-2009, 05:36 PM Re: Convert data to a javascript array
chrishirst's Avatar
Super Moderator

Posts: 22,222
Location: Blackpool. UK
Trades: 0
In javascript everything is an object and can have properties.

So you create an object then create an array of the object.

Code:
function o_ObjName(prop0,prop1,prop2,prop3) {
	this.property0 = prop0;
	this.property1 = prop1;
	this.property2 = prop2;
	this.property3 = prop3;
}

var a_ObjArray(1);
	a_ObjArray(0) = new o_ObjName(0,1,1,"John likes fishing on Sundays",7);
	a_ObjArray(1) = new o_ObjName(6,1,3,"Be careful of porcupines",12);
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-04-2009, 03:36 PM Re: Convert data to a javascript array
Average Talker

Posts: 19
Name: Meir Ech
Trades: 0
I just noticed a mistake in my original question.
This is the end result I want, and not the one I
presented at first:

var newArray =
[
[0, 1, 1, "Jack climbed the beanstock", 7] ;
[6, 1, 3, "Be careful of porcupines", 12] ;
...
]


1) Do your answers to my first post change in any way?

Additional question arose for me:
2) Since I want to use this new javascript array for a new
development project, I need to write the new array into
a file, which I'll then include as an external script reference.
Can you include for me how to do this, please?
I suppose "pushing" therefore becomes the wrong output format.

3) Do I include the oldFile with a PHP include_once statement,
or do I open it as a file?

Thank you wayfarer07 and chrishirst for your time!
ruffy is offline
Reply With Quote
View Public Profile
 
Old 11-04-2009, 07:37 PM Re: Convert data to a javascript array
wayfarer07's Avatar
NYE-KEE

Posts: 3,150
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Your new structure is not any different than the old one you presented, except it has an implied order (0,1,2,3) instead of an explicit one. I also notice that you are putting a semi-colon where there should not be one. Arrays are ordered with commas, and not semi-colons, so even an array within an array must have a comma after all but the last member:

Code:
var newArray = 
[
 [0, 1, 1, "Jack climbed the beanstock", 7],
 [6, 1, 3, "Be careful of porcupines", 12] 
]
In my original example, I am using the array object's push method, which simply adds another member to the end of the array, with an automatically incremented index. This is a fast procedural way to output server-side data into JavaScript.

Chris's example will also work elegantly, though you won't be able to take advantage of the push method since the objects won't be arrays. Arrays are also objects; as Chris pointed out, everything in JavaScript is an object.
__________________
Wayfarer | jQuery Tooltip Plugin | Mapbox: the jQuery Map
Freelance Jobs Available
If Google is the Coca-Cola of Web search, Bing is RC Cola
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-04-2009, 08:29 PM Re: Convert data to a javascript array
Average Talker

Posts: 19
Name: Meir Ech
Trades: 0
But wayfarer07, I need to somehow save the javascript somewhere,
to use it when I start my new project.

Can I use that push code to save for a later occasion?

Perhaps if I can simply take the old PHP (with the original array),
and "massage" it, to look like a javascript file, then later, change the
name of the file and include it as an externally referenced js file?

What do you think? (Thanks, again.)
ruffy is offline
Reply With Quote
View Public Profile
 
Old 11-05-2009, 05:52 AM Re: Convert data to a javascript array
chrishirst's Avatar
Super Moderator

Posts: 22,222
Location: Blackpool. UK
Trades: 0
Quote:
I need to somehow save the javascript somewhere,
to use it when I start my new project.
I don't understand what you mean by this.

Probably you do like the rest of us do, and build our own "code libraries".

I have a "library" of several (lots of) files that I simply include into each project that needs these functions.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 11-05-2009, 01:12 PM Re: Convert data to a javascript array
Average Talker

Posts: 19
Name: Meir Ech
Trades: 0
chrishirst - I mean this:
My future project should not need to run the
php code to create the javascript array - then.
My code then should not be encumbered with
this "unnecessary" php code, if I can create the
array now. I want to have the javascript array
ready and waiting for me, when I need it,
without running php then to create it.

With you guys helping me now, I want to run a
php script now, save the javascript array
in a file, and throw away this one-time-only
conversion php code.

Wayfarer's code creates the javascript array
in a variable, but I need it in a file, so one day
I can include that file, for example, like this:
<script src='newArray.js' type='text/javascript'></script>
ruffy is offline
Reply With Quote
View Public Profile
 
Old 11-05-2009, 02:22 PM Re: Convert data to a javascript array
wayfarer07's Avatar
NYE-KEE

Posts: 3,150
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You could create something that writes the data to a file called whatever you like, but another solution would be to take advantage of the fact that PHP allows you to send Content-type headers from a .php file, allowing it to be linked to as a script, CSS file, etc, if you wish.

Example:
PHP Code:
<?php
//myscript.php
header("Content-type: text/javascript");//allow to be linked to from SCRIPT tag.
$elem[0][0] = 0;
$elem[0][1] = 1;
$elem[0][2] = 1;
$elem[0][3] = "John likes fishing on Sundays";
$elem[0][4] = 7;

$elem[1][0] = 6;
$elem[1][1] = 1;
$elem[1][2] = 3;
$elem[1][3] = "Be careful of porcupines";
$elem[1][4] = 12;
?>
var newArray = [];
<?php
foreach($elem as $key => $inner) {
    echo 
'newArray.push(['.$inner[0].','.$inner[1].','.$inner[2].','.$inner[3].','.$inner[4].']);';
}
?>
Then, from any HTML document:
HTML Code:
<script type="text/javascript" src="myscript.php"></script>
Note the script is pointing to a PHP file. It is just acting like a JavaScript file because it has the right Content-type header being sent.
__________________
Wayfarer | jQuery Tooltip Plugin | Mapbox: the jQuery Map
Freelance Jobs Available
If Google is the Coca-Cola of Web search, Bing is RC Cola

Last edited by wayfarer07; 11-05-2009 at 02:25 PM..
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-05-2009, 02:31 PM Re: Convert data to a javascript array
Average Talker

Posts: 19
Name: Meir Ech
Trades: 0
Wayfarer - you seem to like the esoteric solutions
to simple problems. You're suggesting I stick with
your php code, keep it inside a script, and tell the system
there's a javascript array somewhere in that script.

I don't need all that bloated stuff. I just want a simple
js array created as a file, to be referenced in the future by
<script src='newArray.js' type='text/javascript'></script>.
No php to be seen or heard from
(except temporarily now to create that file).

Last edited by ruffy; 11-05-2009 at 02:34 PM..
ruffy is offline
Reply With Quote
View Public Profile
 
Old 11-05-2009, 02:36 PM Re: Convert data to a javascript array
wayfarer07's Avatar
NYE-KEE

Posts: 3,150
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
There's nothing weird about sending a Content-type header, in fact it is a very common occurrence. In fact it is less "bloated" than other solutions that will require a cron-job, or some other event that causes a reading of the data and writing of it to a file a single time. If you prefer to do it this way I suggest you read up on PHP's file functions.
__________________
Wayfarer | jQuery Tooltip Plugin | Mapbox: the jQuery Map
Freelance Jobs Available
If Google is the Coca-Cola of Web search, Bing is RC Cola
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 11-05-2009, 03:25 PM Re: Convert data to a javascript array
chrishirst's Avatar
Super Moderator

Posts: 22,222
Location: Blackpool. UK
Trades: 0
Definitely not an esotoric soution. Using server side code to to send out different content headers is a pretty run of the mill operation.

"forced" downloads, "hiding" image paths etc.
I do the same thing with menus, when you have a specific point you want a menu to be opened at.

Some of my sites do not have a "physical" robots.txt file, I just sent a "text/plain" header along with some content from an ASP script based on the UA string.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Growing old is mandatory - Growing up is optional
Code Samples | People Counting System | Bits & Bobs

Last edited by chrishirst; 11-05-2009 at 03:28 PM..
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Convert data to a javascript array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.16183 seconds with 13 queries