brooksh Posted March 21, 2012 Share Posted March 21, 2012 How can I split this data and then remove it? I need to split data by ; then split each one of those. $data = "page=1&data=1:21;2:34;5:57&a=yes"; So I will get: $category[0] = 1; $sub_category[0] = 21; $category[1] = 2; $sub_category[1] = 34; $category[2] = 5; $sub_category[2] = 57; Quote Link to comment https://forums.phpfreaks.com/topic/259395-how-to-split-data/ Share on other sites More sharing options...
new_g33k Posted March 21, 2012 Share Posted March 21, 2012 <?php $data="1:21;2:34;5:57"; $data=split(";",$data); foreach($data as $c) { $result=split(":",$c); $category[]=$result[0]; $sub_category[]=$result[1]; } print_r ($category); print_r ($sub_category); ?> Greetings from new_g33k! Quote Link to comment https://forums.phpfreaks.com/topic/259395-how-to-split-data/#findComment-1329740 Share on other sites More sharing options...
mofm Posted March 21, 2012 Share Posted March 21, 2012 http://php.net/manual/en/function.explode.php Explode splits a string into an array e.g. <?php $string="1&2&3&4&5&6&7"; $array=explode("&",$string); ?> Array will be 7 in lenght containg 1 2 3 4 5 6 7 so ur example would be <?php $data = "page=1&data=1:21;2:34;5:57&a=yes"; $array=explode(";",$data): //array[0] will be 1:21 //array[1] will be 2:34 //ect u get the idea, it simple from then on. //just loop though the array do as u wish then use implode to join them back to string if needed. foreach ($array as $k => $v) { // do what u want to the data here } $string=implode($array); ?> Notes on implode http://php.net/manual/en/function.implode.php ; If u want to remove elements from an array you can use unset($array[4]). http://php.net/manual/en/function.unset.php Quote Link to comment https://forums.phpfreaks.com/topic/259395-how-to-split-data/#findComment-1329798 Share on other sites More sharing options...
brooksh Posted March 21, 2012 Author Share Posted March 21, 2012 <?php $data = "page=1&data=1:21;2:34;5:57&a=yes"; $array=explode(";",$data): //array[0] will be 1:21 //array[1] will be 2:34 //ect u get the idea, it simple from then on. //just loop though the array do as u wish then use implode to join them back to string if needed. foreach ($array as $k => $v) { // do what u want to the data here } $string=implode($array); ?> $array[0] = page=1&data=1:21;2:34 How can I get rid of the page=1&data= Quote Link to comment https://forums.phpfreaks.com/topic/259395-how-to-split-data/#findComment-1329903 Share on other sites More sharing options...
brooksh Posted March 22, 2012 Author Share Posted March 22, 2012 $data = preg_match("/data=(.*)&/", $data, $matches); Quote Link to comment https://forums.phpfreaks.com/topic/259395-how-to-split-data/#findComment-1330051 Share on other sites More sharing options...
mofm Posted March 27, 2012 Share Posted March 27, 2012 Although its Solved it does not really help u out that much . Have a look at substr() http://php.net/manual/en/function.substr.php some light reading: http://www.tizag.com/phpT/php-string-strpos.php Quote Link to comment https://forums.phpfreaks.com/topic/259395-how-to-split-data/#findComment-1331726 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.