简体   繁体   中英

filter json array php by string

I have a JSON array that uses json_decode() to setup the data.

I'm looking to match a certain item in the array and if it matches i need to return the entire "row" of the array, not just a single field like all of the filter_array() snippets i'm finding.

Can anyone point me in the right direction?

Data looks like this before calling json_decode() :

[
    {
        "format": "default",
        "media_url": "http://cpe.delvenetworks.com/000337/011911_redzonefull.mp3",
        "title": "RZ - Jim Trotter",
        "thumb_url": "",
        "date_posted": "2011-01-19 14:58:45",
        "media_type": "audio"
    },
    {
        "format": "default",
        "media_url": "http://cpe.delvenetworks.com/000337/f10bestof2010offense.mp4",
        "title": "Best of O - 2010",
        "thumb_url": "http://img.delvenetworks.com/bV7.120x66.jpeg",
        "date_posted": "2011-01-18 16:01:45",
        "media_type": "video"
    },
    {
        "format": "default",
        "media_url": "http://cpe.delvenetworks.com/000337\f10bestof2010defense.mp4",
        "title": "Best od D - 2010",
        "thumb_url": "http://img.delvenetworks.com/UqK.120x66.jpeg",
        "date_posted": "2011-01-18 16:01:45",
        "media_type": "video"
    }

I need to match media_type .

Something like this?

function filter($filter, $array){
    $filtered_array = array();
    for($i = 0; i < count(array); i++){
        if($array[i].media_type == $filter)
            $filtered_array[] = array[i]
    }
    return $filtered_array
}

PHP


media_type_array($json,$filter) // function name <br/>
{ 

$filtered_array=array();

foreach($json as $key=>$value)

{

foreach($value as $key1=>$value1)

  {

        if ($value1==$filter)
        {
        $filtered_array[]=$value;
        }
    }

}

return $filtered_array;

}

Corrected:

function filter($filter, $array){
    $filtered_array = $array();     
    for ($i = 0; $i < count($array); $i++){
        if($array[$i].media_type == $filter)
            $filtered_array[] = $array[$i];
    }
    return $filtered_array;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM