cobusbo Posted September 12, 2015 Share Posted September 12, 2015 (edited) Hi I'm trying to create an array to put it into a string like the following $marray = array( '1' => 'apple,', '2' => 'orange,', '3' => ' ', '4' => ' ', '5' => 'apple'); And the string should look like $mrules = "Welcome Moderator please read the following rules. " . 1. Apple, 2. Orange, 5. Apple. The empty values in the array shouldn't be showed any assistance please Edited September 12, 2015 by cobusbo Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 12, 2015 Share Posted September 12, 2015 $marray = array( '1' => 'apple,', '2' => 'orange,', '3' => ' ', '4' => ' ', '5' => 'apple'); //just values $trimmedArray = array_filter(array_map('trim', $marray)); $mrules = "Welcome Moderator please read the following rules. ".implode(" ",$trimmedArray); echo $mrules."<br />"; //alternate if need keys $mrules = "Welcome Moderator please read the following rules. "; foreach($marray as $key=>$value){ if(trim($value) !=''){ $mrules .= $key.". ".$value." "; } } echo $mrules; 1 Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted September 12, 2015 Solution Share Posted September 12, 2015 (edited) $marray = array( '1' => 'apple,', '2' => 'orange,', '3' => ' ', '4' => ' ', '5' => 'apple'); $tmp = array_filter(array_map('trim', $marray)); // remove blanks $str = ''; foreach ($tmp as $k=>$v) { $str .= "$k. $v "; } echo $str; //==> 1. apple, 2. orange, 5. apple [edit] Damn! Beaten to the post. Edited September 12, 2015 by Barand Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 12, 2015 Share Posted September 12, 2015 A first time maybe!! Quote Link to comment Share on other sites More sharing options...
cobusbo Posted September 12, 2015 Author Share Posted September 12, 2015 $marray = array( '1' => 'apple,', '2' => 'orange,', '3' => ' ', '4' => ' ', '5' => 'apple'); $tmp = array_filter(array_map('trim', $marray)); // remove blanks $str = ''; foreach ($tmp as $k=>$v) { $str .= "$k. $v "; } echo $str; //==> 1. apple, 2. orange, 5. apple [edit] Damn! Beaten to the post. Ah thank you after making a few changes I got it working $marray = array( '1' => 'apple', '2' => 'orange', '3' => ' ', '4' => ' ', '5' => 'apple'); $tmp = array_filter(array_map('trim', $marray)); // remove blanks $str = ''; foreach ($tmp as $k=>$v) { $str .= "$k. $v, "; } $mrules = "Welcome Moderator please read the following rules. " . $str; ///=> Welcome Moderator please read the following rules. 1. apple, 2. orange, 5. apple, Quote Link to comment 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.