为什么此语句只将最后一个值添加到新数组中?

时间:2014-06-25 16:40:16

标签: php arrays

我卡住了,试图将对象数组中的特定值提取到新数组中。

当我执行以下操作时,它会按预期返回所有可用值:

<?php $speakers = array(); ?>

   <?php foreach($item->extra_fields as $fieldname): ?>  
     <?php if ($fieldname->name == 'Speaker') : ?>
         <?php $speakers[] = $fieldname->value; ?>
      <?php endif; ?>
<?php endforeach;  ?>

<pre><?php print_r($speakers); ?></pre>

当我尝试创建一个关联数组时,如下所示,该数组只有循环的最后一个值:

<?php $speakers = array(); ?>

   <?php foreach($item->extra_fields as $fieldname): ?>  
     <?php if ($fieldname->name == 'Speaker') : ?>
         <?php $speakers[$fieldname->alias] = $fieldname->value; ?>
      <?php endif; ?>

<?php endforeach;  ?>

<pre><?php print_r($speakers); ?></pre>

数组$item->extra_fields如下所示:

Array
(
    [1] => stdClass Object
        (
            [id] => 5
            [name] => Speaker
            [value] => Gudrun Timm
            [type] => textfield
            [group] => 2
            [published] => 1
            [ordering] => 2
            [alias] => name
        )

    [2] => stdClass Object
        (
            [id] => 6
            [name] => Credentials
            [value] => Founder - Link Ideas! - Germany
            [type] => textfield
            [group] => 2
            [published] => 1
            [ordering] => 3
            [alias] => credentials
        )

    [3] => stdClass Object
        (
            [id] => 7
            [name] => Presentation TItle
            [value] => My title is blablalba
            [type] => textfield
            [group] => 2
            [published] => 1
            [ordering] => 4
            [alias] => title
        )

    [5] => stdClass Object
        (
            [id] => 16
            [name] => Picture
            [value] => Picture
            [type] => image
            [group] => 2
            [published] => 1
            [ordering] => 6
            [alias] => picture
        )

    [8] => stdClass Object
        (
            [id] => 11
            [name] => Speaker
            [value] => Idar Kreutzer
            [type] => textfield
            [group] => 2
            [published] => 1
            [ordering] => 9
            [alias] => name
        )

    [9] => stdClass Object
        (
            [id] => 12
            [name] => Credentials
            [value] => CEO - Finance Norway - Norway
            [type] => textfield
            [group] => 2
            [published] => 1
            [ordering] => 10
            [alias] => credentials
        )

)

欢迎任何建议:)

以这种方式解决:

    <?php $speakers = array();

       foreach($item->extra_fields as $fieldname){  
         if ($fieldname->name == 'Speaker'){ 
           $speakers[$fieldname->name][] = $fieldname->value;
         }
       } ?>

    <pre><?php print_r($speakers); ?></pre>

3 个答案:

答案 0 :(得分:3)

您的代码完全按照您的说法运行。查看您正在循环的数据。您只处理数组中的name = 'Speaker'条目,其中有两个条目:键#1和键#8:

[1] => stdClass Object (
        [name] => Speaker
        [alias] => name
)


[8] => stdClass Object (
        [name] => Speaker
        [alias] => name
)

由于两个条目中的alias值都相同,因此您只需覆盖以前保存的条目,最后得到您遇到的最后一个条目。

答案 1 :(得分:1)

我会这样做......

     $speakers = array();

     foreach($item->extra_fields as $fieldname)
        if ($fieldname->name == 'Speaker') : 
        $speakers[$fieldname->id] = $fieldname->value; 
     endif; 
     endforeach;  

这样你就可以用我独特的方式存储它们。 或者甚至可以使用这个名字?

       $speakers[$fieldname->name]= $fieldname->value; 

或者,如果您需要该别名值作为键。

      $speakers[][$fieldname->alias]=$fieldname->value;

答案 2 :(得分:0)

所有发言人&#39;具有相同的别名,所以它将它们全部添加到数组中,但每个连续的条目都会覆盖前一个条目,最后只留下最后一个条目。