如何找到关联数组的父数组索引?

时间:2014-04-23 06:10:45

标签: php arrays

我有像

这样的输出
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` 嘿,我得到正确的输出,但它没有打破它来了多次。请帮助我。 我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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

答案 2 :(得分: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;