3

I have a simple piece of code that isn't working as I would expect it would, could someone please explain why it isn't populating the fields array and how to solve it.

$fields = [];

array_walk($class->properties, function($v, $k) use ($fields) {
    $fields[] = $v->name;
});

die(var_dump($fields));

// output is []
3
  • 3
    Pass by reference in the use, not by value: array_walk($class->properties, function($v, $k) use (&$fields){$fields[] = $v->name;}); Commented Aug 11, 2017 at 21:40
  • 1
    worth pointing out for this usecase, the function array_map would have been a better choice. Commented Aug 11, 2017 at 21:53
  • the die() with the var_dump() is gratuitous. Commented Aug 12, 2017 at 0:02

3 Answers 3

5

Use this:

$fields = [];

array_walk($class->properties, function($v, $k) use (&$fields) {
    $fields[] = $v->name;
});

die(var_dump($fields));

After I wrote this I saw Mark Baker comment. That's the right answer.

For reference, see:

Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively, you can use array_map():

$fields = array_map(function($v) {
    return $v->name;
}, $class->properties);

die(var_dump($fields));

For reference, see:

Comments

0

The following code demonstrates a class whose sole property properties contains an array of objects, each of which has a name property, as follows:

<?php

$class = new stdClass;
$class->properties = [new stdClass,new stdClass, new stdClass];

$class->properties[0]->name = "Anne";
$class->properties[1]->name = "Bob";
$class->properties[2]->name = "Robin";

$fieldsA = [];
$fieldsB = [];

if ( array_walk( $class->properties, function( $o ) use ( &$fieldsA ){ 
                                      $fieldsA[] = $o->name;
                                    }) ) {
   echo "\nMission accomplished:\n";
   var_dump($fieldsA);
}

$fieldsB = array_map(  function( $e ) {
                                      return $e->name;
                                      },$class->properties);
if (count($fieldsB) > 0) {
   echo "\nMission accomplished:\n";
   var_dump( $fieldsB );
}

See live code

As you may note, both array_walk() and array_map() will produce the same results of stuffing a new array with the names that the three objects contain. Array_map() offers more simplicity than array_walk() which requires a use variable and a reference operator. Also, the return value of array_map() is a brand new array if all goes well. So, I concur with @localheinz and recommend it as the better choice.

Note: regarding the callback, it is unnecessary to specify value, key parameters. Both of these built-in functions examine each element of properties. Specifying function( $v, $k ) would be helpful if the code needed to do something with an element's key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.