Archimedees Posted October 15, 2007 Share Posted October 15, 2007 I have two arrays, $arCurrentHost and $arCurrentServer they both contains list of servers. Now, some values inthe first array are also in the second array. I want to create a third array containing values in both lists. First, I assign the keys in the second array to a $needle and walk through the first array to see if there is a hit. If hit, we do nothing, if no hit, we add the needle to $arCurrentHost. Can somebody please pinpoint why this function is not returning the results I want? Please? Thanks. function create_list($arNewCurrentHost) { if (isset($arCurrentHost) && (isset($arCurrentServer)) { $arNewCurrentHost = $arCurrentHost; $i=0; $MaxCurrentServer = count($arCurrentServer); for($i=0; $i<$MaxCurrentServer; $i++) { $needle = $arCurrentServer [$i]; } array_search($arNewCurrentHost, $needle); foreach ($arNewCurrentHost as $key => $value) { if($value == $needle) { //do nothing } else { array_push($arNewCurrentHost, $needle ); } } } else { die(); } return $arNewCurrentHost; } Quote Link to comment https://forums.phpfreaks.com/topic/73292-searching-for-a-needle-in-a-haystack/ Share on other sites More sharing options...
zq29 Posted October 15, 2007 Share Posted October 15, 2007 The function array_intersect() does what you want. Quote Link to comment https://forums.phpfreaks.com/topic/73292-searching-for-a-needle-in-a-haystack/#findComment-369801 Share on other sites More sharing options...
Archimedees Posted October 15, 2007 Author Share Posted October 15, 2007 array_intersect gives an array containing elements in first array that are also in second array, leaving out the elements in first array that are not in second array and vice-versa. I want to have an array containing all the elements in first and all the elements in second array. If the elements in second array already exist in first array we just do nothing but if not we add them to the first array. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/73292-searching-for-a-needle-in-a-haystack/#findComment-369804 Share on other sites More sharing options...
Barand Posted October 15, 2007 Share Posted October 15, 2007 do you mean <?php $array1 = array(1,2,3); $array2 = array(2,3,4); $arr = array_unique (array_merge ($array1, $array2)); echo '<pre>', print_r($arr, true), '</pre>'; ?> --> Array ( [0] => 1 [1] => 2 [2] => 3 [5] => 4 ) Quote Link to comment https://forums.phpfreaks.com/topic/73292-searching-for-a-needle-in-a-haystack/#findComment-369809 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.