douweinu8562 2017-08-02 20:57
浏览 36
已采纳

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>
  • 写回答

1条回答 默认 最新

  • duanduo0520 2017-08-02 21:05
    关注

    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);
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能