1
function getposts($search){
    $url = "http://search.twitter.com/search.json?q=".$search."&include_entities=true&result_type=mixed";
    $value = file_get_contents($url);
    $array = json_decode($value, true);

    foreach($array['results'] as $id){
        $uniquearray[] = array_unique($id);
    }

    for($i=0;$i<count($uniquearray);$i++){
        $output .= $uniquearray[$i]['from_user_id'];
        $output .= "<br />";
    }
    return $output;
}

Thats what I tried but it doesnt seem to work it just gets the last post now and I think thats because it filters all the values from every user and since some of them are from a different user but with another key the same value it removes them. I only want to avoid duplicate from_user_id key values.

Can someone help me out with this? I am using a twitter api. And using this url:

http://search.twitter.com/search.json?q=apitesttweet&include_entities=true&result_type=mixed

Sinan Samet
  • 6,432
  • 12
  • 50
  • 93

2 Answers2

1

I've made a similar answer here

You can adapt this one to your issue.

function getposts($search){
    $url = "http://search.twitter.com/search.json?q=".$search."&include_entities=true&result_type=mixed";
    $value = file_get_contents($url);
    $array = json_decode($value, true);

    return $array['results'];
}

function arrayUniqueFromKey(array $arr,$key)
{
    $titles = array();$ret = array();
    foreach ($arr as $v) {
        if (!in_array($v[$key],$titles)) {
            $titles[] = $v[$key];
            $ret[] = $v;
        }
    }
    return $ret;
}
$posts = getposts("apitesttweet");
echo count($posts)." Total posts".PHP_EOL; // 3 Total posts

$uniqs = arrayUniqueFromKey($posts, "from_user_id");
echo count($uniqs). " Unique posts".PHP_EOL; // 2 Unique posts

print_r($uniqs);
Community
  • 1
  • 1
Touki
  • 7,465
  • 3
  • 41
  • 63
0

It actually does work. My mistake was that I was trying to filter it in the loop itself. When I took array_unique out of the filter it worked.

<?php 
function getids($search){
    $url = "http://search.twitter.com/search.json?q=".$search."&include_entities=true&result_type=mixed";
    $value = file_get_contents($url);
    $array = json_decode($value, true);

    $uniquearray = array();
    for($i=0;$i<count($array['results']);$i++){
            $uniquearray[] = $array['results'][$i]['from_user_id'];
    }

    $uniquearray = array_unique($uniquearray);
    return $uniquearray;
}
?>

I made it shorter and cleaner and made it return just an array.

Sinan Samet
  • 6,432
  • 12
  • 50
  • 93