ceci Posted February 28, 2010 Share Posted February 28, 2010 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 Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 1, 2010 Share Posted March 1, 2010 You have an array with many empty values: $new_fruits = array_values($fruits); $fruitarray = "|" . implode("|",$new_fruits)); HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
ceci Posted March 1, 2010 Author Share Posted March 1, 2010 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 Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted March 1, 2010 Share Posted March 1, 2010 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. Quote Link to comment Share on other sites More sharing options...
ceci Posted March 1, 2010 Author Share Posted March 1, 2010 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 Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 1, 2010 Share Posted March 1, 2010 you're right, my bad. array_filter() HTH Teamatomic Quote Link to comment Share on other sites More sharing options...
ceci Posted March 1, 2010 Author Share Posted March 1, 2010 would that function be used as follows? $fruits = array_filter(array_clean($_POST['fruits'])); Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted March 1, 2010 Share Posted March 1, 2010 Ah, use array_filter() instead of that function I gave you. Also look at trim to remove whitespace. Quote Link to comment Share on other sites More sharing options...
teamatomic Posted March 1, 2010 Share Posted March 1, 2010 $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 Quote Link to comment Share on other sites More sharing options...
ceci Posted March 1, 2010 Author Share Posted March 1, 2010 Thank you kindly. And I will try to posting understandable questions. Thanks again. C 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.