Jump to content

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;
}
}

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.