Hence storing two different information in one array element. We can save more data, as we can have a string as key to the array element, where we can have associated data to the value to be stored, like in our example, we stored the type of the car as key along with the name of the car as value.We can provide more meaningful key or index values to our array elements.Here are a few advantages of using associative array in our program/script: " car \n" Äon't get confused by the syntax as $key=>$value, it means in every iteration of the foreach we are representing the array as key-value pair. Using foreach to traverse an associative array is a better approach if you have to traverse the whole array, as we do not have to bother about calculating the size of the array to loop around the array.Ä«elow we have used the foreach to traverse the $lamborghinis array. While using the for loop to traverse an associative array we must know the size/length of the array, which can be found using the count() function.Īlso, as the index in associative array is not numeric and not sequentially, hence to find the index values or keys(as data saved in associative array is in the form of key-value), we can use the function array_keys() to get an array of the keys used in the associative array.įollowing is the syntax for traversing an array using the for loop. However, an array is a form of data structure that has a single name. I think im creating an associative array with the username (rowbert) as the key. An object represents a kind of instance and is a class specimen with memory allocated. To know the syntax and basic usage of for and foreach loop, you can refer to the PHP for and foreach loop tutorial.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |