Jump to content

Recommended Posts

Hi, I'm trying to use in_array to match elements in 2 arrays, but I can't get it to work.

 

To simplify, I have 2 arrays, each contain a list (in fact the lists have about 250 items)

 

$complete - good order              $partial - bad order

Horse                                      Cat

Dog                                          Elephant

Cat                                          Dog

Elephant

 

I want to print the complete list, in the good order, but if the item is also in the partial list, to append a x (in the real example I'll actually be making html links with preg_replace instead of appending x)

 

So, final product:

 

Horse

Dog x

Cat x

Elephant x

 

I have this:

$y= count($complete) -1;
for ($u=1; $u < $y; $u++) {					
if  (in_array ($complete[$u], $partial))	{
print "<li>".$complete[$u].' x</li>'; 
} else {
print "<li>".$complete[$u]."</li>"; 
}

But it seems to print the complete list with no changes - i.e. it's finding no matches.

 

Thanks for any help - if there's a better method than in_array(), I'd be happy to use it.

Link to comment
https://forums.phpfreaks.com/topic/241314-help-with-in_array-beginner-level/
Share on other sites

If your just trying to find matches. You can use array_diff()

http://php.net/manual/en/function.array-diff.php

 

You take 2 arrays (or more I think if you want)

 

$array1 = array(1,2,3,4,5,6,7,8,9,a,b,c);
$array2 = array(z,y,x,9,8,7,6,5,4,3,2,1);
$difference = array_diff($array1, $array2);

sort($difference);

echo "<pre>";
print_r($difference);
echo "</pre>";

 

and what $difference should output is..

 

Array(
  [] = a,
  [] = b,
  [] = c,
  [] = x,
  [] = y,
  [] = z,
) 

 

which you could then know by this array that those aren't matches between the 2. You can definitively expand on this a lot more to then turn around and rebuild a final array where its either just the matches, or the difference + the matches but removing the duplicates.. but this here is like the stepping stone Idea from what I am understanding your trying to do.

monkeytooth beat me to it but still just though I would say the little that I knew.

 

<?php
$complete = array('Horse','Snake','Cat','Dog','Elephant');
$partial = array('Cat','Elephant','Dog');
$matched_items = array_intersect($complete, $partial); 
$unmatched_items = array_diff($complete, $partial); 
print_r ($unmatched_items); //prints unmatched items
echo "<br/>";
print_r($matched_items);
?>

Well that's weird, using

$difference = array_diff($complete, $partial);
sort($difference);
echo "<pre>";print_r($difference);echo "</pre>";

I get the full complete list, double spaced

 

and if I switch it round to

$difference = array_diff($partial, $complete);

I get the partial list, single spaced

 

Either way, no matches, and yet the two lists look like they have the same items. Must be formatted different somehow. Will delve deeper…

It's really hard to see where those extra blank lines are coming from!

This is a sample of the first few lines from my complete list:

(
    [0] => Acanthisittidae

    [1] => Acanthizidae

    [2] => Accipitridae

    [3] => Acrocephalidae

    [4] => Aegithalidae

 

and here is a sample from the partial list

 

(
    [0] => Accipitridae
    [1] => Acrocephalidae
    [2] => Aegithalidae
    [3] => Alaudidae
    [4] => Alcedinidae

 

Both as viewed in Firefox 5 -> view source

 

Any ideas what could be creating that extra line, or how I can see what's really going on?

can use this

<?php

$complete = array("Horse", "Dog", "Cat", "Elephant");
$partial = array("Cat", "Elephant", "Dog");
foreach ($complete as $item){
  if(in_array($item, $partial)){
    echo $item." x<br>";
  } else {
    echo $item."<br>";
  }
}
?>

 

Ray

thanks craygo, I'll bear that in mind later, but it seems now that the problem is that there are in fact no matches, because an extra white line has crept in somehow. I'm going to try and strip the whitespace/returns from the $complete first then give your code a go.

Tendolla, thanks I'll try you method shortly. Though I've just tried this:

foreach ($complete as $item){$item= preg_replace('@([\s]*)([\w]+)([\s]*)@s', "$2", $item); }

and many variations on the theme … and I still seem to get that weird double spacing.

It's going to be complicated to know how many letters to trim, and where, because I can't actually see where the line (if there is one) is coming from. I think I'll try exporting $complete to a new file to se what it looks like.

 

AyKay47, thanks, in fact either of those would be OK just to get a result of some sort (currently intersect gives none, and diff gives all).

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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