Question Locked

Whats wrong with this PHP Array

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

if ($record != "" & $record2 != "")
{
for ($iArray = 0; $iArray < count($record); $iArray++)
{
for ($iArray2 = 0; $iArray2 < count($record2); $iArray++)
{ if ($record[$iArray] = $record2[$iArray2]) { $record3[$iArray] = $record2[$iArray2];
}
}
}
return $record3;
}

I'm trying to make a Array for a search by comparing if array 2 is equal to array 1 then keep it else, discard it.

5 replies

by absolutely

It looks like you're treating $record and $record2 inconsistently, sometimes as arrays, sometimes not. Even if that's supported overloading, it's ugly. Also, for comparisons, you might need == instead of = but again, I don't do PHP, so that might be wrong too.

by larson

I fixed it, was giving me a headache for a bit so I walked to 7-eleven grabbed a energy drink came back deleted it and made this.

if ($record != "" & $record2 != "")
{
for ($iArray = 0; $iArray < count($record); $iArray++)
{
for ($iArray2 = 0; $iArray2 < count($record2); $iArray2++)
{
if ($record[$iArray] == $record2[$iArray2])
{
$record3[$icount] = $record2[$iArray2];
$icount = $icount + 1;
}
}
}
return $record3;
}

by blckspder

you have some syntax errors, you should use && in your first if statement and you should use == in your second if statement.

But you could use something like this to compare 2 arrays:
$result = array_diff($record, $record2);

If there is a difference in the array the different values will be put into the $result array, if there are no differences then nothing will be put in the result array so you could then add what ever you need to with the rest. you can find more information on this function here:
http://us.php.net/array-diff

hope this helps.

This discussion is closed to new replies.