Dragen Posted May 21, 2007 Share Posted May 21, 2007 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 Link to comment https://forums.phpfreaks.com/topic/52392-solved-help-comparing-and-joining-two-arrays-into-a-third/ Share on other sites More sharing options...
per1os Posted May 21, 2007 Share Posted May 21, 2007 <?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? Link to comment https://forums.phpfreaks.com/topic/52392-solved-help-comparing-and-joining-two-arrays-into-a-third/#findComment-258556 Share on other sites More sharing options...
Dragen Posted May 21, 2007 Author Share Posted May 21, 2007 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; } } Link to comment https://forums.phpfreaks.com/topic/52392-solved-help-comparing-and-joining-two-arrays-into-a-third/#findComment-258566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.