我设法动态创建我的数组(因为$$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();
感谢您的帮助
我修改了你的代码,让它使用关联数组,因为你用双倍的钱做了非常奇怪的事情。
<?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',
),
)