我对此失去了理智。我有一个while循环,我正在制作一个数组。然后我想将该数组传递给另一个函数但它仍然失败。我知道它失败了一些格式化错误,但我不能为我的生活弄清楚如何让它正确格式化。
以下是我需要的格式:
array(
'id' => 'some-id',
'field1' => 'somefield',
'field2' => 'someotherfield',
'title' => 'title',
'subarray' => array(
'sub1' => 'subonevalue',
'sub2' => 'sub2value'
)
),
array(
'id' => 'some-other-id',
'field1' => 'some-other-field',
'field2' => 'some-other-otherfield',
'title' => 'other title',
'subarray' => array(
'sub1' => 'othersubonevalue',
'sub2' => 'othersub2value'
)
),
我正在输出一个while循环,并尝试了以下内容:
global $wp_query;
$the_query = new WP_Query( 'post_type=mycustomposttype' );
$pointer_query = array();
while ( $the_query->have_posts() ) : $the_query->the_post();
$pointer_query[] = array(
'id' => get_post_meta( $post->ID, 'keyid', true ),
'field1' => get_post_meta( $post->ID, 'field1key', true ),
'field2' => get_post_meta( $post->ID, 'field2key', true ),
'title' => get_the_title($post->ID),
'subarray' => array(
'sub1' => get_post_meta( $post->ID, 'sub1key', true ),
'sub2' => get_post_meta( $post->ID, 'sub1key', true )
)
);
endwhile;
endif;
不幸的是,这给了我这样的东西:
Array
(
[0] => Array
(
[id] => first-id
[field1] => first-field1
[field2] => first-field2
[title] => first title
[subarray] => Array
(
[sub1] => first-sub1
[sub2] => first-sub2
)
)
[1] => Array
(
[id] => second-id
[field1] => second-field1
[field2] => second-field2
[title] => second title
[subarray] => Array
(
[sub1] => second-sub1
[sub2] => second-sub2
)
)
)
哪个与我需要的格式不匹配,并导致错误。我觉得这里有一些简单的PHP转换,但是经过几个小时的搜索,我发现空白,现在我很沮丧和沮丧。有人可以帮帮我吗?
更新(因为下面的评论对于格式化代码很糟糕):
我只想将while循环中表示的数组($ pointer_query)传递给类似下面的内容,这里它是完整的(尽管我还尝试将它放在自己的函数中并从其他函数中调用它)因此,评论中的符号如下。
function MyPointers()
{
global $wp_query;
$the_query = new WP_Query( 'post_type=mycustomposttype' );
$pointer_query = array();
while ( $the_query->have_posts() ) : $the_query->the_post();
$pointer_query[] = array(
'id' => get_post_meta( $post->ID, 'keyid', true ),
'field1' => get_post_meta( $post->ID, 'field1key', true ),
'field2' => get_post_meta( $post->ID, 'field2key', true ),
'title' => get_the_title($post->ID),
'subarray' => array(
'sub1' => get_post_meta( $post->ID, 'sub1key', true ),
'sub2' => get_post_meta( $post->ID, 'sub1key', true )
)
);
endwhile;
endif;
$pointers = array($pointer_query);
new SS_Pointer( $pointers );
}
然后调用单独的类SS_Pointer。但问题出在$ pointers数组中。
答案 0 :(得分:0)
如下更改你的SomeFunction,也许它会起作用,很难知道你给我们的东西。
function SomeFunction($array){
return new Object($array);
}
并像这样使用它:
$obj = SomeFunction($pointer_query);
答案 1 :(得分:0)
当您将$pointers
声明为数组时,$pointer_query
已经是一个数组。如果您在var_dump
声明之后前往$pointers
,您会看到:
Array
(
Array
(
[0] => Array
(
[id] => first-id
[field1] => first-field1
[field2] => first-field2
[title] => first title
[subarray] => Array
(
[sub1] => first-sub1
[sub2] => first-sub2
)
)
[1] => Array
(
[id] => second-id
[field1] => second-field1
[field2] => second-field2
[title] => second title
[subarray] => Array
(
[sub1] => second-sub1
[sub2] => second-sub2
)
)
)
)
也就是说,$pointers
将是一个包含一个条目的数组:一个数组,其结构与您在问题开头的数组声明中描述的结构完全相同。所以改变:
$pointers = array($pointer_query);
new SS_Pointer( $pointers );
为:
new SS_Pointer( $pointer_query );
然后SS_Pointer
类应该接收一个具有预期结构的数组。