Link to home
Start Free TrialLog in
Avatar of ggjones
ggjonesFlag for Afghanistan

asked on

How to find a value in an array redux




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

Avatar of Sandeep Kothari
Sandeep Kothari
Flag of India image

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

Open in new window

Avatar of ggjones

ASKER

...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
SOLUTION
Avatar of Sandeep Kothari
Sandeep Kothari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ggjones

ASKER

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

regards,

GJ
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 !
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

Avatar of ggjones

ASKER


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

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ggjones

ASKER


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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ggjones

ASKER

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

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

Avatar of ggjones

ASKER


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
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.
Avatar of ggjones

ASKER


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
OK, thanks for the points and good luck with it.  Best, ~Ray