Jump to content

searching for "a needle in a haystack"


Archimedees

Recommended Posts

 

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;

 

}

 

Link to comment
https://forums.phpfreaks.com/topic/73292-searching-for-a-needle-in-a-haystack/
Share on other sites

 

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.

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
)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.