从JSON字符串中获取所有值并删除所有重复项

时间:2015-01-19 19:59:54

标签: php json

如何从this JSON字符串中获取标记的所有值并删除所有重复项? 我的代码:

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://api.tumblr.com/v2/blog/instagram.tumblr.com/posts/text?api_key=myAPIKey',
    CURLOPT_USERAGENT => 'cURL Request'
));
$resp = curl_exec($curl);
$response = json_decode($resp,true);

(API密钥是Tumblr提供的公共测试密钥)

3 个答案:

答案 0 :(得分:3)

JSON解码array

的黑暗中刺伤
function uniqueTags($jsonArray)
{
    $output = array();
    foreach($jsonArray['response']['posts'] as $post)
    {
        foreach($post['tags'] as $tag)
        {
            //Iterate over tags in all posts returned
            if(!in_array($tag, $output))
            {
                $output[] = $tag;
            }
        }
    }
    return $output;
}

答案 1 :(得分:0)

这是你在找什么?

foreach($response['response']['posts'] as $posts ){
    if(isset($posts['tags'])){
        foreach($posts['tags'] as $tag){
            echo $tag . "<br />";
        }
    }
}

答案 2 :(得分:0)

这是一个有趣的:

$tags = array_unique(
            call_user_func_array('array_merge', 
                array_column($response['response']['posts'], 'tags')));