isedeasy Posted February 7, 2011 Share Posted February 7, 2011 I have an array which I contains multiple arrays, I would like to loop through and run a function on a certain key within the array I have the following code which works but I was wondering if there was a better method? $i = 0; foreach($Items as $Item) { $Items[$i]['key'] = custom_function($Item['key']); $i++; } The array $Items structure is as follows array ( [0] => array ( [key] => 'blah' ) [1] => array ( [key] => 'blah' ) [2] => array ( [key] => 'blah' ) ) Quote Link to comment https://forums.phpfreaks.com/topic/226992-looping-and-updating-a-multidimensional-array/ Share on other sites More sharing options...
AbraCadaver Posted February 7, 2011 Share Posted February 7, 2011 Should work, not tested: foreach($Items as &$Item) { $Item['key'] = custom_function($Item['key']); } Quote Link to comment https://forums.phpfreaks.com/topic/226992-looping-and-updating-a-multidimensional-array/#findComment-1171134 Share on other sites More sharing options...
Psycho Posted February 7, 2011 Share Posted February 7, 2011 Or just use the array_map() function conveniently built intp PHP $Items = array_map('custom_function', $Items); Of course your function would have to expect the inner array object and return the same. But, I have to ask - why do you have a two dimensional array if the secondary array only consists of a single element? Quote Link to comment https://forums.phpfreaks.com/topic/226992-looping-and-updating-a-multidimensional-array/#findComment-1171139 Share on other sites More sharing options...
isedeasy Posted February 7, 2011 Author Share Posted February 7, 2011 thanks AbraCadaver, that was the first method I tried but I did not have the $Item in the for loop as a reference which is why it did not work mjdamato, the array I posted was just an example to save space, that is why secondary array only has one element. I had a quick look at array_map but looking at your example I can't see where you tell it what element to effect? Thanks for help Quote Link to comment https://forums.phpfreaks.com/topic/226992-looping-and-updating-a-multidimensional-array/#findComment-1171145 Share on other sites More sharing options...
Psycho Posted February 7, 2011 Share Posted February 7, 2011 I had a quick look at array_map but looking at your example I can't see where you tell it what element to effect? You don't specify any field in array_map(). array_map() takes two parameters: a custom functino that you provide and an array. The function is what determines what happens to the values. So, the function would receive one subarray element and should return a sub-array element (with whatver values you want it to have). You don't show what the function 'custom_function' is supposed to do (poor name of a function if I ever saw one). But, let's say the function is supposed to do somethign simple such as change the value of the 'key' element to lower case. Then you would simply write the function like this functin custom_function($sub_array) { $sub_array['key'] = strtolower($sub_array['key']); return $sub_array; } Then if ther are any other elements in teh sub array they will remain unchanged. Only the 'key' element will be changed. Quote Link to comment https://forums.phpfreaks.com/topic/226992-looping-and-updating-a-multidimensional-array/#findComment-1171155 Share on other sites More sharing options...
isedeasy Posted February 7, 2011 Author Share Posted February 7, 2011 Ok thank you, that makes a lot more sense. The 'custom_function' was just for the example, I would not name a function like that Quote Link to comment https://forums.phpfreaks.com/topic/226992-looping-and-updating-a-multidimensional-array/#findComment-1171168 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.