freelance84 Posted May 12, 2010 Share Posted May 12, 2010 Is there a built in function to split long arrays? I have the following array: Array ( [0] => aa [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => aa [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 1 [16] => aa [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 2 ) This is quite a short version. I need to be able to split the array when ever there is a value "aa" I know I could put this into another loop and array_push but a built in function would be a lot neater if anyone knows of one? NB I did find one function on phpmanual but it said: Warning This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged. Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/ Share on other sites More sharing options...
aeroswat Posted May 12, 2010 Share Posted May 12, 2010 While I'm sure you could use a clever mix of explode and implode and a string function to do what you want to do I don't believe there is a built in function that will do this. You'd be better off just looping. It'd be much easier. Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/#findComment-1057089 Share on other sites More sharing options...
freelance84 Posted May 12, 2010 Author Share Posted May 12, 2010 yea I thought as much. Ok, using loops: $counted = count($all_combinations); print_r($all_combinations); for($a = 0 ; $a < $counted ; ++$a) { if($all_combinations[$a] == 'aa') { echo "<br />"; } else { echo $all_combinations[$a]; } } The result of this is: Array( [ 0] => aa [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => aa [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 1 [16] => aa [17] => 0 [18] => 0 [19] => 0 [20] => 0 [21] => 0 [22] => 0 [23] => 2 ) 1 2 As you can see, for some reason the code is not printing any of the zeros but instead printing a line break. The problem is, zero is an important figure. Any ideas why this is happening? Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/#findComment-1057103 Share on other sites More sharing options...
aeroswat Posted May 12, 2010 Share Posted May 12, 2010 Where are the 1 and 2 coming from? Echo $counted before the loop and tell me what it says. Edit: Nevermind I see where the 1 and 2 are coming from Not sure if it would make a difference but try using a foreach loop instead of a for loop with a counter Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/#findComment-1057120 Share on other sites More sharing options...
freelance84 Posted May 12, 2010 Author Share Posted May 12, 2010 I really don't know where the zero's were going but i've redesigned how the $all_combinations array is produced and the problem doesn't exist any more. Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/#findComment-1057129 Share on other sites More sharing options...
aeroswat Posted May 12, 2010 Share Posted May 12, 2010 I really don't know where the zero's were going but i've redesigned how the $all_combinations array is produced and the problem doesn't exist any more. lol as long as its fixed if you post your old code where you are building it and your new code we can probably explain what was going on to help you not replicate the mistake in the future Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/#findComment-1057135 Share on other sites More sharing options...
freelance84 Posted May 12, 2010 Author Share Posted May 12, 2010 here's the working code. The changed method of producing the array is the section commented "//getting all possible combinations" http://www.phpfreaks.com/forums/index.php/topic,297730.0.html The new method results in each stage in the array being a string of 7numbers separated by a , The old way put each number as a separate value in the array. Still don't understand though why the above resulted in ignoring the value zero Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/#findComment-1057229 Share on other sites More sharing options...
Psycho Posted May 12, 2010 Share Posted May 12, 2010 I think you have uncovered a bug with the array_search() function! It seems that array_search() will return the key of the first values that matches the $needle OR the numerical value of 0. Here is the result of some testing I did: $array = array(0 => 'a', 1 => '0', 2 => 'b', 3 => '1', 4 => 'c'); echo array_search('c', $array); //Returns 4, correct //Change the string '0' to the number 0, for index 1 $array = array(0 => 'a', 1 => 0, 2 => 'b', 3 => '1', 4 => 'c'); echo array_search('c', $array); //Returns 1, incorrect Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/#findComment-1057251 Share on other sites More sharing options...
Psycho Posted May 12, 2010 Share Posted May 12, 2010 It seems this issue has already been reported as a bug (http://bugs.php.net/bug.php?id=30473), but I think the author did a poor job of explaining it as the issue was determined not to be a bug. Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/#findComment-1057292 Share on other sites More sharing options...
freelance84 Posted May 13, 2010 Author Share Posted May 13, 2010 Just my luck! Ha! Well thankfully I have worked around it. I had a look at the bug link however, as i've only been doing php and html for about a month I think I may have to come back to understanding this at a later date when my knowledge of it all is a little more secure. Thanks for having a look though Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/#findComment-1057694 Share on other sites More sharing options...
Psycho Posted May 13, 2010 Share Posted May 13, 2010 Just an FYI, I posted a question in the Guru forum (which you can't see) asking why this would not be a bug. Apparently the issue is that when in_array() and array_search() compare a string and an int, the string is converted to an int. If the string starts with a number, then it will have a numerical value. Otherwise, it is treated as a zero. Those functions do have a "strict" parameter to prevent this. However, that would also cause '3' != 3 Quote Link to comment https://forums.phpfreaks.com/topic/201489-explode-an-array/#findComment-1057717 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.