Friday, 31 August 2018

To create a multidimensional array from a while loop in PHP

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.

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.

0 comments:

Post a Comment