2011-01-21 212 views
1

我有一個JSON數組,它使用json_decode()來設置數據。通過字符串過濾json數組php通過字符串

我正在尋找匹配數組中的某個項目,如果匹配,我需要返回數組的整個「行」,而不僅僅是一個字段,就像我找到的所有filter_array()片段。

任何人都可以指向正確的方向嗎?

數據看起來像這樣調用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" 
    } 

我需要匹配media_type

回答

3

像這樣的東西?

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 
} 
+0

是的答案,謝謝captn! – 2011-01-24 22:46:43

0

更正:

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

} 
0

正確上述

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