1

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.

3 Answers 3

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

0

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.