corics15 Posted May 18, 2010 Share Posted May 18, 2010 hello everyone! i have two arrays, for simplicity i have an example: first array: Array ( [0] => 370 [1] => 368 [2] => 424 [3] => 496 [4] => 307 ) second array: Array ( [0] => 368 [1] => 496 ) all i want is, remove the elements on the first array based from the second array, thus having: final array: Array ( [0] => 370 [2] => 424 [4] => 307 ) ...please help me out...thanks... Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/ Share on other sites More sharing options...
kenrbnsn Posted May 18, 2010 Share Posted May 18, 2010 Take a look at the function array_intersect Ken Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060215 Share on other sites More sharing options...
ale8oneboy Posted May 18, 2010 Share Posted May 18, 2010 Take a look at the 'in_array()' function. Walk through your first array with a for each loop. Have it check to see if the current value exists in the second array. Throw the unique values into another array. http://php.net/manual/en/function.in-array.php Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060219 Share on other sites More sharing options...
MatthewJ Posted May 18, 2010 Share Posted May 18, 2010 $arr1 = array(370, 368, 424, 496, 307); $arr2 = array(368, 496); foreach($arr2 as $v2) { foreach($arr1 as $k => $v1) { if($v1 == $v2) { unset($arr1[$k]); } } } $arr3 = array_values($arr1); print_r($arr3); Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060227 Share on other sites More sharing options...
Alex Posted May 18, 2010 Share Posted May 18, 2010 use array_diff: $new = array_diff($arr1, $arr2); Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060232 Share on other sites More sharing options...
teamatomic Posted May 18, 2010 Share Posted May 18, 2010 Go with array_diff as AlexWD says. array_intersect will return what is found that matches the values of the second array. Not what you want. in_array will only return a single key of the found value. Not what you want. Note on array diff. It will diff multiple values. If you have the value of say"6" in 3 separate elements you will catch them all. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060238 Share on other sites More sharing options...
corics15 Posted May 19, 2010 Author Share Posted May 19, 2010 thanks for all your help! i went for MatthewJ suggestion! and now it's working perfectly just like i want it! cheers to this forum! Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060386 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 thanks for all your help! i went for MatthewJ suggestion! and now it's working perfectly just like i want it! cheers to this forum! wrong choice you should have gone for AlexWD's solution. It's stupid to write something that is already built-in. Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060425 Share on other sites More sharing options...
corics15 Posted May 19, 2010 Author Share Posted May 19, 2010 thanks for all your help! i went for MatthewJ suggestion! and now it's working perfectly just like i want it! cheers to this forum! wrong choice you should have gone for AlexWD's solution. It's stupid to write something that is already built-in. thanks ignace..no need to get furious. i also did AlexWD's solution. Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060431 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 I wasn't furious (bare in mind I'm not a native English speaker) I just pointed out that there is no need to write a function for something that already exists like when someone writes a function to get the extension of a filename instead of using pathinfo Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060453 Share on other sites More sharing options...
Tazerenix Posted May 19, 2010 Share Posted May 19, 2010 I wasn't furious (bare in mind I'm not a native English speaker) I just pointed out that there is no need to write a function for something that already exists like when someone writes a function to get the extension of a filename instead of using pathinfo lol, until recently i never even knew what basename() did. I'd split my urls into arrays and reverse em and take the first element as a new string. There are such useful inbuilt functions Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060458 Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 lol, until recently i never even knew what basename() did. I'd split my urls into arrays and reverse em and take the first element as a new string. There are such useful inbuilt functions Yeah like end which you clearly don't know either Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060468 Share on other sites More sharing options...
MatthewJ Posted May 19, 2010 Share Posted May 19, 2010 Yeah, I have never had a reason to use array_diff(), but it is obviously the better solution. I'm just happy the stupid code I wrote worked Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1060543 Share on other sites More sharing options...
Tazerenix Posted May 20, 2010 Share Posted May 20, 2010 lol, until recently i never even knew what basename() did. I'd split my urls into arrays and reverse em and take the first element as a new string. There are such useful inbuilt functions Yeah like end which you clearly don't know either lol, thats also quite embarrasing XD Link to comment https://forums.phpfreaks.com/topic/202185-remove-elements-in-array/#findComment-1061013 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.