hassank1 Posted November 2, 2008 Share Posted November 2, 2008 I want to create a dynamic array ex: words[] and then later.. (when certain event occurs) I want to add elements to it.. how can I do that? Link to comment https://forums.phpfreaks.com/topic/131105-dynamic-array/ Share on other sites More sharing options...
bobbinsbro Posted November 2, 2008 Share Posted November 2, 2008 to add an element to on to the end of the array: $words[] = "new element"; or array_push($words, "new element 1", "new element 2"....); to add an element to the head of the array: array_unshift($words, "new element"); to add an element in the middle of an array: array_splice($words, $position, 0, "new element"); where $position is the index at which you want to insert the new element. Link to comment https://forums.phpfreaks.com/topic/131105-dynamic-array/#findComment-680682 Share on other sites More sharing options...
CroNiX Posted November 2, 2008 Share Posted November 2, 2008 $words = array(); // do your stuff...then later $words[] = "hello"; // $words[0] will be 'hello' $words[] = "goodbye"; //words[1] will be 'goodbye' //loop through all of the words and print each one out on a separate line foreach($words as $word) { echo "$word<br />"; } Hope that helps. Link to comment https://forums.phpfreaks.com/topic/131105-dynamic-array/#findComment-680683 Share on other sites More sharing options...
hassank1 Posted November 2, 2008 Author Share Posted November 2, 2008 thanks guys..very helpful Link to comment https://forums.phpfreaks.com/topic/131105-dynamic-array/#findComment-680685 Share on other sites More sharing options...
hassank1 Posted November 2, 2008 Author Share Posted November 2, 2008 just one more thing..how can I reset this array (deleting all the elemenet) ? Link to comment https://forums.phpfreaks.com/topic/131105-dynamic-array/#findComment-680689 Share on other sites More sharing options...
wildteen88 Posted November 2, 2008 Share Posted November 2, 2008 use unset($words) or $words = array(); Link to comment https://forums.phpfreaks.com/topic/131105-dynamic-array/#findComment-680690 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.