Jump to content

Find Position in Array and Match to Other Array


casbboy

Recommended Posts

Here is what I have:

First, I set up two arrays to be used later (from a mysql query):

[code=php:0]
while($alinks = mysql_fetch_array($listlinks)) {
$clinks[] = $alinks['link'];
$cid[] = $alinks['article_id'];
}
[/code]

Now I check to see if a new set of links matches any in the $clinks array:

[code=php:0]
$nc = 0;
$pos = 0;
foreach($info[items] as $in) {
if(in_array($info[items][$nc][link], $clinks))  {
$pos = strpos($info[items][$nc][link], $clinks);
$abar = '<a href="/article/'. $cid[$pos] .'"><img src="/images/art/indexed.gif" border="0"></a>';
} else {
$abar = '<img src="/images/art/notindexed.gif">';
}
$nc = $nc + 1;
}
[/code]

If the link already exists, I want the 'article_id' of the matched link to be placed in to the href.  Hence I have the $cid array with the $pos variable giving the position with $cid[$pos]. 

Unfortunately, this isn't working as I had hoped, and only gives the same number for all the links that match.

ANy suggestions?

Thanks
Ryan
As far as I'm aware, strpos() doesn't accept an array as an argument (and even if it did, you have them around the wrong way, the arguments are positioned strpos(haystack,needle)).

[code]<?php
//Try replacing
$pos = strpos($info[items][$nc][link], $clinks);

//With this
$pos = array_search($info['items'][$nc]['link'],$clinks);


//Just a tip
$nc = $nc + 1;
//Can be written like this
$nc++;
?>[/code]

Archived

This topic is now archived and is closed to further replies.

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