使用PHP和CakePHP Set Class搜索和替换数组键

时间:2010-01-24 14:14:02

标签: php class cakephp multidimensional-array set

我需要编写搜索功能以下列方式替换多维数组中的键:

输入数组:

 [0] => Array
    (
        [Foo] => sometext
        [Foo2] => Array
            (
                [lorem] => 1
                [Avatar2] => Array
            (
                [meta_key] => avatar2
                [meta_value] => Array
                    (
                        [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg
                        [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg
                        [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg
                        [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg
                    )

            )
            )
        [Avatar] => Array
            (
                [meta_key] => avatar
                [meta_value] => Array
                    (
                        [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg
                        [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg
                        [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg
                        [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg
                    )

            )
    )

[1] => Array
    (

        [Wallpaper] => Array
            (
                [meta_key] => wallpaper
                [meta_value] => Array
                    (
                        [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg
                        [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg
                        [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg
                        [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg
                    )

            )
    )   

输出数组应如下所示:

      [0] => Array
    (
        [Foo] => sometext
        [Foo2] => Array
            (
                [lorem] => 1
                [Avatar2] => Array (
                        [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg
                        [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg
                        [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg
                        [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg
                    )

            )
        [Avatar] => Array
            (
                [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg
                [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg
                [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg
                [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg
            )
    )

[1] => Array
    (

        [Wallpaper] => Array
            (

                [small] => 65606449bb5b6d10c2dfce9ecb88fdbd-24-small.jpeg
                [medium] => 65606449bb5b6d10c2dfce9ecb88fdbd-45-medium.jpeg
                [large] => 65606449bb5b6d10c2dfce9ecb88fdbd-75-large.jpeg
                [source] => 65606449bb5b6d10c2dfce9ecb88fdbd.jpg


            )
    )

如您所见,我需要重新格式化meta_key / meta_value部分。欢迎任何建议(原始PHP或使用CakePHP Set Class):)

1 个答案:

答案 0 :(得分:1)

你肯定需要使用递归:

function replace_inner_meta_keys($arr) {
    if (array_key_exists('meta_value', $arr)) {
        // return meta value instead of original array
        return $arr['meta_value'];
    }
    else {
        foreach ($arr as $key => $value) {
            if (is_array($value)) {
                // travel through array recursivly
                $arr[$key] = replace_inner_meta_keys($value);
            }
        }
        return $arr;
    }
}