Question

In: Computer Science

How are PHP arrays different than arrays in other programming languages? Please discuss in depth.

How are PHP arrays different than arrays in other programming languages? Please discuss in depth.

Solutions

Expert Solution

Array in python

A list in Python is just an ordered collection of items which can be of any type. By comparison an array is an ordered collection of items of a single type - so in principle a list is more flexible than an array but it is this flexibility that makes things slightly harder when you want to work with a regular structure. A list is also a dynamic mutable type and this means you can add and delete elements from the list at any time.

To define a list you simply write a comma separated list of items in square brackets:

myList=[1,2,3,4,5,6]

This looks like an array because you can use "slicing" notation to pick out an individual element - indexes start from 0. For example

print myList[2]

will display the third element, i.e. the value 3 in this case. Similarly to change the third element you can assign directly to it:

myList[2]=100

The slicing notation looks like array indexing but it is a lot more flexible. For example

myList[2:5]

is a sublist from the third element to the fifth i.e. from myList[2] to myList[4]. notice that the final element specified i.e. [5] is not included in the slice.

Also notice that you can leave out either of the start and end indexes and they will be assumed to have their maximum possible value. For example

myList[5:]

is the list from List[5] to the end of the list and

myList[:5]

is the list up to and not including myList[5] and

myList[:]

is the entire list.

List slicing is more or less the same as string slicing except that you can modify a slice. For example:

myList[0:2]=[0,1]

has the same effect as

myList[0]=0
myList[1]=1

Finally is it worth knowing that the list you assign to a slice doesn't have to be the same size as the slice - it simply replaces it even if it is a different size.

Basic array operations

So far so good, and it looks as if using a list is as easy as using an array. The first thing that we tend to need to do is to scan through an array and examine values. For example, to find the maximum value (forgetting for a moment that there is a built-in max function) you could use:

m=0
for e in myList:
 if m<e:
  m=e

This uses the for..in construct to scan through each item in the list. This is a very useful way to access the elements of an array but it isn't the one that most programmers will be familiar with. In most cases arrays are accessed by index and you can do this in Python:

m=0
for i in range(len(myList)):
 if m<myList[i]:
  m=myList[i]

Notice that we are now using range to generate the sequence 0,1, and so on up to the length of myList.

You have to admit that there is a lot to be said for the simplicity of the non-index version of the for loop but in many cases the index of the entry is needed. There is the option of using the index method to discover the index of an element but this has its problems.

For example if you wanted to return not the maximum element but its index position in the list you could use:

m=0
mi=0
for i in range(len(myList)):
 if m<myList[i]:
  m=myList[i]
  mi=i

or you could use the non-indexed loop and the index method:

m=0
for e in myList:
 if m<e:
  m=e
mi=myList.index(m)
print mi

The only problem is that index(m) returns the index of m in the list or an error if m isn't in the list. There is also the slight problem that behind the scenes it is clear that index is performing another scan of the entire list.


Related Solutions

Compare PHP code used to create and update two-dimensional arrays to another programming language. Which is...
Compare PHP code used to create and update two-dimensional arrays to another programming language. Which is easier and more efficient? Why? Can be compared to any other well used language.
discuss the relationship between history and social welfare policy. Please give a different response other than...
discuss the relationship between history and social welfare policy. Please give a different response other than the ones posted .
Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages...
Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages which support the user of the programming language deciding which to use when creating their method. (Programming Languages)
Please code the arrays. This is in PHP/HTML Perform some calculations: 1. $ages = [24, 12,...
Please code the arrays. This is in PHP/HTML Perform some calculations: 1. $ages = [24, 12, 10, 17, 23, 29, 35, 32, 30, 22] Using a FOR loop, calculate the sum and average of these numbers, then print them. Please provide legible output. 2. If I change the array to $ages = [1 => 24, 12, 10, 17, 23, 29, 35, 32, 30, 22] How will the program change? Re-run the program and provide output 3. Assume that the above...
Discuss the shape of a protein and how this dictates function. Please explain in depth.
Discuss the shape of a protein and how this dictates function. Please explain in depth.
Programming for business applications - (New to programming. Just learned arrays, for/if/ifelse/else, and tryParse method. Please...
Programming for business applications - (New to programming. Just learned arrays, for/if/ifelse/else, and tryParse method. Please put comments so I am able to follow along with ease while doing this project.) C# is the language Visual Studio 2019. Create a C# Console app (using .NET Framework) Create a credit card console app. Each card has a Name, FICA score, and Credit Balance. Prompt the user for the number of cards in this group Declare/define 3 arrays (Names, FICA Scores, Balances)...
(a) How Context Free languages are different from Regular languages. (b) What are the automata accepting...
(a) How Context Free languages are different from Regular languages. (b) What are the automata accepting those languages and how they are different from DFA and NFA. (c) How the grammar of Context Free Languages looks different from those in Regular languages.
Web Programming: Explain how a session actually works in PHP, including how the client and server...
Web Programming: Explain how a session actually works in PHP, including how the client and server use the session ID to identify the session Then, compare and contrast cookies and sessions as a means of storing state information for a given user. Thank you
1. Look for information about two other object-oriented programming languages and their program development environments. Briefly...
1. Look for information about two other object-oriented programming languages and their program development environments. Briefly describe them other than Visual Basic. 2. Explain the difference between a Compiler and an Interpreter in Visual Basic
1.In C++, C#, Java. Discuss string operations functions, with examples. Then make Comparison of programming languages  ...
1.In C++, C#, Java. Discuss string operations functions, with examples. Then make Comparison of programming languages   2.String and StringBuffer (C#, Java) with examples.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT