Page 1 of 2
array_unique
Posted: Tue Oct 31, 2006 7:18 pm
by pgolovko
Code: Select all
<?php
$array[0] = array('http://www.wmdirectory.com/category-9-0.htm');
$array[1] = array('http://www.wmdirectory.com/category-71-0.htm');
$array[2] = array('http://www.wmdirectory.com/deadlink-1035.htm');
$array[3] = array('http://www.wmdirectory.com/category-39-0.htm');
$array[4] = array('http://www.wmdirectory.com/category-84-0.htm');
$array[5] = array('http://www.wmdirectory.com/category-38-0.htm');
$array[6] = array('http://www.wmdirectory.com/deadlink-353.htm');
$array[7] = array('http://www.wmdirectory.com/info-1035.htm');
echo '<pre>';
print_r(array_unique($array));
echo '</pre>';
?>
The above will produce:
Code: Select all
Array
(
[0] => Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
)
)
What I need is:
Code: Select all
Array
(
[0] => Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
[1] => http://www.wmdirectory.com/category-71-0.htm
)
)
Any idea how?

Posted: Tue Oct 31, 2006 7:30 pm
by feyd
You're running
array_unique() on an array of arrays. That's not what
array_unique() was designed for. It's for one-dimensional arrays.
Posted: Tue Oct 31, 2006 7:53 pm
by pgolovko
Oh, can we simplify it then? Lets say I need the following:
Code: Select all
Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
[1] => http://www.wmdirectory.com/deadlink-1035.htm'
)
if I have:
Code: Select all
$array[0] = 'http://www.wmdirectory.com/category-9-0.htm';
$array[1] = 'http://www.altavista.com/';
$array[2] = 'http://www.wmdirectory.com/deadlink-1035.htm';
$array[3] = 'http://www.wmdirectory.com/category-39-0.htm';
$array[4] = 'http://www.wmdirectory.com/category-84-0.htm';
$array[5] = 'http://www.wmdirectory.com/category-38-0.htm';
$array[6] = 'http://www.wmdirectory.com/deadlink-353.htm';
$array[7] = 'http://www.wmdirectory.com/info-1035.htm';
$array[8] = 'http://www.google.com';
$array[9] = 'http://www.yahoo.com';
The idea stays the same, I need the remove all entries except the first two domain-matching entries. This is kind of hard one. I guess I need to perform the following on each entry to match against the other ones:
Code: Select all
$urlparts = parse_url( ... );
$main_host = $urlparts['host'];
though honestly I've got no idea how.
Posted: Tue Oct 31, 2006 8:06 pm
by John Cartwright
One way would be to loop through your existing array and build a new array with the first two entries of each hostname. Heres some placebo to work with
Code: Select all
foreach ($links as $link) {
//parse url
if (count($newArray[$parsedurl]) < 2)) {
//insert value into new array
}
}
Posted: Tue Oct 31, 2006 8:06 pm
by feyd
I think you may want to look into using
array_filter().
Posted: Wed Nov 01, 2006 5:43 pm
by pgolovko
Code: Select all
<?php
$links[0] = 'http://www.wmdirectory.com/category-9-0.htm';
$links[1] = 'http://www.altavista.com/';
$links[2] = 'http://www.wmdirectory.com/deadlink-1035.htm';
$links[3] = 'http://www.wmdirectory.com/category-39-0.htm';
$links[4] = 'http://www.wmdirectory.com/category-84-0.htm';
$links[5] = 'http://www.wmdirectory.com/category-38-0.htm';
$links[6] = 'http://www.wmdirectory.com/deadlink-353.htm';
$links[7] = 'http://www.wmdirectory.com/info-1035.htm';
$links[8] = 'http://www.google.com';
$links[9] = 'http://www.yahoo.com';
echo '<pre>';
print_r($links);
echo '</pre>';
foreach($links as $link){
print $link."<br>";
$urlparts = parse_url($link);
$main_host = $urlparts['host'];
if (count($newarray[$main_host]) < 2){
$newarray[] = $link;
}
}
echo '<pre>';
print_r($newarray);
echo '</pre>';
?>
It doesnt check the $newarray for the $main_host. The output of the above is:
Code: Select all
Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
[1] => http://www.altavista.com/
[2] => http://www.wmdirectory.com/deadlink-1035.htm
[3] => http://www.wmdirectory.com/category-39-0.htm
[4] => http://www.wmdirectory.com/category-84-0.htm
[5] => http://www.wmdirectory.com/category-38-0.htm
[6] => http://www.wmdirectory.com/deadlink-353.htm
[7] => http://www.wmdirectory.com/info-1035.htm
[8] => http://www.google.com
[9] => http://www.yahoo.com
)
http://www.wmdirectory.com/category-9-0.htm
http://www.altavista.com/
http://www.wmdirectory.com/deadlink-1035.htm
http://www.wmdirectory.com/category-39-0.htm
http://www.wmdirectory.com/category-84-0.htm
http://www.wmdirectory.com/category-38-0.htm
http://www.wmdirectory.com/deadlink-353.htm
http://www.wmdirectory.com/info-1035.htm
http://www.google.com
http://www.yahoo.com
Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
[1] => http://www.altavista.com/
[2] => http://www.wmdirectory.com/deadlink-1035.htm
[3] => http://www.wmdirectory.com/category-39-0.htm
[4] => http://www.wmdirectory.com/category-84-0.htm
[5] => http://www.wmdirectory.com/category-38-0.htm
[6] => http://www.wmdirectory.com/deadlink-353.htm
[7] => http://www.wmdirectory.com/info-1035.htm
[8] => http://www.google.com
[9] => http://www.yahoo.com
)
Posted: Wed Nov 01, 2006 7:00 pm
by feyd
Your code is looking for a named element, but writes to that array numerically.
Posted: Wed Nov 01, 2006 7:22 pm
by pgolovko
What do you mean?
Posted: Wed Nov 01, 2006 7:27 pm
by feyd
$main_host is a string -- a name.
$foo[] = 'bar' generates a numeric entry in the array $foo.
Water vs oil.
Posted: Wed Nov 01, 2006 7:49 pm
by pgolovko
Oh. What would be the proper way to check it then?
Posted: Wed Nov 01, 2006 9:30 pm
by pgolovko
Well, the following kinda works, but I bet there's a better way of doing it:
Code: Select all
<?php
$links[0] = 'http://www.wmdirectory.com/category-9-0.htm';
$links[1] = 'http://www.altavista.com/';
$links[2] = 'http://www.google.com';
$links[3] = 'http://www.wmdirectory.com/deadlink-1035.htm';
$links[4] = 'http://www.wmdirectory.com/category-39-0.htm';
$links[5] = 'http://www.wmdirectory.com/category-84-0.htm';
$links[6] = 'http://www.wmdirectory.com/category-38-0.htm';
$links[7] = 'http://www.wmdirectory.com/deadlink-353.htm';
$links[8] = 'http://www.wmdirectory.com/info-1035.htm';
$links[9] = 'http://www.yahoo.com';
echo '<pre>';
print_r($links);
echo '</pre>';
$urlparts = parse_url($links[0]);
$main_host = $urlparts['host'];
print $main_host."<p>";
foreach($links as $link){
print $link."<br>";
if(strpos($link, $main_host)){
$num++;
if($num <= 2){
$newarray[] = $link;
}
}
}
echo '<pre>';
print_r($newarray);
echo '</pre>';
?>
Output:
Code: Select all
Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
[1] => http://www.altavista.com/
[2] => http://www.google.com
[3] => http://www.wmdirectory.com/deadlink-1035.htm
[4] => http://www.wmdirectory.com/category-39-0.htm
[5] => http://www.wmdirectory.com/category-84-0.htm
[6] => http://www.wmdirectory.com/category-38-0.htm
[7] => http://www.wmdirectory.com/deadlink-353.htm
[8] => http://www.wmdirectory.com/info-1035.htm
[9] => http://www.yahoo.com
)
www.wmdirectory.com
http://www.wmdirectory.com/category-9-0.htm
http://www.altavista.com/
http://www.google.com
http://www.wmdirectory.com/deadlink-1035.htm
http://www.wmdirectory.com/category-39-0.htm
http://www.wmdirectory.com/category-84-0.htm
http://www.wmdirectory.com/category-38-0.htm
http://www.wmdirectory.com/deadlink-353.htm
http://www.wmdirectory.com/info-1035.htm
http://www.yahoo.com
Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
[1] => http://www.wmdirectory.com/deadlink-1035.htm
)
Re: array_unique
Posted: Wed Nov 01, 2006 9:52 pm
by olddog
pgolovko wrote:Code: Select all
<?php
$array[0] = array('http://www.wmdirectory.com/category-9-0.htm');
$array[1] = array('http://www.wmdirectory.com/category-71-0.htm');
$array[2] = array('http://www.wmdirectory.com/deadlink-1035.htm');
$array[3] = array('http://www.wmdirectory.com/category-39-0.htm');
$array[4] = array('http://www.wmdirectory.com/category-84-0.htm');
$array[5] = array('http://www.wmdirectory.com/category-38-0.htm');
$array[6] = array('http://www.wmdirectory.com/deadlink-353.htm');
$array[7] = array('http://www.wmdirectory.com/info-1035.htm');
echo '<pre>';
print_r(array_unique($array));
echo '</pre>';
?>
The above will produce:
Code: Select all
Array
(
[0] => Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
)
)
What I need is:
Code: Select all
Array
(
[0] => Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
[1] => http://www.wmdirectory.com/category-71-0.htm
)
)
Any idea how?

$links[0] = '
http://www.wmdirectory.com/category-9-0.htm';
$links[1] = '
http://www.altavista.com/';
$links[2] = '
http://www.wmdirectory.com/deadlink-1035.htm';
$links[3] = '
http://www.wmdirectory.com/category-39-0.htm';
$links[4] = '
http://www.wmdirectory.com/category-84-0.htm';
$links[5] = '
http://www.wmdirectory.com/category-38-0.htm';
$links[6] = '
http://www.wmdirectory.com/deadlink-353.htm';
$links[7] = '
http://www.wmdirectory.com/info-1035.htm';
$links[8] = '
http://www.google.com';
$links[9] = '
http://www.yahoo.com';
given what you've got, if it was me, I'd do this:
Code: Select all
//-------------------------------------------------------
$suspects = array(); // EMPTY CONTAINER
foreach($links as $nth=>$link)
{
$parts = explode('.',$link); // SEGMENT THE DOT SEPARATED URL
$suspects[$parts[1]] += 1; // GRAB THE HOST SEGMENT, KEEP A RUNNING COUNT OF EACH OCCURANCE
}
$unique_hosts = array_keys($suspects);// GIMME A LIST
//-------------------------------------------------------
This would create:
Code: Select all
$unique_hosts[0] => wmdirectory
$unique_hosts[1] => altavista
$unique_hosts[2] => wmdirectory
$unique_hosts[3] => google
$unique_hosts[4] => yahoo
Re: array_unique
Posted: Wed Nov 01, 2006 10:33 pm
by olddog
pgolovko wrote:Code: Select all
<?php
$array[0] = array('http://www.wmdirectory.com/category-9-0.htm');
$array[1] = array('http://www.wmdirectory.com/category-71-0.htm');
$array[2] = array('http://www.wmdirectory.com/deadlink-1035.htm');
$array[3] = array('http://www.wmdirectory.com/category-39-0.htm');
$array[4] = array('http://www.wmdirectory.com/category-84-0.htm');
$array[5] = array('http://www.wmdirectory.com/category-38-0.htm');
$array[6] = array('http://www.wmdirectory.com/deadlink-353.htm');
$array[7] = array('http://www.wmdirectory.com/info-1035.htm');
echo '<pre>';
print_r(array_unique($array));
echo '</pre>';
?>
The above will produce:
Code: Select all
Array
(
[0] => Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
)
)
What I need is:
Code: Select all
Array
(
[0] => Array
(
[0] => http://www.wmdirectory.com/category-9-0.htm
[1] => http://www.wmdirectory.com/category-71-0.htm
)
)
Any idea how?

or you could do this:
Code: Select all
//-------------------------------------------------------
$suspects = array(); // EMPTY CONTAINER
foreach($links as $nth=>$link)
{
$parts = explode('.',$link); // SEGMENT THE DOT SEPARATED URL
$page = explode('/',$parts[2]);// SEGMENT THE SLASH
$suspects[$page[1]] += 1; // GRAB THE PAGE SEGMENT, KEEP A RUNNING COUNT OF EACH OCCURANCE
}
$unique_pages = array_keys($suspects); // GIMME A LIST
//which would give you this
//-------------------------------------------------------
$unique_pages[0] => category-9-0
$unique_pages[1] =>
$unique_pages[2] => deadlink-1035
$unique_pages[3] => category-39-0
$unique_pages[4] => category-84-0
$unique_pages[5] => category-38-0
$unique_pages[6] => deadlink-353
$unique_pages[7] => info-1035
probably what you want is this:
given:
$array[0] = array('
http://www.wmdirectory.com/category-9-0.htm');
$array[1] = array('
http://www.wmdirectory.com/category-71-0.htm');
$array[2] = array('
http://www.wmdirectory.com/deadlink-1035.htm');
$array[3] = array('
http://www.wmdirectory.com/category-39-0.htm');
$array[4] = array('
http://www.wmdirectory.com/category-84-0.htm');
$array[5] = array('
http://www.wmdirectory.com/category-38-0.htm');
$array[6] = array('
http://www.wmdirectory.com/deadlink-353.htm');
$array[7] = array('
http://www.wmdirectory.com/info-1035.htm');
Code: Select all
//----------------------------------------------------
$suspects = array(); // EMPTY CONTAINER
foreach($array as $nth=>$url)
{
$suspects[$url[0]] += 1;
}
$unique_urls = array_keys($suspects); // GIMME A LIST
//which would give you a list of the unique URLs
$unique_urls[0] => http://www.wmdirectory.com/category-9-0.htm
$unique_urls[1] => http://www.wmdirectory.com/category-71-0.htm
$unique_urls[2] => http://www.wmdirectory.com/deadlink-1035.htm
$unique_urls[3] => http://www.wmdirectory.com/category-39-0.htm
$unique_urls[4] => http://www.wmdirectory.com/category-84-0.htm
$unique_urls[5] => http://www.wmdirectory.com/category-38-0.htm
$unique_urls[6] => http://www.wmdirectory.com/deadlink-353.htm
$unique_urls[7] => http://www.wmdirectory.com/info-1035.htm
Posted: Thu Nov 02, 2006 4:19 am
by pgolovko
Thanks, I needed to get the first pair of domain-matching URLs from the array of completely different URLs. Here's what I finally got myself:
Code: Select all
<?php
$links[0] = 'http://www.wmdirectory.com/category-9-0.htm';
$links[1] = 'http://www.altavista.com/';
$links[2] = 'http://www.google.com';
$links[3] = 'http://www.wmdirectory.com/deadlink-1035.htm';
$links[4] = 'http://www.wmdirectory.com/category-39-0.htm';
$links[5] = 'http://www.wmdirectory.com/category-84-0.htm';
$links[6] = 'http://www.wmdirectory.com/category-38-0.htm';
$links[7] = 'http://www.wmdirectory.com/deadlink-353.htm';
$links[8] = 'http://www.wmdirectory.com/info-1035.htm';
$links[9] = 'http://www.yahoo.com';
echo '<pre>';
print_r($links);
echo '</pre>';
$urlparts = parse_url($links[0]);
$main_host = $urlparts['host'];
foreach($links as $link){
if(strpos($link, $main_host)){
$num++;
if($num <= 2){
$newarray[] = $link;
}
}
}
echo '<pre>';
print_r($newarray);
echo '</pre>';
?>
Now that I have a working code, I just wanted to verify that I have a grammatically correct PHP code. I will use it to sort one huge database, and not to overload system resources.
Posted: Fri Nov 03, 2006 1:58 am
by pgolovko
Hmmm, I'm a bit confused. How would I apply the above code into "while" loop here?
Code: Select all
if (mysql_num_rows($result) <> ''){
while ($row = mysql_fetch_array($result)) {
if(strlen($row['title']) > 100){ $row['title'] = substr($row['title'], 0, 100)."..."; }
if(strlen($row['description']) > 500){ $row['description'] = substr($row['description'], 0, 500)."..."; }
$results[] = $row;
}
}