Jump to content

explode an array?


freelance84

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

:confused:

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  :D

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.