INTPnerd Posted February 6, 2008 Share Posted February 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89759-array-algorithm-question/ Share on other sites More sharing options...
INTPnerd Posted February 6, 2008 Author Share Posted February 6, 2008 I guess I need the c# code to be in code brackets to show all the characters. c# code: int[] intArray = new int[100]; for(int i = 0; i < 100; i++) { intArray[i] = i; } Quote Link to comment https://forums.phpfreaks.com/topic/89759-array-algorithm-question/#findComment-459979 Share on other sites More sharing options...
kenrbnsn Posted February 6, 2008 Share Posted February 6, 2008 If you just want to fill an array with the numbers from 0 to 99, you should use the range() funciton: <?php $int = range(0,99); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/89759-array-algorithm-question/#findComment-459984 Share on other sites More sharing options...
Psycho Posted February 6, 2008 Share Posted February 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89759-array-algorithm-question/#findComment-459986 Share on other sites More sharing options...
INTPnerd Posted February 6, 2008 Author Share Posted February 6, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/89759-array-algorithm-question/#findComment-459991 Share on other sites More sharing options...
kenrbnsn Posted February 6, 2008 Share Posted February 6, 2008 Read this section in the manual. It describes what arrays are in PHP. Ken Quote Link to comment https://forums.phpfreaks.com/topic/89759-array-algorithm-question/#findComment-459993 Share on other sites More sharing options...
INTPnerd Posted February 6, 2008 Author Share Posted February 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89759-array-algorithm-question/#findComment-459998 Share on other sites More sharing options...
Psycho Posted February 6, 2008 Share Posted February 6, 2008 @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. Quote Link to comment https://forums.phpfreaks.com/topic/89759-array-algorithm-question/#findComment-460002 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.