Iris
Iris

Reputation: 287

Why is this statement only adding the last value to the new array?

I'm stuck, trying to fetch specific values from an object array into a new array.

When I do the following, it returns all available values as expected:

<?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>

When I try to create an associative array instead, like below, the array only has the last value of the loop:

<?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>

The array $item->extra_fields looks like this:

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
        )

)

Any advice is welcome please :)

SOLVED IT THIS WAY:

    <?php $speakers = array();

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

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

Upvotes: 0

Views: 62

Answers (3)

Kylie
Kylie

Reputation: 11759

I would just do this...

     $speakers = array();

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

That way you just store them with the unique I'd. Or maybe even use the name?

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

Or if you need that alias value as the key.

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

Upvotes: 1

Marc B
Marc B

Reputation: 360872

Your code is working exactly as you told it to. Look at the data you're looping on. You only deal with the name = 'Speaker' entries in the array, of which there's two: key #1 and key #8:

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


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

Since both alias values in the two entries are the same, you just keep overwriting the previously saved entries, and end up with the LAST entry you encounter.

Upvotes: 3

Jeff Lambert
Jeff Lambert

Reputation: 24671

All of your 'speakers' have the same alias, so it is adding them all into the array, but each successive entry is overwriting the previous leaving you at the end with only the last entry.

Upvotes: 0

Related Questions