Jump to content

help using IMPLODE() function


ceci

Recommended Posts

I have a form that passes the following array 'fruits'. The ARRAY can contain up to 30 items ( or 30 different fields ).

It gets store in the following format: |oranges | apples | pears |... into MySQL.

 

Before saving to MySQL, I do following:

if(is_array($fruits)){
$fruitarray = "|" . implode("|",$fruits));
}

 

Now if I

print_r($_POST); 

I see the following:

 

[fruits] => Array
        (
            [1] => |oranges 
            [2] => apples 
            [3] => pears
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
            [18] => 
            [19] => 
            [20] => 
            [21] => 
            [22] => 
            [23] => 
            [24] => 
            [25] => 
            [26] => 
            [27] => 
            [28] => 
            [29] => 
            [30] => 
        )

 

Here it the problem. On the database, I get extra "|"s at the end. 

|oranges | apples | pears |||||||||||||||||||||||| How can I get rid of these extra pipes

 

Is there a some kind of unset($fruits) or something to ignore empty arrays?

 

Thank you for in advance.

 

ceci

Link to comment
Share on other sites

I tried your code and this is what I get:

|item 1|||||||||||||||||||||||||||||

 

print_r($fruits) returns this

 [fruts] => Array
        (
            [1] => item 1
            [2] => 
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
            [18] => 
            [19] => 
            [20] => 
            [21] => 
            [22] => 
            [23] => 
            [24] => 
            [25] => 
            [26] => 
            [27] => 
            [28] => 
            [29] => 
            [30] => 
        )

 

I know the empty arrays are causing the problem. I thought the is_array() function would take care of that. No?

 

thank you for your help.

 

Ceci

Link to comment
Share on other sites

You'll need to basically "clean" the array:

 

function array_clean($array){
$tmp = array();
foreach($array as $element){
	if(str_replace(" ", "", $element) != ""){
		$tmp[] = $element;
	}
}
return $tmp;
}

$fruits = array_clean($_POST['fruits']);

$fruitarray = "|" . implode("|", $fruits);

 

So basically you just loop through and make sure that each element in the array has a value, if it does then add it to a temporary variable, then return that variable.

 

Good luck.

Link to comment
Share on other sites

Thank you! That fixed that problem.

 

My other issue I noticed is on the update.  Every time I update I get an extra "|" added at the beginning. For instance, this is my new string stored on the MySQL database after 3 updates.

 

|||new item|item 2|item3

 

My other question is, how can I ensure that the items stored are always very tight(no whitespaces between the items) like this:

 

DESIRED: 

|item 1|item 2|item 3|item 4|

 

NOT DESIRED: (notice spaces) 

|item 1 |item 2 | item 3|item 4|

 

Thanks again for saving my life.

 

Ceci

Link to comment
Share on other sites

$fruits = array_filter(array_clean($_POST['fruits']));

Do a print_r and see if you can use it like that.

 

Otherwise just

$fruits = array_clean($_POST['fruits']);

$new_fruits = array_filter($fruits);

$fruitarray = "|" . implode("|",$new_fruits));

 

 

HTH

Teamatomic

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.