Skip to main content

Featured Post

Top 10 Advance Java Interview questions?

Top 10 Advance Java Interview questions?   What are the differences between abstract classes and interfaces in Java? What is the difference between ArrayList and LinkedList in Java? What is the purpose of the finalize() method in Java? What is polymorphism in Java and how is it achieved? What are the different types of inner classes in Java? What is the difference between static and non-static methods in Java? What are the different types of exceptions in Java and how do they differ? What is the difference between checked and unchecked exceptions in Java? How does Java handle multithreading and synchronization? What are the different types of JDBC drivers in Java and how do they differ?

What is Array in PHP?

 Now that, you know what arrays are? Let’s look at how to use them in PHP, starting with indexable arrays. All right. My opening PHP tags. On a comment here, indexable array. And let’s create that color array. So the array is going to have a name just like any other variable with a dollar sign first. So we’ll say colors, get, and then we’re going to use the keyword array and inside of the parenthesis for the array function or keyword, we’re going to have a comma-separated list of items. We’ll say, red, blue, and green. So if we want to see what this array looks like,

we can use a special function called print r. That will print the array nicely for us. So we’ll say print our colors. I’ll save this, and then I’ll go to the browser. And you can see that the array is a simple array with our indexes as numbers, starting at 0. That means that if we want to reference a specific color, we would do so by writing Echo, colors, square bracket, and then the index.
So if we wanted to print blue we would say Echo colors square bracket, 1 close square bracket. Now, if we go back out and refresh you can see the word blue shows up here. And if we wanted to have it on its own line, we can add some HTML.
So I will add single quotes. I’ll say, Echo, single quote paragraph, tag close single quote,
use our concatenation period, And then after colors, I’ll do the same thing. Single quote and
P tag. and now, Blue is on its own line. So that is an indexable array. Something that makes PHP
arrays, especially flexible. When compared to other programming languages is the way you define and add elements. While stricter languages require you to define a size when you define the array PHP. Does not. So we’ve defined our array up here with initial values, but if we want to add a new value, we could simply say colors. Square brackets with no index get and then whatever color we want to add. So let’s say yellow. Now if we print our array, And refresh the page. We have a brand new element with index 3.

So again, with stricter languages, you might have to say that you want an array of size 4, or you’d have to name the index here, which means that you need to keep track. Mentally of how many items are in your array PHP, makes this a little bit easier. Now, let’s look at a slightly more complicated example, with associative arrays. So let’s use that. Hometowns example. I’ll create a variable called hometowns. I’ll make it an array, but we want this to be an associative array. So, whereas with the indexable array, we only need to define the values. With an associative array. We need to define the keys as well. So I’ll say Joe, and then the arrow operator, this is how we assign values in arrays, so we’ll say, Joe Arrow operator. Middletown, New York, Aaron.

And then Westchester. Notice, we also have to put a comma after every key-value pair here as well. Dave & Eton. And Brian.
From Grand Rapids. Now, I’m also going to add a comma after the last element here. This is a perfectly valid syntax and it will make adding elements to the list. If we want to easier because we won’t have to remember to add this later. And again, let’s use print to visualize this. All right, so this looks a little bit more complicated. Let’s make this a little bit pretty to do that. We will Echo our
pre tags before and after print

00:05:00 Speaker 1
Great. That looks a lot nicer and you could see that the keys are the strings that we defined and the values are the values. So now if we want to print out, let’s say Dave’s Hometown we could say,
Echo will have an R P tag, dot hometowns and then we would need to reference the string index and its case sensitivity. So keep that in mind. We used a capital D for Dave, so we’ll say hometowns square bracket, Dave, and then we’ll close our P tag here. Now, if we refresh our page, it’ll say, Eaton PA. The last thing we’ll take a look at is a multi-dimensional array and to save some time. I’ve already pointed this out for you. It is a multi-dimensional array, where it has my brothers and me with various information about each of us. So we have our brothers variable gets array. And then the first key is my name. And the value is another array with key-value pairs for age job and state. If we want to show how this is visualized, we can again print out our pre-tag here. So we’ll say Echo Prix. Print is  Brothers. And then Echo and pre. Now, let’s go to that page.

So you could see the visualization looks very similar to the definition here and it looks familiar to the other arrays that we saw. But if we try to do something like for example, Echo, a key in the brother’s array, so we’ll say Echo pee. Brothers. Rob. we’ll save that if we go back to our browser,
You’ll see that we get an error here that says, notice array to string conversion and just the word array. That’s because we need to be more specific. Since Rob is a key that points to another array, we need to grab a key inside that second array to get the value. So if we want to get the state where Rob lives, we would need to add a second pair of square brackets here. So after the closing square bracket on Rob, we would open a square bracket at our quote And then grab the key to the second array. So we’ll say the state will close the square bracket will save. This will refresh and you can see the letters Florida show up. Here are the letters FL for Florida shows up here.
A raise will be a big part of working with data in your code and it’s good to get to know them as early as possible. Later,
you’ll learn how to easily Traverse through an array using loops.

Comments

Popular posts from this blog

WHAT ARE NEURAL NETWORKS? | Comingfly

WHAT ARE NEURAL NETWORKS ? Neural Networks the process of machine learning are neural networks. These are brain-inspired networks of interconnected layers of algorithms, called neurons, that feed data into each other, and which can be trained to carry out specific tasks by modifying the importance attributed to input data as it passes between the layers. During training of these neural networks, the weights attached to different inputs will continue to be varied until the output from the neural network is very close to what is desired, at which point the network will have 'learned' how to carry out a particular task. A subset of machine learning is deep learning, where neural networks are expanded into sprawling networks with a huge number of layers that are trained using massive amounts of data. It is these deep neural networks that have fueled the current leap forward in the ability of computers to carry out task like speech recognition and computer vision. T here are vario...

What is Go Language ? | Comingfly

What is Go Language ? What Go Language? :- v    Go is a modern, general purpose language. v    A new ,Open source Programming language. v    Concurrent , Builds fast at scale. v    Go language is very fast. v    Garbage-Collected. v    Lightweight syntax. v    Simple type system. v    Productive. v    Simple. v    Fun History of Go language :- Go language developed by Rob Pike   and Ken Thompson. More than 250 contributors join the project.   Design began late in 2007.   Become open source in 2009.   Version is 1.2.2. Feature of Go language ? v   In the current world, languages don't help enough. v   Computers fast but software construction slow. v   Efficiency. v   Safety. v   Speed. v   Concurrency. v   Scalability. v   Fast development cycle. v   No surprises. v   Multi-core Why Developed a Language ? Google has bi...

What is Java? | Comingfly

What is Java? Developed by Sun Microsystems in the mid-1990s, Java was originally built to be a high-level and object-oriented programming language that looks and feels similar to C++. Along with being extremely popular, Java can implement a wide variety of algorithms, which are very useful to the machine learning community. Java is regarded as one of the most secure programming languages, largely due to its use of bytecode and sandboxes. Java manages to harness much of the power of C++ while overcoming its security issues and overall complexity. But with all of its improvements over C++, Java has a reputation for being slower than many other programming languages. Additionally, as of 2019, Java has implemented commercial licensing for certain business applications, which can be costly.