Help Understanding array_walk

After doing some research, I think array_walk is what I need to use for my script. Basically, I want to pull some information with a select query on a mysql table, get the results into an array, perform some calculations on it (using array_walk), then reinsert the results into another table.

The following example prints out the desired results of the function used. array_walk is new to me and I’m trying to put my hands around it and have a few questions. The example code I am using is as follows:

function output_element($a) {
   $b = $a*2;
   print("Element value: $b\
");  
}

array_walk($results, "output_element");
$results = Array ( [0] => 2 [1] => 1 [2] => 3 [3] => 3 )

One of my questions is, is it possible to return basically $b as an array in the same format as $results? I’ve been screwing around with it, but have had no luck as of yet.

I want to check the output of the function before inserting it, as I build the script and the calculations become a little more complexed, but I should be able to insert the results as part of the function, no?

Any input would be greatly appreciated, I’ll continue my research and see what I come up with.

Thanks in advance,
Craig

Able to get $results to print out with the calculations done in the form I think I need. I changed the code to:

function output_element(&$a) {
   $a = $a*2;
   print("Element value: $a\
"); 
}

array_walk($results, "output_element");

print_r ($results);

That changed the value of $results from:

Array ( [0] => 2 [1] => 1 [2] => 3 [3] => 3 )

To:

Array ( [0] => 4 [1] => 2 [2] => 6 [3] => 6 ) 

Good stopping point for the night. I’ll pick it back up in the am, but that’s what I was looking for. I’ll keep working and post any further questions that may (will) come up.

array_map allows multiple arrays to be fed in (as additional parameters to the function, where $a1[1] is evaluated with $a2[1], etc); array_walk takes one input array (and optional single parameter).
array_map returns a new array; array_walk overwrites the values in the input array

My opinions:
Foreach: Breakable, though iterating through multiple arrays can cause readability issues as you try to handle multiple input arrays; retains original array unless overwritten in the code.
array_map: combinative, good for quickly (and readibly) drawing from multiple arrays to generate a single result array; retains original arrays (unless result is assigned to one of the original arrays).
array_walk: shorthand foreach for when there is no break on a single array; transformative. Overwrites original array.

Thank you guys for the input. I looked into array_map as well, to me (not an expert by any means) they both may have worked with my application. Went with array_walk first and it did the trick.

Not sure if foreach will work with my application. The code sample I used was just to check the output of the new array. I will need to insert the results into a new table. Not sure if the $result variable from Paul’s examples would be able to do without any modification.

Looking ahead with the whole script, ideally, I’d like to have the multiplier be a separate number dependent on which group the set belongs to. Any ideas on how to tackle this the most efficient way?

A little further explanation. (Sorry I don’t have specific examples yet as this is still in the concept stage.) I have a row of values from table A with an individual id and a group. There could be multiple (hundreds hopefully) of rows in the same group. Table B would have specific values the values from table A would need to multiplied by based on what group they belong to. Then I’d like to insert all the new values into table C that can be used to pull the updated values from.

Not sure if it’s possible to perform this as one operation or if I’ll need to pull each group’s values from each table and perform the operation on each group individually. Any pointers on which path I should be looking towards would be greatly appreciated. Having the variable values for the separate groups makes the process much more complicated, but would add a ton of value to the final product.

This is kind of the key to my whole application and without a doubt the most ambitious task I’ve attempted thus far in the short time I’ve been programming. My employment circumstances (or lack there of) have allowed me to pursue an idea I’ve had for a while, but lacked the time and knowledge to fulfill. I’m kind of proud of how far I’ve come and enjoy the challenge and thought process involved with the development of my project. I greatly appreciate the generous sharing of ideas the members of this board provide. Without their expertise to help me through my struggles, I probably would have given up a long time ago.

I appreciate any advice in advance.
Craig

array_map vs foreach:

Array_map will essentially go through the entire loop and the result is also an array. Normally I use array_map alot more then foreach.

However, its shortcoming is that it goes through the entire contents of the loop as you cannot specify a break. With foreach on the other hand, you can break the loop any time you wish.

Consider this:

You have 100 elements in you array and the 5th one is not numeric. Using array_map() will still check the remaining 95 values even though you already know the array doesn’t pass your test. However, with foreach you can opt to exit out of the loop at that very instance.

If I’m not mistaken array_walk simply return a Bool value, as opposed to array_map which return an array.

Someone elses input is greatly appreciated. I’m fairly new to all of this =)

Depending on how much work there is to be done on each item, it may be easier for the code to be understood by using a standard foreach statement.


$results = array(1, 2, 3);
foreach ($results as $index => $result) {
    $result = $result * 2;
    $results[$index] = $result;
    print("Element value: $result\
"); 
}

Or you could mix-and-match techniques with:



$results = array(1, 2, 3);
foreach ($results as &$result) {
    $result = $result * 2;
    print("Element value: $result\
"); 
}

I would like to hear more of a discussion about array_map versus array_walk versus foreach?

Under which circumstances is one more appropriate to use than the other? Are there any existing semantics that apply between them?

Or a nice little lambda function. (Probably not advised due to readability issues), but a different way to tackle the problem at hand =)

array_map(function($n) { return $n * 2; }, $results);

Or for PHP < 5.3

array_map(create_function('$n, 'return $n * 2;'), $results); 

But foreach is also good.