Jump to content

[SOLVED] Help comparing and joining two arrays into a third


Dragen

Recommended Posts

Okay.. I'm having some difficulty with some arrays.

I have two arrays which will both have the same amount of variables in them, but different information for each one for example:

$dday('mon => 1', 'tues => 0', 'weds => 1', 'thurs => 1', 'fri => 1')
$ddesc('mon => hello', 'tues => bye', 'weds => worms', 'thurs => parakeet', 'fri => somebody say something')

 

As you can see the names are the same, but the settings are not. I'm trying to write a foreach statement or something similar which will go through both arrays and for each matching one (mon & mon, tues & tues etc) it will put those two together to create another seperate array, so I'll end up with a final array with something like this:

$sday('mon => 1#hello', 'tues => 0#bye', 'weds => 1#worms', 'thurs => 1#parakeet', 'fri => 1#somebody say something')

note: I'm wanting it to place a # between the two values, but I also want it to cheek if the $ddesc array has a set value on the day (mon, tues, weds etc) it's checking. If not, then it doesn't add the # or that value to the $sday array. Instead it just adds the value from $dday.

 

I hope all my ramblings made some sense.. I've been trying to figure it our for a while with no luck.

Thanks

<?php
$dday = array('mon' => '1', 'tues' => '0', 'weds' => '1', 'thurs' => '1', 'fri' => '1');
$ddesc = array('mon' => 'hello', 'tues' => 'bye', 'weds' => 'worms', 'thurs' => 'parakeet', 'fri' => 'somebody say something');

foreach ($dday as $key => $val) {
    if (isset($ddesc[$key])) {
       $newArr[$key] = $val . '#' . $ddesc[$key] . '#';
    }
}

echo '<pre>', print_r($newArr), '</pre>';
?>

 

Something like that?

ah thanks! I knew it'd be so simple...

I've edited it slightly to work for me as the isset() wouldn't work, because the value is always set, just sometimes as nothing.

foreach ($dday as $key => $value) {
if($ddesc[$key] != ''){
	$sday[$key] = $value . '#' . $ddesc[$key];
}else{
	$sday[$key] = $value;
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.