Jump to content

Array to string


cobusbo
Go to solution Solved by Barand,

Recommended Posts

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 by cobusbo
Link to comment
Share on other sites


$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;
  • Like 1
Link to comment
Share on other sites

  • Solution
$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 by Barand
Link to comment
Share on other sites

$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, 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.