Array_walk problem

From the client comes a multi-dimensional array which holds a servicename,also comes a second array which holds an ID and I am trying to use walk_array to attach the ID to the multi-dimensional array…here is the code and take a closer look where I use var_dump…

 $IDs=$_POST['updateIDs'];//holds the ID
 var_dump($output['form']);//holds the service
 var_dump($IDs);
        
        array_walk($output['form'], function ($value, $key) use (&$servicedta, $IDs) {
                if (! isset($IDs[$key])) {
                    return $value;
                }
                $value['id'] = $IDs[$key];
                $servicedta[] = $value;
                });
                
var_dump($servicedta);return;
           

suppose the arrays are these:

array(1) {
  [0]=>
  array(1) {
    ["servicename"]=>
    string(6) "kloper"
  }
}
array(1) {
  [0]=>
  string(3) "443"
}

the (successful) result is this:

array(1) {
  [0]=>
  array(2) {
    ["servicename"]=>
    string(6) "kloper"
    ["id"]=>
    string(3) "443"
  }
}

and not the problem…take a look at these two arrays…

array(1) {
  [1]=>
  array(1) {
    ["servicename"]=>
    string(10) "τeστer"
  }
}
array(1) {
  [0]=>
  string(3) "463"
}

here is the (unsuccessful)result of array_walk…

array(0) {
}

of course this is not cause the values are different…I am assuming that the cause of the problem is the fact that the inner array key of the first multidimensional array begins from 1…but I cannot understand how this might be a problem…and this is where I need help

The problem here is that you try to combine two arrays, where you have no clearly defined relation between the two. if it’s just the position in the arrays, run both arrays through ksort() and array_values() for re-indexing.

array_values seems to do the job…what is the point in using ksort?

In case you get an array like array(1 => 'foo', 0 => 'bar').

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.