i have a variable array that gets all what my function has retrieved.
$array = $funcs->searchCompany($bizName);
and then i used foreach to check if the value is null for varchar and 0 for int and then i replace its value to "Not Provided" so that everytime it is being called it will say "Not Provided"
foreach ($array as $var) {
if($var == " " || $var == 0) {
$var = "Not Provided";
}
}
$name = $var['name'];
$url = $var['url'];
$tagline = $var['tagline'];
$descrip = $var['descrip'];
$bemail = $var['bemail'];
$address = $var['address'];
$city = $var['city'];
but it seems wrong because it destroys the output instead.
You can use &
here to pass the value of array to change inside foreach without actually worrying about which is the current array key, which is also sometimes called as passing a variable's value by reference.
Using foreach
foreach ($array as &$value) // note the &
{
if(empty($value)) $value = 'Not Provided';
// other values remain untouched
}
Using array_map()
$array = array_map(function($value){
if(empty($value))
return 'Not Provided';
return $value;
}, $array);
But i will suggest to go with foreach.
update your function so something like
foreach ($array as &$var) {
if($var == " " || $var == 0) {
$var = "Not Provided";
}
}
$name = $array['name'];
$url = $array['url'];
$tagline = $array['tagline'];
$descrip = $array['descrip'];
$bemail = $array['bemail'];
$address = $array['address'];
$city = $array['city'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With