PHP添加到子数组[重复]

This question already has an answer here:

I am using WordPress and have a custom post type setup with a set number of options, with the basic breakdown being something like this:

  • 30" Round Table
    • Buffet
    • Cocktail
    • Dessert
  • Banquet Table
    • Buffet
    • Gift
    • DJ Table

I am trying to collect all items into a grouped collection, for use with <optgroup /> to show all tables under Buffet, all under Cocktail, etc. like so:

  • Buffet
    • 30" Round Table
    • Banquet Table
  • Cocktail
    • Banquet Table

[and so on]

The PHP issue I'm running into is I have a master array that has all the types (buffet, cocktail, etc.) along with an (initially) empty array element called tables for me to add all the tables that support that specific type. Retrieving the options from ACF works fine, as does retrieving the individual posts from WordPress, so this is strictly a packaging issue to kick back as JSON. Here is a code example:

//gets the field associated with the different table types (Buffet, Cocktail, etc.)
    $field = get_field_object('field_577ff065e6699');

    $list = array();

    //create the base array
    foreach ($field["choices"] as $type) {
        $list[] = array
                  (
                    "type"      => $type, //Buffet, Cocktail, etc.
                    "tables"    => array() //placeholder to add WP items
                  );
    }

    //now get all tables
    $tablesQuery = new WP_Query( array( 'post_type' => 'table', 'posts_per_page' => -1, 'order' => 'ASC' ) );

    //loop through and add the table(s) to their categories                      
    while ( $tablesQuery->have_posts() ) : $tablesQuery->the_post();

        //gets the types supported by this table
        $tableTypes = get_field('options');

        //individual types
        foreach ($tableTypes as $tableType) {

            //all types
            foreach ($list as $supportedType) {

                //see if there is a match and add it
                if ($tableType == $supportedType["type"]) {
                    //add to the array since it matches
                    $table = array
                             (
                                "name"  => get_the_title(),
                                "sqft"  => (int)get_field('square_footage'),
                                "seats" => (int)get_field('seats')
                             );

                    array_push($supportedType["tables"], $table);

                    //shows the single table above, but nothing prior
                    print_r($supportedType);
                }
            }
        }

    endwhile;

    wp_reset_postdata();

    //all "tables" arrays are empty here
    var_dump($list);

The output of print_r($supportedType) above ends up showing all data, however the tables entry is always only one element when it should be multiple:

Array
(
    [type] => Buffet
    [tables] => Array
        (
            [0] => Array
                (
                    [name] => 30” Round Bistro/Cocktail Table
                    [sqft] => 42
                    [seats] => 2
                )

        )

)
Array
(
    [type] => Cake
    [tables] => Array
        (
            [0] => Array
                (
                    [name] => 30” Round Bistro/Cocktail Table
                    [sqft] => 42
                    [seats] => 2
                )

        )

)

[.. snip ..]

Finally, when I do the var_dump($list) at the end, all of the types show up but their associated tables arrays are all empty:

array(11) {
  [0]=>
  array(2) {
    ["type"]=>
    string(6) "Buffet"
    ["tables"]=>
    array(0) {
    }
  }
  [1]=>
  array(2) {
    ["type"]=>
    string(4) "Cake"
    ["tables"]=>
    array(0) {
    }
  }

This has me completely lost, even though it has to be something incredibly basic that I'm missing. Any ideas why tables is empty, despite using array_push on the looped item? I also tried $supportedType["tables"][] = $table, but that has the same effect.

</div>

I think you're working with a copy of the array inside this foreach: foreach ($list as $supportedType) {

Try changing it to

foreach ($list as $key => $supportedType) {
    ...
    // this operates on the copy
    // array_push($supportedType["tables"], $table);
    // operate on the original instead
    array_push($list[$key]["tables"], $table);
    ...
}