What I’m trying to do is default the $names array value to it’s parallel $urls value if a $_POST[] value of $names is empty. (This is based on the assumption that an empty $_POST[] will return empty, someone please correct me if I’m wrong).
Here’s my code:
$urls = array(1 => $_POST['url_1'], 2 => ['url_2'], 3 => $_POST['url_3'], 4 => $_POST['url_4'], 5 => $_POST['url_5']);
$names = array(1 => $_POST['name_1'], 2 => ['name_2'], 3 => $_POST['name_3'], 4 => $_POST['name_4'], 5 => $_POST['name_5']);
if(empty($names[1])) { $names[1] = $_POST['url_1']; }
if(empty($names[2])) { $names[2] = $_POST['url_2']; }
if(empty($names[3])) { $names[3] = $_POST['url_3']; }
if(empty($names[4])) { $names[4] = $_POST['url_4']; }
if(empty($names[5])) { $names[5] = $_POST['url_5']; }
I thought about using a foreach() loop, but I don’t really see how that would work, as each individual array value e.g. $names[1] would have to be set to $urls[1] if empty.
Any advice, comments or other information would be very much appreciated :)!
1 Answer