geroido Posted August 23, 2008 Share Posted August 23, 2008 Hi I have a session variable called $_SESSION['cart'];. This variable will typically hold values in the form 69,69,69,65,73,75. When I echoed this variable this is the exact readout that I got. What I need to do is extract the distinct values as well as a count of those distinct values. So for the above example I would get 'There are 3 (69s), 1 (65), 1 (73) and 1 (75)'. I need this info to insert into my database for fields 'quantity(3,1,1,1,) ' and MenuItemID(69, 65, 73, 75). Any ideas? Link to comment https://forums.phpfreaks.com/topic/121007-how-to-extract-comma-delimited-items-from-a-variable/ Share on other sites More sharing options...
The Little Guy Posted August 23, 2008 Share Posted August 23, 2008 Try this: $string = '69,69,69,65,73,75'; $arr = explode(',',$string); $countVals = array_count_values($arr); print_r($countVals); Link to comment https://forums.phpfreaks.com/topic/121007-how-to-extract-comma-delimited-items-from-a-variable/#findComment-623782 Share on other sites More sharing options...
geroido Posted August 23, 2008 Author Share Posted August 23, 2008 Hi The little guy I got the following result from the echo which is great Array ( [69] => 3 [65] => 1 [73] => 1 [75] => 1 ) but how do I insert the different values into the database fields e.g. MenuItemID quantity 69 3 65 1 73 1 75 1 Link to comment https://forums.phpfreaks.com/topic/121007-how-to-extract-comma-delimited-items-from-a-variable/#findComment-623786 Share on other sites More sharing options...
The Little Guy Posted August 23, 2008 Share Posted August 23, 2008 $string = '69,69,69,65,73,75'; $arr = explode(',',$string); $countVals = array_count_values($arr); foreach($countVals as $key => $val){ mysql_query("INSERT INTO tbl_name (`MenuItemID`,`quantity`) VALUES ('$key','$val')"); } Link to comment https://forums.phpfreaks.com/topic/121007-how-to-extract-comma-delimited-items-from-a-variable/#findComment-623789 Share on other sites More sharing options...
geroido Posted August 23, 2008 Author Share Posted August 23, 2008 Thanks a lot. That's a great help. I'll test it now Link to comment https://forums.phpfreaks.com/topic/121007-how-to-extract-comma-delimited-items-from-a-variable/#findComment-623793 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.