MDanz Posted October 28, 2010 Share Posted October 28, 2010 when i echo count($words) it is 1. but when i print_r($itemid) it displays this.. Array ( [2] => 17 [3] => 15 [4] => 17 ) when i count($words) it should echo 3. print_r($itemid); $words = implode(" ", $itemid); echo count($words); Link to comment https://forums.phpfreaks.com/topic/217131-help-with-implode/ Share on other sites More sharing options...
Andy-H Posted October 28, 2010 Share Posted October 28, 2010 You are imploding the array into a string then using count on it, which should be used on arrays. If you wish to count the values you need to do it before using implode Link to comment https://forums.phpfreaks.com/topic/217131-help-with-implode/#findComment-1127683 Share on other sites More sharing options...
Pikachu2000 Posted October 28, 2010 Share Posted October 28, 2010 You can also have a look at str_word_count(). It may work for you. Link to comment https://forums.phpfreaks.com/topic/217131-help-with-implode/#findComment-1127684 Share on other sites More sharing options...
Andy-H Posted October 28, 2010 Share Posted October 28, 2010 @OP $words = implode(" ", $itemid); echo count($itemid); Would also work. @Pikachu For the purpose of this function, 'word' is defined as a locale dependent string containing alphabetic characters, which also may contain, but not start with "'" and "-" characters. Array ( [2] => 17 [3] => 15 [4] => 17 ) Link to comment https://forums.phpfreaks.com/topic/217131-help-with-implode/#findComment-1127686 Share on other sites More sharing options...
MDanz Posted October 28, 2010 Author Share Posted October 28, 2010 thanks for the help. could i have help on this. $oldarray = implode(" ", $itemid); $newarray = explode(" ", $oldarray); i have to do the above to reset the array index to start at zero. how do i set a session array for $newarray? e.g. $newarray = 13 14 15 so i want the session array to be [0] =>13 [1] =>14 [2] =>15 Link to comment https://forums.phpfreaks.com/topic/217131-help-with-implode/#findComment-1127688 Share on other sites More sharing options...
Andy-H Posted October 28, 2010 Share Posted October 28, 2010 Use array_values $arrItemId= array_values($itemid); $_SESSION['desiredSessionName'] = $arrItemId; echo '<pre>' . print_r($_SESSION['desiredSessionName'], true) . '</pre>'; //to see result Link to comment https://forums.phpfreaks.com/topic/217131-help-with-implode/#findComment-1127691 Share on other sites More sharing options...
MDanz Posted October 28, 2010 Author Share Posted October 28, 2010 thanks alot... i tried array_values before but didn't get desired results.... thanks Link to comment https://forums.phpfreaks.com/topic/217131-help-with-implode/#findComment-1127692 Share on other sites More sharing options...
Pikachu2000 Posted October 28, 2010 Share Posted October 28, 2010 Somehow when I read that, my mind saw 'alphanumeric'. Alphabetic definitely makes more sense, though. LOL. Link to comment https://forums.phpfreaks.com/topic/217131-help-with-implode/#findComment-1127695 Share on other sites More sharing options...
Andy-H Posted October 28, 2010 Share Posted October 28, 2010 lol easy mistake to make. I would've missed it too if I didn't remember the example with 'fri3nd' split into 2 words. Link to comment https://forums.phpfreaks.com/topic/217131-help-with-implode/#findComment-1127700 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.