Jump to content

query and alter data in an associative array


bbunlock

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.