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); Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 ) Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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. 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.