saynotojava Posted January 3, 2014 Share Posted January 3, 2014 I have following two dimensional array: $shop = array( array( Title => "rose", Price => 1.25, Number => 15 ), array( Title => "daisy", Price => 0.75, Number => 25, ), array( Title => "orchid", Price => 1.15, Number => 7 ) ); Where i want to unset/remove one array in it,i do that with following: $count=3; unset ($shop[0]); And i output array after that with this: for ($row = 0; $row < $count; $row++){ echo $shop[$row]["Title"]." costs ".$shop[$row]["Price"]." and you get ".$shop[$row]["Number"]; echo "<br />";} The problem is how with unset array is not fully removed,it just show as empty value,so while it should output array like this: daisy costs 0.75 and you get 25orchid costs 1.15 and you get 7 It shows this: costs and you getdaisy costs 0.75 and you get 25orchid costs 1.15 and you get 7 And if i output array with print_r,then it shows properly but i need it with echo format. Quote Link to comment https://forums.phpfreaks.com/topic/285054-removing-empty-array-after-unset-with-for-and-echo/ Share on other sites More sharing options...
Solution PaperTiger Posted January 3, 2014 Solution Share Posted January 3, 2014 Because you set the count as 3 before you unset the first element, so it will always loop through 3 times. You don't really need the count at all. You can just do: unset ($shop[0]); foreach($shop as $item) { echo $item["Title"]." costs ".$item["Price"]." and you get ".$item["Number"]; echo "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/285054-removing-empty-array-after-unset-with-for-and-echo/#findComment-1463680 Share on other sites More sharing options...
Ch0cu3r Posted January 3, 2014 Share Posted January 3, 2014 Alternately you could initialize $row to 1 for ($row = 1; $row < $count; $row++) { echo $shop[$row]["Title"]." costs ".$shop[$row]["Price"]." and you get ".$shop[$row]["Number"]; echo "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/285054-removing-empty-array-after-unset-with-for-and-echo/#findComment-1463684 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.