4

I have a variable $id = 10, it's need to use inside of array_walk(). Like bellow :

$id = 10;
array_walk($profile_items, function(&$a) {
    $count = $this->db->where('profile_item_id', $a['id'])->where('cover_type_id', $id)->count_all_results('cover_type_profile_items_link');
    $a['selected'] = $id;
});
echo "<pre>";
print_r($profile_items).exit;

When i used $id variable inside of array_walk() it's show a error.

Message: Undefined variable: id

Any solution?

Thanks for Advice

LF00
  • 27,015
  • 29
  • 156
  • 295
Razib Al Mamun
  • 2,663
  • 1
  • 16
  • 24
  • Add `use ($id)` after your function paramters, eg: `array_walk($profile_items, function(&$a) use ($id) {`. See [Example #3](http://php.net/manual/en/functions.anonymous.php) in the PHP docs for info. – ccKep Dec 27 '16 at 06:35

5 Answers5

14

You can use use keyword:

array_walk($profile_items, function(&$a) use($id) {

so,

$id = 10;
array_walk($profile_items, function(&$a) use($id) {
    $count = $this->db->where('profile_item_id', $a['id'])->where('cover_type_id', $id)->count_all_results('cover_type_profile_items_link');
    $a['selected'] = $id;
});
echo "<pre>";
print_r($profile_items);

To inherit by reference, add ampersand:

array_walk($profile_items, function(&$a) use(&$id) {
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
2
    $sample = [
        ['id'=>2, 'name' => 'one'],
        ['id'=>3, 'name' => 'two'],
    ];
    $id = 2;
    function test_alter(&$item1, $key, $prefix)
    {   
    if($item1['id'] == $prefix){
        $item1['name'] =  $item1['name']." modified";
        }            
    }
    array_walk($sample, 'test_alter', $id);
    echo "<pre>";
    print_r($sample);

Output

Array(
    [0] => Array
        (
            [id] => 2
            [name] => one modified
        )
    [1] => Array
        (
            [id] => 3
            [name] => two
        )
)
Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55
Pragnesh P
  • 155
  • 2
  • 13
1

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.

Using global keyword outside a function is not an error. It can be used if the file is included from inside a function.

$id = 10;
array_walk($profile_items, function(&$a) {
global $id;
    $count = $this->db->where('profile_item_id', $a['id'])->where('cover_type_id', $id)->count_all_results('cover_type_profile_items_link');
    $a['selected'] = $id;
});
echo "<pre>";
print_r($profile_items).exit;
Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43
1

add parameter to use() like this, if you want to modify the $id, trans it by reference with &, otherwise by value.

array_walk($value, function($v, $k) use ($id){});

or

array_walk($value, function($v, $k) use (&$id){});
LF00
  • 27,015
  • 29
  • 156
  • 295
0

You can pass $id to your function array_walk using use like :

  $id = 10;
  array_walk($profile_items, function(&$a) use($id){
  $count = $this->db->where('profile_item_id', $a['id'])->where('cover_type_id', $id)->count_all_results('cover_type_profile_items_link');
 $a['selected'] = $id;
});
echo "<pre>";
 print_r($profile_items).exit;
vaibhav raychura
  • 162
  • 1
  • 10