dcole.ath.cx Posted June 19, 2006 Share Posted June 19, 2006 if I have a dynamic array that I don't know how many places it has, how can I create another array that has the same number of places with the same thing in each:like if array: abc, def, ghi, jklthe new array would be: 0, 0, 0, 0but the array could have been: abcor: abc, def, ghi, jkl, mno, pqr, stu, vwxthen array 0 would have been made or 0, 0, 0, 0, 0, 0, 0, 0 Link to comment Share on other sites More sharing options...
aspnetguy Posted June 19, 2006 Share Posted June 19, 2006 you wan thte new array to be the same length as the first array? $array2 = array();for($i=0;$i<sizeof($array1);$i++){ $array2[] = "";} This would work...but I am sure there must be a better method. Link to comment Share on other sites More sharing options...
ThePsion5 Posted June 19, 2006 Share Posted June 19, 2006 This should work. $count = count($firstArray);$newArray = array_fill(0, $count, '') Link to comment Share on other sites More sharing options...
skym Posted June 20, 2006 Share Posted June 20, 2006 Just a notice for dcole.ath.cx: aspnetguy used sizeof(), ThePsion5 used count(), these two functions are completely the same, they both count the elements in an array. And I think it's actually $newArray = array_fill(0, $count, 0) if you want the 0 values in the array from the start, and not empty strings. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now