0

I am hoping I can get some assistance with some issues I'm trying to get the array data from one function to be used in another function.

So in the function that's creating the array data, it looks like this:

function wlp_generate_pdf_and_save( $data, $post_id ){
    // Function needing array data
}

function wlp_generate_preview($data, $generateType){
    $out_product_brands = array();
    if( count($all_posts) > 0 ){
        foreach( $all_posts as $s_post )
        {
            ...
            $out_product_brands[] = array( 'brand' => vooHelperNew::get_posts_products( $s_post->ID ) );
            ...
        }
    }
}

This function "wlp_generate_preview" works perfectly, but it's this $out_product_brands data i need to use in the function (wlp_generate_pdf_and_save) above this one.

Does the order the functions are place change anything?

2
  • $all_posts didn't declared in the function arguments Commented Nov 15, 2019 at 8:26
  • You can use $this keyword when you refer to any properties and methods within same class. inside function wlp_generate_pdf_and_save() call function you want like this $this->wlp_generate_preview($data, $generateType) Commented Nov 15, 2019 at 8:26

2 Answers 2

1

Return the data from wlp_generate_preview then pass it as a parameter of wlp_generate_pdf_and_save

Sign up to request clarification or add additional context in comments.

Comments

0

First you want to return the array from your first function and then send that as a variable to the second function:

<?php



function wlp_generate_pdf_and_save( $data, $post_id, $input_array ){ //added a third 
varible to this functions input
// Function needing array data
}

function wlp_generate_preview($data, $generateType){
    $out_product_brands = array();
    if( count($all_posts) > 0 ){
        foreach( $all_posts as $s_post )
    {
        ...
        $out_product_brands[] = array( 'brand' => vooHelperNew::get_posts_products( $s_post->ID ) );
        ...
    }
}

return $out_product_brands;//Returned The array
}

And then when calling your functions you might do something like this:

    $input_array = wlp_generate_preview($data, $generateType);//Calls the first function

    wlp_generate_pdf_and_save($data, $post_id, $input_array);//The third variable passes it into the next function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.