-1

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.

Stephen
  • 8,038
  • 6
  • 41
  • 59
  • 1
    Looks like it matches, how exactly does it not? – ElefantPhace May 20 '14 at 02:25
  • I'm not seeing where anything is wrong, the var_dump looks exactly like what you're expecting. What is not expected in the output? – joe42 May 20 '14 at 02:25
  • For some reason, when I pass this array to my other function, it fails. It seems that it needs it formatted EXACTLY as above, with quotes and everything (as a string maybe), or it won't work. When I paste the entire array formatted like the FIRST part above, as TEXT..it works...but when I try to pull the values from the db and pass the array..it fails. – Stephen May 20 '14 at 02:27
  • 1
    Then it's a problem with your 'other function' – ElefantPhace May 20 '14 at 02:28
  • how could it be, when pasting the text in exactly works just fine? – Stephen May 20 '14 at 02:28
  • when you paste the text from a var dump? – joe42 May 20 '14 at 02:29
  • Cause you're passing text, not an array... – ElefantPhace May 20 '14 at 02:29
  • I need to somehow pass my array as a perfectly formatted string... – Stephen May 20 '14 at 02:29
  • Then turn your array into a string? – ElefantPhace May 20 '14 at 02:30
  • No, when I take the VALUES I get back from my array, format it as the first part above, and paste it into the place in my function that would otherwise pull the array. – Stephen May 20 '14 at 02:30
  • Better yet, why don't you post your function so we know what you're talking about – ElefantPhace May 20 '14 at 02:31
  • Trying to understand this — so this function doesn't expect an actual array, it expects a string containing a declaration of an array in PHP syntax? – Josh Davenport-Smith May 20 '14 at 02:32
  • yes, the 2nd function expects a string formatted like the FIRST part above, NOT what I am getting back when I try to pass the array. – Stephen May 20 '14 at 02:32
  • You should fix your function then to work properly as you expect it – ElefantPhace May 20 '14 at 02:34
  • I feel if I could somehow just pass the array as a string it would work, but I have not been able to do that. Anyone know? – Stephen May 20 '14 at 02:34
  • You could use var_export(), I would recommend you fix you're other function though ;) – joe42 May 20 '14 at 02:35
  • I have no idea what you mean by "fix my other function" – Stephen May 20 '14 at 02:36
  • 1
    Okay, well you should know that is really bad practice on the part of whoever wrote that function. It is likely using [eval](http://www.php.net/manual/en/function.eval.php) to convert a declaration of an array in PHP syntax to an actual array. That has potential to cause serious security issues ([info here](http://stackoverflow.com/questions/951373/when-is-eval-evil-in-php)). That function really should be modified to actually accept a standard array. – Josh Davenport-Smith May 20 '14 at 02:36
  • Why in the world would you pass a string, that is obviously an array, instead of just fixing your function? – ElefantPhace May 20 '14 at 02:36
  • 1
    if you're passing an array, the function should just take an array as a parameter, not a string with an array in the perfect format. – joe42 May 20 '14 at 02:36
  • By fix, we all mean fix it to expect an array, not a string – ElefantPhace May 20 '14 at 02:37
  • It is completely possible (since I am new to this) that I am not passing those values correctly, so please (instead of screaming with your hands in the air) help me understand. – Stephen May 20 '14 at 02:40
  • 1
    As I've said, post your function and the call to it. And lol @joe42! – ElefantPhace May 20 '14 at 02:41
  • 2
    it looks like you're constructing your array correctly, but for us to help you fix the other function, and not scream, we'd need to see it – joe42 May 20 '14 at 02:42
  • the other function I need to pass this array to looks like this: function SomeFunction() { $bigarray = array(myArrayabove()); new Object($bigarray); } – Stephen May 20 '14 at 02:43
  • That function takes no parameters. And what function is myArrayabove()? And it's better if you add the actual code you're using to the original question, not in comments – ElefantPhace May 20 '14 at 02:45
  • well, it works just dandy if instead of passing the array to the place where myArrayabove() goes I just paste in the values properly formatted. And btw, I have also tried putting $pointer_query and $pointer_query() in the same place, to no avail. – Stephen May 20 '14 at 02:47
  • 1
    Because `$pointer_query` is out of that function's scope. You need to pass it as an argument to your function and then there is no need to make an array out of an already existing array – ElefantPhace May 20 '14 at 02:50
  • Well I hope you're not assigning an empty array to a variable function name ;) use $pointer_query since it's not a function, it's a variable. Can we see how someFunction() uses the "array" – joe42 May 20 '14 at 02:51
  • 1
    We're having real trouble working out what is actually going on in your code. That's why there are so many comments on this question. Help us help you: http://stackoverflow.com/help/mcve – Josh Davenport-Smith May 20 '14 at 02:53
  • I just added more detail above. – Stephen May 20 '14 at 02:55
  • Unfortunately, that still isn't much help. `$pointer_query` is already an array. Why are you making `$pointer`? – ElefantPhace May 20 '14 at 03:00

2 Answers2

0

Change your SomeFunction as follows, maybe it'll work, hard to know with what you've given us.

function SomeFunction($array){
    return new Object($array);
}

And use it like this:

$obj = SomeFunction($pointer_query);
ElefantPhace
  • 3,806
  • 3
  • 20
  • 36
0

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.

Josh Davenport-Smith
  • 5,456
  • 2
  • 29
  • 40
  • I think this solves this part of the problem, thanks! – Stephen May 20 '14 at 03:05
  • Could've had your answer 10 minutes ago if you looked at my answer and saw that I didn't have the redundant array – ElefantPhace May 20 '14 at 03:07
  • Josh actually bothered to explain to me what was happening, helping me to understand, instead of just being insulting. So it was actually more helpful. – Stephen May 20 '14 at 03:10
  • If you say so, next time listen to the people who are trying to help you the first time so we don't end up with ~30 comments back and forth. – ElefantPhace May 20 '14 at 03:18
  • You may think your comments above were helpful, but I honestly encourage you to look at what you said and try to imagine how they were helpful instead of flip. It might help to remember that not everyone is as expert as you, and a little kindness and patience goes a long way. – Stephen May 20 '14 at 03:39
  • OK boy. Just next time provide information when it is requested instead of saying I don't know what you mean and I can't do that – ElefantPhace May 20 '14 at 05:18