简体   繁体   English

Foreach在PHP中创建多级而不是单级数组

[英]Foreach creating multi-level instead of single level array in PHP

I'm trying to gather a group of term_id's output in a foreach and create an array from them. 我正在尝试在foreach中收集一组term_id的输出并从中创建一个数组。 I then want to update the taxonomy with the values in the array however the array is being created as multi-level. 然后,我想用数组中的值更新分类,但是数组是作为多级创建的。 My code is as follows: 我的代码如下:

$updateTax = array();
foreach ($featuresArray as $key => $value) {
    if ($key = 'en_value') {

        $termResult = get_term_by('name', $value['en_value'], $taxonomy);
        $term = $termResult->term_id;
        $updateTax[] = array($term);

    }
}

...which then gives this output: ...然后给出这个输出:

var_dump($updateTax);

array(29) {

 [0]=> array(1) {
  [0]=> int(111) } [1]=> array(1) {
  [0]=> int(116) } [2]=> array(1) {
  [0]=> int(124) } [3]=> array(1) {
  ...
  [0]=> int(408) } [25]=> array(1) {
  [0]=> int(447) } [26]=> array(1) {
  [0]=> int(520) } [27]=> array(1) {
  [0]=> int(593) } [28]=> array(1) {
  [0]=> int(628) }

  }

...but I was expecting the following: ......但我期待以下内容:

array(29) {

 [0]=> int(111) } 
 [1]=> int(116) }
 [2]=> int(124) }
 [3]=> int(125) }
 ...

Bit puzzled so could do with some guidance please. 有点困惑,所以可以做一些指导,请。 Many thanks. 非常感谢。

Replace the following line, where you are creating an individual array for each $term : 替换以下行,您要为每个$term创建单独的数组:

$updateTax[] = array($term);

With this: 有了这个:

$updateTax[] = $term;

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

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