2014-04-23 134 views
0

我有這樣如何找到關聯數組的父數組索引?

Array 
(
    [2014-04-29] => Array 
     (
      [2] => Array 
       (
        [cal_icon] => remindar_images.jpg 
        [category_slug] => reminder 
       ) 

     ) 

    [2014-04-22] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

     ) 

    [2014-04-21] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

      [4] => Array 
       (
        [cal_icon] => travel_images.jpg 
        [category_slug] => travel 
       ) 

      [11] => Array 
       (
        [cal_icon] => invite.jpg 
        [category_slug] => invite 
       ) 

     ) 

    [2014-04-19] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

     ) 

    [2014-04-23] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

      [4] => Array 
       (
        [cal_icon] => travel_images.jpg 
        [category_slug] => travel 
       ) 

      [7] => Array 
       (
        [cal_icon] => restaurants_images.jpg 
        [category_slug] => restaurants 
       ) 

     ) 

    [2014-04-10] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

     ) 

    [2014-04-20] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

     ) 

    [2014-04-15] => Array 
     (
      [5] => Array 
       (
        [cal_icon] => photo_images.jpg 
        [category_slug] => photos 
       ) 

     ) 

    [2014-04-16] => Array 
     (
      [5] => Array 
       (
        [cal_icon] => photo_images.jpg 
        [category_slug] => photos 
       ) 

     ) 

    [2014-05-01] => Array 
     (
      [6] => Array 
       (
        [cal_icon] => event_images.jpg 
        [category_slug] => events 
       ) 

     ) 

) 
Array 
(
    [2014-04-29] => Array 
     (
      [2] => Array 
       (
        [cal_icon] => remindar_images.jpg 
        [category_slug] => reminder 
       ) 

     ) 

    [2014-04-22] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

     ) 

    [2014-04-21] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

      [4] => Array 
       (
        [cal_icon] => travel_images.jpg 
        [category_slug] => travel 
       ) 

      [11] => Array 
       (
        [cal_icon] => invite.jpg 
        [category_slug] => invite 
       ) 

     ) 

    [2014-04-19] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

     ) 

    [2014-04-23] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

      [4] => Array 
       (
        [cal_icon] => travel_images.jpg 
        [category_slug] => travel 
       ) 

      [7] => Array 
       (
        [cal_icon] => restaurants_images.jpg 
        [category_slug] => restaurants 
       ) 

     ) 

    [2014-04-10] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

     ) 

    [2014-04-20] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

     ) 

    [2014-04-15] => Array 
     (
      [5] => Array 
       (
        [cal_icon] => photo_images.jpg 
        [category_slug] => photos 
       ) 

     ) 

    [2014-04-16] => Array 
     (
      [5] => Array 
       (
        [cal_icon] => photo_images.jpg 
        [category_slug] => photos 
       ) 

     ) 



) 
Array 
(
    [2014-04-29] => Array 
     (
      [2] => Array 
       (
        [cal_icon] => remindar_images.jpg 
        [category_slug] => reminder 
       ) 

     ) 

    [2014-04-22] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

     ) 

    [2014-04-21] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

      [4] => Array 
       (
        [cal_icon] => travel_images.jpg 
        [category_slug] => travel 
       ) 

      [11] => Array 
       (
        [cal_icon] => invite.jpg 
        [category_slug] => invite 
       ) 

     ) 

    [2014-04-19] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

     ) 

    [2014-04-23] => Array 
     (
      [3] => Array 
       (
        [cal_icon] => shopping_images.jpg 
        [category_slug] => shopping 
       ) 

      [4] => Array 
       (
        [cal_icon] => travel_images.jpg 
        [category_slug] => travel 
       ) 

      [7] => Array 
       (
        [cal_icon] => restaurants_images.jpg 
        [category_slug] => restaurants 
       ) 

     ) 

輸出如果我有[category_slug] => travel那麼我想all the date index that belongs to [category_slug] => travel` 嘿我得到正確的輸出,但它不破它是未來多time.Please幫我。 我該如何做到這一點?

回答

1
foreach ($array as $date => $entries) { 
    foreach ($entries as $entry) { 
     if ($entry['category_slug'] == 'shopping') { 
      echo $date; // or return, or whatever 
      break 2; 
     } 
    } 
} 
0

使用簡單foreach

$keyword = 'shopping'; 
foreach ($arr1 as $k=>$arr) 
{ 
    foreach($arr as $k2=>$arr2) 
    { 
     if($arr2['category_slug']==$keyword) 
     { 
      echo $k; 
      //break; //<--- Uncommented since you need all the entries 
     } 
    } 
} 

Working Demo

+0

嘿我得到正確的輸出,但它不破它是未來多時間。如何打破它。價值不是固定時間。它可以變化。 – Muk

+0

使用代碼中顯示的'break'。你看過演示了嗎? –

+0

是的,我已檢查演示。我想所有的日期,在循環不僅一個,我得到所有日期,但多次。我也使用break.please幫助我 – Muk

0

試試這個

// $ARR_INPUT is your array 

$keyword_search = "shopping"; 
$key_to_search = "category_slug"; 

$flag = 0; 
$output = ""; 
foreach($ARR_INPUT as $key1=>$arr1) 
{ 
    foreach($arr1 as $key2=>$arr2) 
    { 
     if($arr2[$key_to_search] == $keyword_search) 
     { 
      $output = $key1; 
      $flag = 1; 
      break; 
     } 
    } 
    if($flag==1) 
    { 
     break; 
    } 
} 

echo "Output : ".$key1;