bbunlock Posted July 29, 2010 Share Posted July 29, 2010 Hi and thanks for taking the time to read this and to help if you can I have a flex app that is sending data to a php script that will then perform tasks based on the data found inthe array, my problem is that there are over 270 items in the array and I need to alter many of them BEFORE the script performs its task, what I need is a way to alter the data in the array without having to do it one at a time which I will have to do, below is some exxamples of what I am looking to do for example the array will have data on aprox 35 colors and I need to check and these values and change them if needed, my current system is as follows $color1 = $skin['color1']; if (preg_match("/0x/i", $color1)) { $color1 = str_replace("0x", "", $color1); } else { $color1 = str_pad(dechex($color1), 6, '0', STR_PAD_LEFT); } if($color1=="" || $color1=="000000"){$color1="100000";} now currently I would have to do this individualy for $skin['color1'], $skin['color2'], $skin['color3'] etc etc so is there a way I can do it without such bloated code, for example a while or a foreach statement etc? im not great at php so I am not sure how I would go about this if its possible another thing is that the rest of the values in the array are for image values and I need to fillter through them to find out which are the default images and which are not (no tasks are performed on default images) the following are example values from the array for the arrar item $clockface = $skin['clockface'] /home/thesite/public_html/flex_app/assets/default/clockface.png /home/thesite/public_html/flex_app/gallery/clock/clockface_3.png /home/thesite/public_html/flex_app/uploads/uploaded_file.jpg now depending on the value of the array I would wat the following for the 3 values above $clockface = ""; // if its the first value $clockface = "/home/thesite/public_html/flex_app/gallery/clock/pack3/clockface.svg" // if its the second value $clockface = "/home/thesite/public_html/flex_app/uploads/uploaded_file.jpg" // if its the third value now the above is easy to do on an individual basis but I want to streamline my code and not have an if statement with str_replace etc for each and every value which means I would have to do the code over 200 times for the images now as you can see from the first set of values there are certain things that give clues as to what and where the image is ("assests/default", "gallery/clock/clockface_3.png" and "uploads/uploaded_file.jpg") and I would use these to produce the final values above but again I would have to do it on an individual basis for each array value so is there any way I can streamline the process with again a while or a foreach statement or another way of doing this? your help is very much appriciated regards wayne Link to comment https://forums.phpfreaks.com/topic/209255-query-and-alter-data-in-an-associative-array/ Share on other sites More sharing options...
AbraCadaver Posted July 29, 2010 Share Posted July 29, 2010 For the first part, this will give you an array of colors. Use this same method (which can be in this same loop) to build an array of images (insert where it says // logic here): foreach($skin as $key => $value) { if(stripos($key, 'color') === 0) { // check if key starts with color if(stripos($value, '0x') === 0) { // check if color starts with 0x $colors[$key] = str_replace('0x', '', $value); } else { $colors[$key] = str_pad(dechex($value), 6, '0', STR_PAD_LEFT); } if(empty($color[$key])) { $color[$key] = '100000'; } } elseif($key == 'clockface') { // logic here } } Link to comment https://forums.phpfreaks.com/topic/209255-query-and-alter-data-in-an-associative-array/#findComment-1092793 Share on other sites More sharing options...
bbunlock Posted August 2, 2010 Author Share Posted August 2, 2010 Just wanted to say a big thanks to AbraCadaver for the code provided above, I now have everything work great using your code as a guide thanks you very much regards wayne Link to comment https://forums.phpfreaks.com/topic/209255-query-and-alter-data-in-an-associative-array/#findComment-1094240 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.