0

Inside a foreach loop I am assigning an array to a variable.

Because this array is within a loop it will output more than once.

Because it will output more than once I need the end of the array to have a comma so it doesn't break the array for each time it returns and instance of the array.

Is there a way to do this? - I found ways online but they only showed how to do this with strings in foreach loops, to either add or remove the comma at the end of the last foreach loop.

My code is below to explain.

// ----------------------------------------------------------------------------------------------------
// Start our framework config arrays
// ----------------------------------------------------------------------------------------------------

$options = array();

// ----------------------------------------------------------------------
// MENU - Layout Settings
// ----------------------------------------------------------------------

$options[] = 
array(
    'title'  => 'Layout Settings',
    'name'   => 'layout-settings',
    'icon'   => 'fa fa-cog',
    'fields' => 
    array(

        // ----------------------------------------------------------------------
        // TAB - Layout Settings
        // ----------------------------------------------------------------------

        array('type' => 'tabbed', 'id' => 'layout_settings', 'tabs' => array(

            return_post_type_layout_settings()

        ))

    ),
);

// Our return array function

function return_post_type_layout_settings() {

    $public_post_type = get_post_types(
        array(
            '_builtin' => TRUE,
            'public'   => TRUE
        )
    );

    sort($public_post_type, SORT_NATURAL);

    foreach($public_post_type as $post_type) {

        $layout_options = 

            array('title' => ucwords($post_type) . ' Layout', 'fields' => array(

                array('id' => $post_type, 'type' => 'grid', 'span' => '6-12', 'fields' => array(

                    // ----------------------------------------------------------------------
                    // FIELD - Header Settings Panel
                    // ----------------------------------------------------------------------

                    array('message' => 'Enable ' . ucwords($post_type). ' Header Settings?', 'video' => 'QAEjuDpIaE4', 'type' => 'title_with_help'),
                    array('id' => $post_type . '_enable_header', 'type' => 'switcher'),

                ))

            )) // << I need to comma to post at the end of this array
            // because in my array above this will output more than once

        ;

        return $layout_options;

    }

}
5
  • If there is a better way to do this I'm ears, but this is what I have so far in trying to achieve these options to load for each public post type returned. If I could just get the ending comma it should output fine, as I am testing it. Commented Feb 21, 2017 at 4:29
  • I think multiple return statement in foreach loop will not work the way you expected. You need to cascade your results to a single variable output within your foreach and return by the end. (or use Generator Syntax). Commented Feb 21, 2017 at 4:40
  • I'd usually add all values to a single array, then use implode to add commas in-between items. Commented Feb 21, 2017 at 4:41
  • Wait ... the code doesn't make sense. What value do you expected to have in your 'tabs' array? Can you provide an example value for it? Commented Feb 21, 2017 at 4:43
  • Hi. I think you may be right about the multiple return, but not entirely sure because I haven't been able to get pass the comma issue. The value within my tabs array is one instance of the foreach loop. Here is a screenshot of that within the array @ take.ms/Ov544 Commented Feb 21, 2017 at 5:44

2 Answers 2

1

Multiple issues with the code. And there is nothing to do with actual commas. But rather the data structure of function output.

Code Receiving the Function output

In this piece of code:

<?php

$options[] = 
array(
    'title'  => 'Layout Settings',
    'name'   => 'layout-settings',
    'icon'   => 'fa fa-cog',
    'fields' => 
    array(

        // ----------------------------------------------------------------------
        // TAB - Layout Settings
        // ----------------------------------------------------------------------

        array('type' => 'tabbed', 'id' => 'layout_settings', 'tabs' => array(
            return_post_type_layout_settings()
        ))

    ),
);

The tabs is declared only as an array with 1 value. There is no way you can put multiple value into tabs like this. It should be modified like this:

<?php

$options[] = 
array(
    'title'  => 'Layout Settings',
    'name'   => 'layout-settings',
    'icon'   => 'fa fa-cog',
    'fields' => 
    array(

        // ----------------------------------------------------------------------
        // TAB - Layout Settings
        // ----------------------------------------------------------------------

        array('type' => 'tabbed', 'id' => 'layout_settings', 'tabs' => return_post_type_layout_settings())

    ),
);

The Function

Then you need to modify your function to return the correct array format:

function return_post_type_layout_settings() {

    $public_post_type = get_post_types(
        array(
            '_builtin' => TRUE,
            'public'   => TRUE
        )
    );

    sort($public_post_type, SORT_NATURAL);
    $layout_options = array(); // initialize $layout_options as array

    foreach($public_post_type as $post_type) {
        // append each options into $layout_options
        $layout_options[] = 
            array('title' => ucwords($post_type) . ' Layout', 'fields' => array(
                array('id' => $post_type, 'type' => 'grid', 'span' => '6-12', 'fields' => array(

                    // ----------------------------------------------------------------------
                    // FIELD - Header Settings Panel
                    // ----------------------------------------------------------------------
                    array('message' => 'Enable ' . ucwords($post_type). ' Header Settings?', 'video' => 'QAEjuDpIaE4', 'type' => 'title_with_help'),
                    array('id' => $post_type . '_enable_header', 'type' => 'switcher'),
                ))
            ));
    }

    // return after $layout_options is finished
    return $layout_options;

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

1 Comment

Brilliant! Thanks. Small overlook led to another led to a great misuse of much time. Thank you! Life saver.
0

Is the goal to have $layout_options be an array of multiple ["title"=>"My Layout", "fields"=>[..]] objects? If so, I'd do something like this:

function return_post_type_layout_settings() {
    $public_post_type = get_post_types(
        array(
            '_builtin' => TRUE,
            'public'   => TRUE
        )
    );
    sort($public_post_type, SORT_NATURAL);
    $layout_options = array();
    foreach($public_post_type as $post_type) {
        $layout_options[] = 
            array('title' => ucwords($post_type) . ' Layout', 'fields' => array(
                array('id' => $post_type, 'type' => 'grid', 'span' => '6-12', 'fields' => array(
                    // ----------------------------------------------------------------------
                    // FIELD - Header Settings Panel
                    // ----------------------------------------------------------------------
                    array('message' => 'Enable ' . ucwords($post_type). ' Header Settings?', 'video' => 'QAEjuDpIaE4', 'type' => 'title_with_help'),
                    array('id' => $post_type . '_enable_header', 'type' => 'switcher'),
                ))
            ))
        ;
    }
    return $layout_options;
}

3 Comments

Hi. Yes I think we are saying the same thing. For each post type I want the array to output with my options array. Normally I would write out the array, but I can't determine how many post types are on a user's site. So I'm trying to make the array of options output based on the post types detected. I tried your method and played with some of the code too, but all attempts returned nothing. It just broke my array output so no options at all were returned. Here is a screenshot of my output. Highlighted in red is what I need to generate for each post type. Tab & Fields. @ take.ms/Jb7g3
@NoahjChampion Could you provide a code example of what $public_post_type might look like, and then what you would expect $layout_options to look like as output?
This questions is answered above but I'll answer for someone else who might need to differentiate for themselves. This was an easy thing, haha, I just was off as to where I was running my function. If I would have looked at older code I could have spotted that. So the answer above can show the array return I needed. $public_post_type is a Wordpress function and the doc of it can be found @ codex.wordpress.org/Function_Reference/… - Thanks for helping! Appreciate it! #Salute

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.