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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.