Jump to content

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.

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.