简体   繁体   English

PHP - 通过索引将多个项目添加到多维数组

[英]PHP - Add multiple items by index to multidimensional array

I have this array called $all_cats which outputs the following我有一个名为 $all_cats 的数组,它输出以下内容

Array(
    [0] => WP_Term Object(
        [term_id] => 386
        [name] => Ales
        [slug] => ales
        [term_group] => 0
        [term_taxonomy_id] => 386
        [taxonomy] => product_cat
        [description] => 
        [parent] => 384
        [count] => 10
        [filter] => raw
    )
    [1] => WP_Term Object(
        [term_id] => 385
        [name] => Beers
        [slug] => beers
        [term_group] => 0
        [term_taxonomy_id] => 385
        [taxonomy] => product_cat
        [description] => 
        [parent] => 384
        [count] => 10
        [filter] => raw
     )
)

I'm trying to add the "term_id" and "name" to an indexed multidimensional array so i can output the following -我正在尝试将“term_id”和“name”添加到索引多维数组中,这样我就可以 output 以下 -

Example A示例 A

Array
(
    [0] => Array
        (
            [parent_cats] => Array
                (
                    [id] => 385,
                    [name] => "Beers"
                )
        )
    [1] => Array
        (
            [parent_cats] => Array
                (
                    [id] => 386,
                    [name] => "Ales"
                )

        )
)

I've tried the following but cant seem to add each item to the same key.我已经尝试了以下但似乎无法将每个项目添加到同一个键。 How can i add each term_id & name so it outputs like example A?如何添加每个 term_id 和名称,使其输出类似于示例 A?

$full_cats = array();

foreach ($all_cats as $cat_term) {

    $parent_termID = $cat_term->term_id;
    $parent_title = $cat_term->name;

    // this doesnt work
    $full_cats[]['parent_cats']['id'] = $parent_termID;
    $full_cats[]['parent_cats']['name'] = $parent_title;

    // this doesnt work
    array_push($full_cats[]['parent_cats']['id'],$parent_termID);
    array_push($full_cats[]['parent_cats']['name'],$parent_title);

}

How can i add each term_id & name so it outputs like example A?如何添加每个 term_id 和名称,使其输出类似于示例 A?

$full_cats = array();

foreach ($all_cats as $cat_term) {
    $parent_termID = $cat_term->term_id;
    $parent_title = $cat_term->name;

    // this doesnt work
    $full_cats[]=array(
                      "parent_cats"=>array(
                                       "id"   => $parent_termID,
                                       "name" => $parent_title
                      )
                  );
}

The above code should work上面的代码应该可以工作

  • You need to learn the structure of multi and assoc array你需要学习multi和assoc数组的结构
$full_cats = array();
$indx = 0;

foreach ($all_cats as $cat_term) {

  $parent_termID = $cat_term->term_id;
  $parent_title = $cat_term->name;

  // this doesnt work
  $full_cats[$indx]['parent_cats']['id'] = $parent_termID;
  $full_cats[$indx]['parent_cats']['name'] = $parent_title;
  $indx++;

  // this doesnt work
  // array_push($full_cats[]['parent_cats']['id'],$parent_termID);
  // array_push($full_cats[]['parent_cats']['name'],$parent_title);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM