MyBB Community Forums

Full Version: PHP Array Intersect Problem.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Here is my code:


$array_intersect_1 = array_intersect($array1, $array2);
$array_intersect_2 = array_intersect($array3, $array4);

if (!empty($array_intersect_1)) {
	echo $error_x = 'You have a duplicate entry for X';
}

if (!empty($array_intersect_2)) {
	echo $error_y = 'You have a duplicate entry for Y';
}

if (!empty($array_intersect_1) && !empty($array_intersect_2)) {
	echo $error_x_y = 'You have duplicate entries for X and Y';
}

What the code is doing:

If there are matching array values in $array_intersect_1, echo the text.
If there are matching array values in $array_intersect_2, echo the text.
If there are matching array values in $array_intersect_1 & $array_intersect_2, echo the text.


Here is my problem:

The last if statement doesn't work, because the first 2 if statements already run. The functionality I need, is if both of the array intersects (1 & 2) find a matching value in the arrays respectively, to echo out $error_x_y.

How do I make the last if statement to run, and echo out the last error?
It would be preferable if you share the code as you originally coded it so that we could better understand what the issue is.

Currently if I paste the code and test it directly I get the following warning:
Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\Users\Dell\Dropbox\htdocs\m\global.php on line 18

Which was expected.
The following works for me:
$array_intersect_1 = array_intersect($array1, $array2);
$array_intersect_2 = array_intersect($array3, $array4);

$error_x = $error_y = $error_x_y = '';
if (!empty($array_intersect_1)) {
    $error_x = 'You have a duplicate entry for X';
}

if (!empty($array_intersect_2)) {
    $error_y = 'You have a duplicate entry for Y';
}

if (!empty($array_intersect_1) && !empty($array_intersect_2)) {
    $error_x_y = 'You have duplicate entries for X and Y';
}

if ($error_x_y)
{
	echo $error_x_y;
}
else
{
	echo $error_x;
	echo $error_y;
}
(2015-02-15, 06:09 AM)Omar G. Wrote: [ -> ]It would be preferable if you share the code as you originally coded it so that we could better understand what the issue is.

Currently if I paste the code and test it directly I get the following warning:


Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\Users\Dell\Dropbox\htdocs\m\global.php on line 18

Which was expected.

I put the echo in the wrong place.

The code seems to work, but in the small program I am writing, it just doesn't seem to work.

EDIT: Ok. I think I know what the problem is. I need to write the below code as one statement, instead of 3 if statement:


$array1 = array('horse','dog');
$array2 = array('dog','cat');

$array3 = array('fish','frog');
$array4 = array('fish','rabbit');

$array_intersect_1 = array_intersect($array1, $array2);
$array_intersect_2 = array_intersect($array3, $array4);

if (!empty($array_intersect_1)) {
	echo 'You have a duplicate entry for X';
}

if (!empty($array_intersect_2)) {
	echo 'You have a duplicate entry for Y';
}

if (!empty($array_intersect_1) && !empty($array_intersect_2)) {
	echo 'You have duplicate enetries for X and Y';
}

Is it possible to somehow join these 3 separate if statements, into one?
(2015-02-15, 07:12 AM)Greg Winston Wrote: [ -> ]Is it possible to somehow join these 3 separate if statements, into one?

If I understand correctly what you are asking for:
if (!empty($array_intersect_1) && !empty($array_intersect_2)) {
    echo 'You have duplicate enetries for X and Y';
} 

else if (!empty($array_intersect_1)) {
    echo 'You have a duplicate entry for X';
}

else if (!empty($array_intersect_2)) {
    echo 'You have a duplicate entry for Y';
}
Not exactly, I still need 3 outputs.

"You have a duplicate entry for X You have a duplicate entry for Y You have duplicate enetries for X and Y".

With the else if statements, it just outputs:

"You have a duplicate entry for X".
Sorry but then I don't understand the issue, your code should work unless I'm missing something plain easy.

Your code already outputs "You have a duplicate entry for XYou have a duplicate entry for YYou have duplicate enetries for X and Y" for me.

What you want it to print in the page exactly?
But they are all separate if statements. I need them to be be merged into a single block of code.

The code below only outputs the first if statement:

if (!empty($array_intersect_1) && !empty($array_intersect_2)) {
    echo 'You have duplicate enetries for X and Y';
} 

else if (!empty($array_intersect_1)) {
    echo 'You have a duplicate entry for X';
}

else if (!empty($array_intersect_2)) {
    echo 'You have a duplicate entry for Y';
} 

This only outputs 1 result:
"You have duplicate enetries for X and Y"

But it also needs to output the other 2 conditions, if they are met also.

I need it to output:

"You have a duplicate entry for X You have a duplicate entry for Y You have duplicate enetries for X and Y".

Maybe an if/elseif statement is not the best option of doing it, because it will only output 1 result.
That is what I don't understand, you core in this post works, why do you want to merge it into one if statement and why you think it is possible?

Are you trying to replicate MyBB functionality? If so can you tell me which one is that so I can understand better?
(2015-02-15, 09:03 AM)Omar G. Wrote: [ -> ]That is what I don't understand, you core in this post works, why do you want to merge it into one if statement and why you think it is possible?

Are you trying to replicate MyBB functionality? If so can you tell me which one is that so I can understand better?

I'm not trying to replicate MyBB functionality.

I need it into one block of code, because if they are all separate if statements, then:

1. PHP code executes going down the page, line by line.
2. If the first if statement is true, the other 2 if statements won't get executed. This is because after each echo, I made the page redirect.
3. What if 2 if statements are both true? There can't be 2 page redirects, or 2 MySQL queries.

Maybe PHP loops can help, instead of if statements.
Pages: 1 2