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

报告相同问题?

悬赏问题

  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题