I found the following post here on stackoverflow.com about how to create a true and false, very well explained, but how I could enable data boolean
in a array
according to the specification using true
or false
.
For example, I have the following array
that enables me three forms of payment in an orderly manner according to the numerical order.
$MetodoPago = array(
1 => 'Transferencia Bancaria',
2 => 'PayPal',
3 => 'Tarjeta de credito',
);
And, if I want to disable a payment method, I must change the numerical order and comment the method so that it does not appear among the user's visible options, for example.
$MetodoPago = array(
1 => 'Transferencia Bancaria',
//2 => 'PayPal',
2 => 'Tarjeta de credito',
);
I tried the following as a test:
$CreditCard = false;
$enableCreditCard = $CreditCard ? 'Tarjeta de credito' : false;
$MetodoPago =array(
1 => 'Transferencia Bancaria',
2 => $enableCreditCard,
3 => 'PayPal',
);
foreach ($MetodoPago as $key => $value) {
print_r($value);
}
This gives me the following output:
Transferencia BancariaPayPal
Everything seems to be fine, but when printing the variable $key
using print_r($key);
it, it shows me the following result:
123
So my question is the following: how to generate the numbers in order 123
and disable both the number and the text according to the data boolean
in order to have the following result:
print_r($value)
Transferencia BancariaPayPal
....
print_r($key);
12
You can use a helper function to remove from the array those elements whose value is
false
:Note that we pass the array by reference (
&
) in order to modify it.With that function added, your code would look like this: