Jump to content

Arrays


Go to solution Solved by dalecosp,

Recommended Posts

Hey guys. I come from a PHP programming background. In php, to create arrays, its simple as doing the following

$array = array1("dog", cat);

echo $array[0];

But for java its not that simple. My first question is what is the difference between a literal array, condensed array, and regular array.

 

My second question is how can i display a value of an array using the example below? For example how do I display dog in the browser?

              <script>
        var array1 = ["dog", "cat"];
         array1[0];
        
        
        </script>

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/283951-arrays/
Share on other sites

See this SO answer they explain it a lot better. At least read the top 2 answers, though there's good info in a lot of the rest.

 

As far as displaying the value.. well that depends on what you mean by "display".

 

This will output the value of the first element to the javascript console:

 

console.log(array1[0]);
This will give you a popup alert:

 

alert(array1[0]);
Link to comment
https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458486
Share on other sites

You'd do well to get a book or some web tutorials on Java*script*.  Remember this is NOT JAVA.

 

An array in Javascript is just as simple as PHP, or perhaps moreso, and can be initialized like this:

myArray = ['dog','cat'];

Putting it "in the browser", though, is a whole 'nother can of worms known as "manipulating the DOM" (Document Object Model).

One of Javascript's biggest proponents calls it (the DOM) nothing less than the "worst API ever invented."

Here's one way it could be done, but I would never recommend it for general use:
 

myArray = ['dog','cat'];
body = document.getElementsByTagName("body");
body[0].innerHTML = myArray[0];
Edited by dalecosp
Link to comment
https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458487
Share on other sites

See this SO answer they explain it a lot better. At least read the top 2 answers, though there's good info in a lot of the rest.

 

As far as displaying the value.. well that depends on what you mean by "display".

 

This will output the value of the first element to the javascript console:

 

console.log(array1[0]);
This will give you a popup alert:

 

alert(array1[0]);

.josh. I mean just echoing it out... I guess javascript doesn't work like that does it?

 

It needs to be triggered by an event?

Link to comment
https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458494
Share on other sites

The API is the browser, and the quote is from Douglas Crockford, a renowned speaker on Javascript.

Start here:  

 and you'll learn quite a bit about Javascript.  There are 3 parts to this original teaching, and he's gone on from there to "world famous" on the subject.
Link to comment
https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458500
Share on other sites

  • Solution

.josh. I mean just echoing it out... I guess javascript doesn't work like that does it?

 

It needs to be triggered by an event?

Well, yes, triggered by something, typically an event.  Javascript is the internal language of the browser, and the browser is displaying what's given to it by the server.  In the context of the loaded "page", it will then execute.  So you use Javascript most often to manipulate the document (page) when it's already loaded in the browser window, which means you're manipulating the browser's view of the code (the DOM) after it's already been parsed and rendered, more or less.

Link to comment
https://forums.phpfreaks.com/topic/283951-arrays/#findComment-1458501
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.