KevinM1990112qwq
KevinM1990112qwq

Reputation: 735

Variable to = specific parts of array

I have 36 check boxes... I call them with this. Labeled like this.

 <input type='checkbox' value='1' name='ck[]'>

Called like this

$checks = $_POST['ck']
$vars = array(
                'ck1' => 'Demo',
                'ck2' => 'Demo2',
                'ck3' => 'Demo3',
                'ck4' => 'Demo4',
                'ck5' => 'Demo5',
                'ck6' => 'Demo6',
                'ck7' => 'Demo7',
                'ck8' => 'Demo8',
                'ck9' => 'Demo9',
                'ck10' => 'Demo10',

                );


            foreach($vars as $key=>$default) {
                $checks[$key] = !empty($checks[$key]) ? $default : '';
            }

How can I now make it where 2 new variables = specifics from that post. For instance,

$CH1 = checks[1] - checks[6];
$ch2 = checks[6] - checks[10];

And then,

How can I make it where if there are numbers in $CH1 it will list and break.. like this

 $checks[1] . '<br>' . $checks[2] and so on....

UPDATE***

$vars = array(
                '1' => 'Desktop',
                '2' => 'Laptop Only',
                '3' => 'Laptop / Dock',
                '4' => 'Laptop Case',
                '5' => 'Desk Phone',
                '6' => 'Monitor',
                '7' => 'Printer Access',
                '8' => 'Printer - Personal',
                '9' => 'Email(phone)',
                '10' => 'Office 365',
                '11' => 'Sharepoint',
                '12' => 'Fax Fwd',
                '13' => 'Adobe DC',
                '14' => 'Apacheta',
                '15' => 'Brightree',
                '16' => 'TeamDME',
                '17' => 'DirectView',
                '18' => 'RingCentral',
                '19' => 'Faxage',
                '20' => 'Google Docs',
                '21' => 'Badge Access',
                '22' => 'Title Plate',
                '23' => 'Bis Cards',
                '24' => 'Travel',
                '25' => 'Exp Reports',
                '26' => 'Order Pads',
                '27' => 'Demo Trilogy',
                '28' => 'Stickers',
                '29' => 'APS',
                '30' => 'CBSS',
                '31' => 'Conexis',
                '32' => 'Evenfy',
                '33' => 'Inventory',
                '34' => 'AMEX',
                '35' => 'Fuel Card',
                '36' => 'Vehicle'
                );


            foreach($vars as $key=>$default) {
                $checks[$key] = !empty($checks[$key]) ? $default : '';
            }

When I echo $checks, it just says array.. does not show any numbers names that were checked.

Upvotes: 0

Views: 82

Answers (2)

trincot
trincot

Reputation: 350310

You might want to use array_slice:

$CH1 = array_slice($checks, 0, 6);

Now $CH1 will be an array with the first 6 checks only. To get the next 10 (for instance):

$CH2 = array_slice($checks, 6, 10);

...etc.

Then you can loop through them just like you would do with another array:

foreach($CH1 as $chk) {
    //
}

If you want to check if any of those is numeric, use is_numeric:

foreach($CH1 as $chk) {
    if (is_numeric($chk)) echo $chk . "<br>";
}

You can also filter an array based on a condition. Let's say you want to eliminate the empty elements from the $checks array:

$filtered = array_filter($checks, function ($chk) { return !empty($chk); });

... and now output that filtered array with each entry on a separate line:

echo implode('<br>', $filtered);

Finally, you can use array_intersect_key to get out of your $vars array only those elements that have been checked, as follows:

$result = array_intersect_key($vars, array_flip($_POST['ck']));

So, if the user had selected checkboxes with value 1, 5 and 10, the $results array will look like this:

array (
  1 => 'Desktop',
  5 => 'Desk Phone',
  20 => 'Google Docs',
)

See it on eval.in.

I hope these ideas will all be helpful and of use to you.

Upvotes: 0

SanF
SanF

Reputation: 186

If you are trying to dinamically read the values, you can construct the key value with the name you already have:

$num_a = 1;
$num_b = 6;

$CH1 = checks['ck' . $num_a] - checks['ck' . $num_b];

Upvotes: 2

Related Questions