Jump to content

Array algorithm question


INTPnerd

Recommended Posts

I don't understand how arrays work in php on an algorithm and data structures level.  Here is some code in c# and something similar in php

 

c# code:

 

int[] intArray = new int[100];

for(int i = 0; i < 100; i++) {

    intArray = i;

}

 

php code:

 

$intArray = array();
for($i = 0; $i < 100; $i++) {
     $intArray[$i] = $i;
}

 

The c# code is obviously efficient as far as the algorithm, but I have no idea what is actually happening with the php code or how efficient it is.  Please help me understand.  thanks.

Link to comment
https://forums.phpfreaks.com/topic/89759-array-algorithm-question/
Share on other sites

I really don't know what you are really asking. The PHP code is nearly identical to the C# code, so why are you unsure "what" is happening in the PHP code. The first line declares a new array object (you do not need to assign a size to the array - it is just an empty array). The loop then itterates from 0 to 99 and creates an item int he array and assigns a value to it in one step.

Thanks for the help.  I am trying to understand what is happening under the covers.  I guess the part that is confusing me is that I don't specify an initial size for the array.  To achieve something that is similarly dynamic in C# Java I would need to use some kind of array list or linked list.  Are either of those data structures similar to what php uses for arrays?

Thanks.  I looked at that a while back, and I could remember that it did not help much, so I did not look at it again this time.  Now that I look at it again I remember why it did not help.  I don't know what a map data structure is.  I guess I should have studied harder in my computer science classes.  Thanks for the help.

@INTPnerd,

 

I understand what you mean. I had similar difficulties when dealing with a language that had very tightly structured arrays for the first time (i.e. had to set the size when creating and changing the size was a chore). But, PHP is much easier IMHO.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.