0

我设法动态创建我的数组($$myGenre包含每个name => id),但$myGenre不包含任何东西......我怎样才能使它工作:$myGenre应该包含每个$$myGenre$$myGenre应该有它的内容name => id,并且我们应该让每个彼此$myGenre分开(这里,因为foreach,我们覆盖 $myGenre了每种不同的流派):

<?php function findSection() {
    global $post, $custom_meta_fields, $myGenre;
    foreach ($custom_meta_fields as $fields) {  

        foreach ($fields as $field) {

            if ($field == $fields['genre']) {

                $myGenre = array($field['title']);
                $$myGenre = array();

            } else {
                ${$myGenre}[$field['name']] = $field['id'];
            }
        }
        var_dump($$myGenre);
     }
}


$custom_meta_fields = array(
                            array( //THRILLER
                            'genre' => array( 'title' => 'Thriller'),
                            'fields' => array(
                                        'name' => 'Movie1',
                                        'desc' => 'Desc movie1',
                                        'id' => 'id1',
                                        'type' => 'text'),
                                        array(
                                            'name' => 'Movie2',
                                            'desc' => 'desc movie2',
                                            'id' => 'id2',
                                            'type' => 'text'
                                              ),
                                        array(
                                            'name'  => 'movie3',
                                            'desc'  => 'desc',
                                            'id'    => 'id3',
                                            'type'  => 'image'
                                            )
                            ),
                            array(
                                'genre' => array( 'title' => 'Action'),
                                'fields' =>  array('name' => 'Action1',
                                           'desc' => 'desc act1',
                                           'id'   => 'idAction1')
                            )
                        );

findSection();

谢谢你的帮助

4

1 回答 1

3

我修改了你的代码,使它使用关联数组,因为你用你的双美元做了一些很奇怪的事情。

<?php
function findSection() {
    global $post, $custom_meta_fields, $myGenre;
    foreach ($custom_meta_fields as $fields) {  

        foreach ($fields as $field) {

            if ($field == $fields['genre']) {

                $genre =$field['title'];
                $all[$genre]= array();

            } else {
                $all[$genre][$field['name']] = $field['id'];
            }
        }
     }
    echo "<pre>".var_export($all,TRUE)."</pre>";
}

结果:

array (
  'Thriller' => 
  array (
    'Movie1' => 'id1',
    'Movie2' => 'id2',
    'movie3' => 'id3',
  ),
  'Action' => 
  array (
    'Action1' => 'idAction1',
  ),
)
于 2012-04-28T19:03:35.197 回答