dousui3124 2014-05-20 02:21
浏览 98
已采纳

从PHP中的while循环创建一个多维数组

I am losing my mind with this. I have a while loop that I am making into an array. I then want to pass that array to another function but it keeps failing. I know that it is failing for some formatting error, but I can't for the life of me figure out how to get it to format properly.

Here is the format I NEED:

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'
    )
),

I am outputting a while loop, and have tried the following:

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;  

Unfortunately, that gives me back something like this:

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
            )

    )

)

Which does NOT match the format I need, and is causing errors. I feel like there is some simple PHP transformation I am missing here, but after hours of searching I have turned up blank, and now I am depressed and frustrated. Can anyone help me please?

UPDATE (because the comments below are crappy for formatting code):

I just want to pass the array represented in the while loop ($pointer_query) to something like the following, here it is in full (although I have ALSO tried to put it in its own function and call it from the other function, hence the notation below in the comments.

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 );
}

Which then calls a separate class SS_Pointer. But the problem is in the $pointers array.

  • 写回答

2条回答 默认 最新

  • doushi4795 2014-05-20 02:58
    关注

    By the time you declare $pointers as an array, $pointer_query already is an array. If you were to var_dump after your $pointers declaration you would see:

    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
                    )
    
            )
    
        )
    )
    

    That is, $pointers will be an array which contains one entry: an array that would have a structure exactly as you describe in the array declaration at the beginning of your question. So change:

    $pointers = array($pointer_query);  
    new SS_Pointer( $pointers );
    

    to:

    new SS_Pointer( $pointer_query );
    

    And then the SS_Pointer class should then receive an array with a structure it expects.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)