How to find a value in an array redux

ggjones
ggjones used Ask the Experts™
on



I’m trying to create a list of words, each one based on a sequence of conditions.

Importantly, none of the words can be duplicates.

I’m not having any luck this far.

I imagined that the comparison to the preceding words would occur in “function notExist”… but I sense some looping may be in order.

Any ideas???

Many thanks,

GJ

function notExist($keyWord) {
        if (sizeof($keywordArray) == 0 || !in_array($keyWord, $keywordArray)){ return true;} else {return false; }
    }

$Key1 = (isset($theCOne) ? $theCOne :
	(isset($theSFArray[0]) ? $theSFArray[0] :
	(isset($thePFArray[0]) ? $thePFArray[0] :
	(isset($theTArray[0]) ? $theTArray[0] :
	'no KEY1 available!'))));


$Key2 = (isset($theSFArray[0]) && notExist($theSFArray[0]) ? $theSFArray[0] :
	(isset($thePFArray[0]) && notExist($thePFArray[0]) ? $thePFArray[0] :
	(isset($theCTwo) && notExist($theCTwo) ? $theCTwo :
	(isset($theTArray[1]) && notExist($theTArray[1]) ? $theTArray[1] :
	'no KEY2 available!'))));array_push($keywordArray, $vpKey2);


$Key3 = (isset($thePFArray[0]) && notExist($thePFArray[0]) ? $thePFArray[0] :
	(isset($theCTwo) && notExist($theCTwo) ? $theCTwo :
	(isset($theSFArray[1]) && notExist($theSFArray[1]) ? $theSFArray[1] :
	(isset($theTArray[2]) && notExist($theTArray[2]) ? $theTArray[2] :
	'no KEY3 available!'))));


$Key4 = (isset($theCTwo) && notExist($theCTwo) ? $theCTwo :
	(isset($theSFArray[1]) && notExist($theSFArray[1]) ? $theSFArray[1] :
	(isset($thePFArray[1]) && notExist($thePFArray[1]) ? $thePFArray[1] :
	(isset($theTArray[3]) && notExist($theTArray[3]) ? $theTArray[3] :
	'no KEY4 available!'))));

$keywordArray = array($Key1, $Key2, $Key3, $Key4);


echo "<P>" .$Key1;
echo "<P>" .$Key2;
echo "<P>" .$Key3;
echo "<P>" .$Key4;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sandeep KothariProject Lead

Commented:
use array_unique to remove duplicate values from the array...
sample code :
$arr = array_unique($arr);

Open in new window

Author

Commented:
...t hanks kshna... but I don't want to remove duplicates after the fact, I want to avoid getting them in the first place!

regards,

GJ
Sandeep KothariProject Lead
Commented:
oh yes... you dont have $keywordArray defined in the notExist function thats the issue...

use global $keywordArray or pass it as a argument to the function !...

Author

Commented:
...t hanks again kshna...the problem is that it still allows duplicates!!

regards,

GJ
Sandeep KothariProject Lead

Commented:
can you print_r($keyworArray) in the function notExist

I want to check if you are getting that array as it is...

pls paste complete code so that I can see where the error is !
Sandeep KothariProject Lead

Commented:
also try trimming the keyword while comparing in in_array

so it should be like
 if (sizeof($keywordArray) == 0 || !in_array(trim($keyWord), $keywordArray))

Open in new window

Author

Commented:

thanks kshna.

You were right about the scope - the global declaration was necessary.

The other thing was to create the array dynamically, after the array value had been defined , through array_push.

This works ... but can you think of any efficiencies??

thanks,

GJ

.
global $keywordArray;

function notExist($keyWord) {

global $keywordArray;

       if (sizeof($keywordArray) == 0 || !in_array(trim($keyWord), $keywordArray)){ return true;} else {return false; }

    }

$Key1 = (isset($theCOne) ? $theCOne :
        (isset($theSFArray[0]) ? $theSFArray[0] :
        (isset($thePFArray[0]) ? $thePFArray[0] :
        (isset($theTArray[0]) ? $theTArray[0] :
        'no KEY1 available!'))));

$keywordArray = array(); array_push($keywordArray, $Key1);


$Key2 = (isset($theSFArray[0]) && notExist($theSFArray[0]) ? $theSFArray[0] :
        (isset($thePFArray[0]) && notExist($thePFArray[0]) ? $thePFArray[0] :
        (isset($theCTwo) && notExist($theCTwo) ? $theCTwo :
        (isset($theTArray[1]) && notExist($theTArray[1]) ? $theTArray[1] :
        'no KEY2 available!'))));array_push($keywordArray, $Key2);

array_push($keywordArray, $Key2);


$Key3 = (isset($thePFArray[0]) && notExist($thePFArray[0]) ? $thePFArray[0] :
        (isset($theCTwo) && notExist($theCTwo) ? $theCTwo :
        (isset($theSFArray[1]) && notExist($theSFArray[1]) ? $theSFArray[1] :
        (isset($theTArray[2]) && notExist($theTArray[2]) ? $theTArray[2] :
        'no KEY3 available!'))));

array_push($keywordArray, $Key3);


$Key4 = (isset($theCTwo) && notExist($theCTwo) ? $theCTwo :
        (isset($theSFArray[1]) && notExist($theSFArray[1]) ? $theSFArray[1] :
        (isset($thePFArray[1]) && notExist($thePFArray[1]) ? $thePFArray[1] :
        (isset($theTArray[3]) && notExist($theTArray[3]) ? $theTArray[3] :
        'no KEY4 available!'))));

array_push($keywordArray, $Key4);


echo "<P>" .$Key1;
echo "<P>" .$Key2;
echo "<P>" .$Key3;
echo "<P>" .$Key4;


.

Open in new window

Sandeep KothariProject Lead

Commented:
oh yes... instead of repeating the same set of code again and again write a function ...
something like this ...

function pushKeyword($arr){ 
    foreach($arr as $a){
       if(isset($a) && notExist($a)){
          array_push($keywordArray, $a);
          break;
       }
    }
}

$arr_key2 = array($val1,$val2,$val3,$val4);

Open in new window


this is just a pseudo code ... you should make changes as per your keyword logic...
Sandeep KothariProject Lead

Commented:
I missed one line in the above pseudo code ...
which is calling the function pushKeyword...

it goes like this...

pushKeyword($arr_key2);

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
As a general practice I would avoid using GLOBAL declarations for variable names.  If you really need a global variable, put it into a position in the array $GLOBALS.  Otherwise you're creating a condition that makes programming and debugging awkward and risks variable name collisions.  Trust me - I've done it wrong many times before.

I cannot understand the function posted with the question, but if you can post a test-case sample showing the input data and show us what the desired output would contain, then I am fairly sure we can find an answer for you.  Not sure if this code example will be helpful, so I'll look forward to seeing the test data, then we can respond with TDD ("Test-Driven Design").

http://www.laprbass.com/RAY_array_not_unique.php Outputs
THE VALUE abc@xyz.com APPEARED 3 TIMES
THE VALUE def@xyz.com APPEARED 2 TIMES
THE VALUE jkl@xyz.com APPEARED 2 TIMES
<?php // RAY_array_not_unique.php
error_reporting(E_ALL);
echo "<pre>";



// A FUNCTION TO FIND REPLICATED VALUES IN AN ARRAY
function array_not_unique($raw)
{
    // MAN PAGE: http://us.php.net/manual/en/function.array-count-values.php
    $new = array_count_values($raw);
    foreach ($new as $key => $val)
    {
       if ($val < 2) unset($new[$key]);
    }
    return $new;
}



// SOME TEST DATA
$raw_array   = array();
$raw_array[] = 'abc@xyz.com';
$raw_array[] = 'def@xyz.com';
$raw_array[] = 'ghi@xyz.com';
$raw_array[] = 'jkl@xyz.com';
$raw_array[] = 'mno@xyz.com';
$raw_array[] = 'pqr@xyz.com';
$raw_array[] = 'stu@xyz.com';

// SOME DUPLICATES
$raw_array[] = 'abc@xyz.com';
$raw_array[] = 'jkl@xyz.com';
$raw_array[] = 'abc@xyz.com';
$raw_array[] = 'def@xyz.com';



// SHOW THE FUNCTION AT WORK
$common = array_not_unique($raw_array);
foreach ($common as $x => $n)
{
    echo PHP_EOL . "THE VALUE $x APPEARED $n TIMES";
}

Open in new window

Author

Commented:

Thanks for the replies.

I have included, some data to populate, as Ray suggested.

The key issue is to arrive at a list of 4 words that are not duplicated.

The problem with looping, as I see it, is that the logic defining each $Key does NOT necessarily follow a set pattern. Assume the logic is unique for each $Key, in other words.

I should say that this test case is simply one of a wide variety. In most cases, one or more of the arrays (eg: $arrayOne) will not exist, for example, or the array will have only one or two elements.

many,many thanks for your help.

GJ


$arrayOne = array ('word1','word2','word3','word4','word5','word6','word7');
$arrayTwo = array ('word8','word2','word3','word9','word5','word10','word11');
$arrayThree = array ('word8','word1','word14','word13','word5','word14','word11');
$arrayFour = array ('word1','word2','word3','word9','word15','word16','word11');

global $keywordArray;

function notExist($keyWord) {

global $keywordArray;

       if (sizeof($keywordArray) == 0 || !in_array(trim($keyWord), $keywordArray)){ return true;} else {return false; }

    }

$Key1 = (isset($arrayOne[0]) ? $arrayOne[0] :
        (isset($arrayTwo[0]) ? $arrayTwo[0] :
        (isset($arrayThree[0]) ? $arrayThree[0] :
        (isset($arrayFour[0]) && notExist($arrayFour[0]) ? $arrayFour[0] :
        'no KEY1 available!'))));

$keywordArray = array(); array_push($keywordArray, $Key1);


$Key2 = (isset($arrayTwo[0]) && notExist($arrayTwo[0]) ? $arrayTwo[0] :
        (isset($arrayThree[0]) && notExist($arrayThree[0]) ? $arrayThree[0] :
        (isset($arrayOne[1]) && notExist($arrayOne[1]) ? $arrayOne[1] :
        (isset($arrayFour[1]) && notExist($arrayFour[1]) ? $arrayFour[1] :
        'no KEY2 available!'))));array_push($keywordArray, $Key2);

array_push($keywordArray, $Key2);


$Key3 = (isset($arrayThree[0]) && notExist($arrayThree[0]) ? $arrayThree[0] :
        (isset($arrayOne[1]) && notExist($arrayOne[1]) ? $arrayOne[1] :
        (isset($arrayThree[1]) && notExist($arrayThree[1]) ? $arrayThree[1] :
        (isset($arrayFour[2]) && notExist($arrayFour[2]) ? $arrayFour[2] :
        'no KEY3 available!'))));

array_push($keywordArray, $Key3);


$Key4 = (isset($arrayOne[1]) && notExist($arrayOne[1]) ? $arrayOne[1] :
        (isset($arrayTwo[1]) && notExist($arrayTwo[1]) ? $arrayTwo[1] :
        (isset($arrayThree[1]) && notExist($arrayThree[1]) ? $arrayThree[1] :
        (isset($arrayFour[3]) && notExist($arrayFour[3]) ? $arrayFour[3] :
        'no KEY4 available!'))));

array_push($keywordArray, $Key4);


echo "<P>" .$Key1;
echo "<P>" .$Key2;
echo "<P>" .$Key3;
echo "<P>" .$Key4;


.

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
If I understand the objective correctly, array_count_values() is your friend here.

http://www.laprbass.com/RAY_temp_ggjones.php

Outputs:
THESE WORDS HAPPEN ONCE: Array ( [0] => word4 [1] => word6 [2] => word7 [3] => word10 [4] => word13 [5] => word15 [6] => word16 )
THESE WORDS HAPPEN TWICE: Array ( [0] => word8 [1] => word9 [2] => word14 )
THESE WORDS HAPPEN THRICE: Array ( [0] => word1 [1] => word2 [2] => word3 [3] => word5 [4] => word11 )
<?php // RAY_temp_ggjones.php
// echo "<pre>";

// THE ASSIGNMENT
// The key issue is to arrive at a list of 4 words that are not duplicated.

// TEST DATA FROM THE POST AT EE
$arrayOne = array ('word1','word2','word3','word4','word5','word6','word7');
$arrayTwo = array ('word8','word2','word3','word9','word5','word10','word11');
$arrayThree = array ('word8','word1','word14','word13','word5','word14','word11');
$arrayFour = array ('word1','word2','word3','word9','word15','word16','word11');

// MAKE AN ARRAY OF ARRAYS, TO ALLOW ANY NUMBER OF ARRAYS INTO THE FUNCTION
$arr = array
( $arrayOne
, $arrayTwo
, $arrayThree
, $arrayFour
)
;



// RUN THE FUNCTION AND SHOW THE RESULTS
echo "<br/>THESE WORDS HAPPEN ONCE: ";
print_r(find_words($arr, $num=1));
echo "<br/>THESE WORDS HAPPEN TWICE: ";
print_r(find_words($arr, $num=2));
echo "<br/>THESE WORDS HAPPEN THRICE: ";
print_r(find_words($arr, $num=3));



// A FUNCTION TO FIND THE SINGLETONS, DOUBLETONS, ETC.
function find_words($arr, $num=1)
{
    // COMBINE THE INPUT ARRAYS http://php.net/array_merge
    $new = array();
    foreach ($arr as $sub_array)
    {
        $new = array_merge($new, $sub_array);
    }

    // COUNT THE VALUES http://php.net/array-count-values
    $cnt = array_count_values($new);
    foreach ($cnt as $word => $count)
    {
        // IDENTIFY SINGLETONS, DOUBLETONS, ETC.
        if ($count == $num) $out[] = $word;
    }

    // RETURN THE WORK PRODUCT
    return $out;
}

Open in new window

Author

Commented:
... apologies for my tardiness Ray; I've been on the road for few days.

Thanks for the code ... however, it's not clear to me how this moves me forward.

You will notice that each element of $keywordArray in my example is populated through a if-not-exist test. If  the word DOES exist, the next string to test  does not necessarily follow a linear sequence.

Now, my example appears to work fine. The syntax seems so awkward though, that I cant help feeling that there must be a more efficient way.
regards,

GJ

Most Valuable Expert 2011
Top Expert 2016

Commented:
Yeah, I've been away for a while, too.

I must be missing something.  I copied the code posted at ID:36315823, where it said, ... key issue is to arrive at a list of 4 words that are not duplicated.

The code is in this snippet and installed it on my server at this URL.
http://www.laprbass.com/RAY_temp_ggjones.php

The output is:
word1
word8
word2
word9

If "not duplicated" means "not duplicated within any individual array" then word3 should appear since it is not duplicated in $arrayOne.
If "not duplicated" means "not duplicated within all of the arrays" then word8 should NOT appear since it is duplicated in $arrayTwo and $arrayThree.

Maybe the easiest way to move this forward would be to step back from the technical details and just describe in plain language what rules you want to apply for singletons or duplicates.

Best regards, ~Ray
<?php // RAY_temp_ggjones.php
error_reporting(E_ALL);

$arrayOne = array ('word1','word2','word3','word4','word5','word6','word7');
$arrayTwo = array ('word8','word2','word3','word9','word5','word10','word11');
$arrayThree = array ('word8','word1','word14','word13','word5','word14','word11');
$arrayFour = array ('word1','word2','word3','word9','word15','word16','word11');

global $keywordArray;

function notExist($keyWord) {

global $keywordArray;

       if (sizeof($keywordArray) == 0 || !in_array(trim($keyWord), $keywordArray)){ return true;} else {return false; }

    }

$Key1 = (isset($arrayOne[0]) ? $arrayOne[0] :
        (isset($arrayTwo[0]) ? $arrayTwo[0] :
        (isset($arrayThree[0]) ? $arrayThree[0] :
        (isset($arrayFour[0]) && notExist($arrayFour[0]) ? $arrayFour[0] :
        'no KEY1 available!'))));

$keywordArray = array(); array_push($keywordArray, $Key1);


$Key2 = (isset($arrayTwo[0]) && notExist($arrayTwo[0]) ? $arrayTwo[0] :
        (isset($arrayThree[0]) && notExist($arrayThree[0]) ? $arrayThree[0] :
        (isset($arrayOne[1]) && notExist($arrayOne[1]) ? $arrayOne[1] :
        (isset($arrayFour[1]) && notExist($arrayFour[1]) ? $arrayFour[1] :
        'no KEY2 available!'))));array_push($keywordArray, $Key2);

array_push($keywordArray, $Key2);


$Key3 = (isset($arrayThree[0]) && notExist($arrayThree[0]) ? $arrayThree[0] :
        (isset($arrayOne[1]) && notExist($arrayOne[1]) ? $arrayOne[1] :
        (isset($arrayThree[1]) && notExist($arrayThree[1]) ? $arrayThree[1] :
        (isset($arrayFour[2]) && notExist($arrayFour[2]) ? $arrayFour[2] :
        'no KEY3 available!'))));

array_push($keywordArray, $Key3);


$Key4 = (isset($arrayOne[1]) && notExist($arrayOne[1]) ? $arrayOne[1] :
        (isset($arrayTwo[1]) && notExist($arrayTwo[1]) ? $arrayTwo[1] :
        (isset($arrayThree[1]) && notExist($arrayThree[1]) ? $arrayThree[1] :
        (isset($arrayFour[3]) && notExist($arrayFour[3]) ? $arrayFour[3] :
        'no KEY4 available!'))));

array_push($keywordArray, $Key4);


echo "<P>" .$Key1;
echo "<P>" .$Key2;
echo "<P>" .$Key3;
echo "<P>" .$Key4;

Open in new window

Author

Commented:

Heh, heh… "plain language". Thanks Ray.

1) I'm trying to create a meta list of unique phrases

2) these phrases each come from a series of other lists of phrases, most of which contain duplicates within each list, and across the other lists.

3) the rules for deciding what to do, when discovering a duplicate, do not follow a constant pattern, rather they change according to a modifiable logic for each index of the meta list.

In English:

The meta list of unique phases is composed of 4 phrase:

Phrase one = $arrayOne[0], if it exists, otherwise $arrayTwo[0], if it exists, otherwise $arraythree[0], if it exists, otherwise $arrayFour[0], if it exists.

Open in new window


Phrase two = $arrayTwo[0], if it exists, and it doesn't already exist in the Meta List, otherwise $arrayThree[0], if it exists, and it doesn't already exist in the Meta List, otherwise $arrayOne[1], if it exists, and it doesn't already exist in the Meta List, otherwise $arrayThree[1], if it exists, and it doesn't already exist in the Meta List, otherwise $arrayFour[2], if it exists, and it doesn't already exist in the Meta List, otherwise NULL

Open in new window


Phrase three = etc

Open in new window


I hope this clarifies.

regards,

GJ
Most Valuable Expert 2011
Top Expert 2016

Commented:
Given these patterns, I am unable to figure out the rules that give meaning for "etc" in "Phrase three = etc."  The Phrase one part of things seems to make sense,  but how would one know that Phrase two should look at $arrayOne[1] then at $arrayThree[1], skipping over $arrayTwo[1]?  As you said, the next string to test  does not necessarily follow a linear sequence and that is fine, so long as it follows some kind of predictable or discernible sequence!

Phrase one = $arrayOne[0],   if it exists,
   otherwise $arrayTwo[0],   if it exists,
   otherwise $arraythree[0], if it exists,
   otherwise $arrayFour[0],  if it exists.
   
Phrase two = $arrayTwo[0],   if it exists, and it doesn't already exist in the Meta List,
   otherwise $arrayThree[0], if it exists, and it doesn't already exist in the Meta List,
   otherwise $arrayOne[1],   if it exists, and it doesn't already exist in the Meta List,
   otherwise $arrayThree[1], if it exists, and it doesn't already exist in the Meta List,
   otherwise $arrayFour[2],  if it exists, and it doesn't already exist in the Meta List,
   otherwise NULL  

Am I overlooking a mathematical or set theory construct at work here?  Or some kind of named sequence, or does this emulate something else in the real world?  I'd really like to help, but I am having a hard time discerning a repeating pattern that can be implemented in any well-understood computer science design.

Author

Commented:

Thanks for the reply Ray.

No, this is not a matter of you failing to grasp complex set theory.

Each word choice within the meta list is based on unique, modifiable logic. If anything, the criteria are qualitative rather than quantitative, and I apologize for not making my 'does not necessarily follow a linear sequence" statement more clear.

It may be that, as a consequence, the logic is as efficient (or inefficient) as it can be, given that some kind of logic loop to discover each meta list phrase is unavailable.

Outside of that, the other methods I'm using appear to now get the job done - testing every candidate phrase for uniqueness prior to pushing it onto the meta phrase array on-the-fly.

So I won't belabor the point any longer.

Once again Ray, thanks for the input, and I apologize for not being more clear and succinct.

Regards,

GJ
Most Valuable Expert 2011
Top Expert 2016

Commented:
OK, thanks for the points and good luck with it.  Best, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial