cfgcjm Posted August 4, 2008 Share Posted August 4, 2008 I've passed data into an array (and i've triple checked to make sure it is in there) and i'm trying to remove the duplicates. The echo is "5-A******" which is incorrect. It should be "5-A*6-B*7-A*7-B*8-A" Diagnosis welcome... My code is as follows: sort($homerooms); $cleanhr=array_unique($homerooms); echo $cleanhr[0][0]; echo"*"; echo $cleanhr[1][0]; echo"*"; echo $cleanhr[2][0]; echo"*"; echo $cleanhr[3][0]; echo"*"; echo $cleanhr[4][0]; echo"*"; echo $cleanhr[5][0]; echo"*"; echo $cleanhr[6][0]; Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/ Share on other sites More sharing options...
genericnumber1 Posted August 4, 2008 Share Posted August 4, 2008 Can you show us what's in your array? Maybe a print_r() or a var_dump() of it? How you're doing it now looks like it's a multidimensional array, but it's hard for us to judge what you're doing wrong if we don't know the data you're trying to manipulate Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-607989 Share on other sites More sharing options...
ohdang888 Posted August 4, 2008 Share Posted August 4, 2008 ^^ ah beat me to it. do this before and after you clean it print_r($homerooms); and copy and paste what you get Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-607990 Share on other sites More sharing options...
cfgcjm Posted August 4, 2008 Author Share Posted August 4, 2008 Array ( [0] => Array ( [0] => 5-A ) [1] => Array ( [0] => 6-B ) [2] => Array ( [0] => 6-B ) [3] => Array ( [0] => 7-A ) [4] => Array ( [0] => 7-B ) [5] => Array ( [0] => 8-A ) ) and you're right it is coded as multi-dimensional however only one dimension is being used Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608001 Share on other sites More sharing options...
DarkWater Posted August 4, 2008 Share Posted August 4, 2008 Try $cleanhr[0][0], $cleanhr[0][1], $cleanhr[0][2], etc. Based on your array dump, it should work. EDIT: Okay yeah, that DID post strange then. Try what I just said, but now I'm thinking that it won't work. Give it a shot anyway. Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608004 Share on other sites More sharing options...
cfgcjm Posted August 4, 2008 Author Share Posted August 4, 2008 That posted strange Array ( [0] => Array ( [0] => 5-A ) [1] => Array ( [0] => 6-B ) [2] => Array ( [0] => 6-B ) [3] => Array ( [0] => 7-A ) [4] => Array ( [0] => 7-B ) [5] => Array ( [0] => 8-A ) ) Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608005 Share on other sites More sharing options...
lemmin Posted August 4, 2008 Share Posted August 4, 2008 sort($homerooms[0]); $cleanhr=array_unique($homerooms[0]); If you don't have the [0] you are only checking a list of 1 array which does nothing. Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608010 Share on other sites More sharing options...
cfgcjm Posted August 4, 2008 Author Share Posted August 4, 2008 Changed it to sort($homerooms[0]); $cleanhr=array_unique($homerooms[0]); echo $cleanhr[0][0]; echo"*"; echo $cleanhr[1][0]; echo"*"; echo $cleanhr[2][0]; echo"*"; echo $cleanhr[3][0]; echo"*"; echo $cleanhr[4][0]; echo"*"; echo $cleanhr[5][0]; echo"*"; echo $cleanhr[6][0]; and it now gave me "6******"...odd Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608012 Share on other sites More sharing options...
ohdang888 Posted August 4, 2008 Share Posted August 4, 2008 echo $cleanhr[0][0]; echo"*"; echo $cleanhr[0][1]; echo"*"; echo $cleanhr[0][2]; echo"*"; echo $cleanhr[0][3]; echo"*"; echo $cleanhr[0][4]; echo"*"; echo $cleanhr[0][5]; echo"*"; echo $cleanhr[0][6]; Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608015 Share on other sites More sharing options...
cfgcjm Posted August 4, 2008 Author Share Posted August 4, 2008 "6*-*B****" Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608016 Share on other sites More sharing options...
cfgcjm Posted August 4, 2008 Author Share Posted August 4, 2008 I got it! The second dimension in the array seemed to be screwing it up. I went back into it and completely removed the second dimension from existence. Now i've got 5-A*6-B**7-A*7-B*8-A* which is correct. Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608019 Share on other sites More sharing options...
lemmin Posted August 4, 2008 Share Posted August 4, 2008 That is because there wasn't a second dimension in the array after you set the variable equal to only one of the dimensions. Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608021 Share on other sites More sharing options...
.josh Posted August 4, 2008 Share Posted August 4, 2008 That is because there wasn't a second dimension in the array after you set the variable equal to only one of the dimensions. Umm, where did he do that? It didn't work because the way array_unique works is it removes duplicates inside a single level array, but the values were inside separate arrays (which is inside a master array). Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608032 Share on other sites More sharing options...
lemmin Posted August 4, 2008 Share Posted August 4, 2008 Right here: $cleanhr=array_unique($homerooms[0]); Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608069 Share on other sites More sharing options...
.josh Posted August 5, 2008 Share Posted August 5, 2008 Well he was supposed to do that because it's a 2d array. It's a 2d array with only 1 position for the first dimension. Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608088 Share on other sites More sharing options...
lemmin Posted August 5, 2008 Share Posted August 5, 2008 I know, I told him to do it. I was just explaining why he got the error message. Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608655 Share on other sites More sharing options...
.josh Posted August 5, 2008 Share Posted August 5, 2008 Oh okay I didn't read that far back, sorry. Quote Link to comment https://forums.phpfreaks.com/topic/118169-array_unique-deletes-all-but-first-value/#findComment-608657 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.