Page 1 of 1

extracting info within an array from a checkbox selection

Posted: Mon Jan 30, 2012 7:53 pm
by dareskog
I've got what I want to achieve in my head, but I don't yet think in the way of PHP so I'm having trouble getting it to work in practise.

So I've got (for examples sake) 2 predefined arrays, something like this.
$arrayone = array("pizza" => "random unique stuff", "egg" => "again sumin random", "cheese" => "random");
$arraytwo = array("pizza" => "blah", "egg" => "tee hee", "cheese" => "cough");
I then need somehow for these arrays to be linked to checkboxs (store the variable in the value?). So that if the user selected say both of them, then with a foreach command i need to be able to just extract the "egg" key from within all that have been selected so I can then carry out the rest of my code.

I know how to do this with normal variables. But I don't know how to do this with arrays where i want the same specific data thats attached to the particular "key", in this case the key being "egg".

I think I'm just getting really confused because I'm not sure any of that made sense! Lol but any help would be awesome! Cheers =]

Re: extracting info within an array from a checkbox selectio

Posted: Tue Jan 31, 2012 7:30 pm
by jacek
Well to get the egg out of each array you can use $arrayone['egg'] and $arraytwo['egg']

If 'egg' is the value of another variable you can also do $arrayone[$something] which also works for form inputs $arrayone[$_GET['key']] for example.


Is that what you mean :s ?

Re: extracting info within an array from a checkbox selectio

Posted: Sat Feb 04, 2012 11:04 am
by dareskog
Cheers Jacek! I'll give this ago! =]

Re: extracting info within an array from a checkbox selectio

Posted: Sun Feb 05, 2012 11:34 pm
by dareskog
Okay, I had a read over what you sent when I had some time, but don't really get it. Let me try and explain what i need it to do.
So you've got these arrays predefined at the top of the document. There could be loads of them but they all contain the same 3 keys but the data attached to all these keys are going to be unique.

The user is presented with checkboxes that link to the arrays name. I then need for what ever checkboxes the user has selected to the have the data put into new arrays in a particular array.
So say he selected "arrayfour" and "arraysixty". I then need the data associated with the pizza key, only from the arrays the user has selected, to all be stored into another array. All the data from egg to be stored in an array. And all the data from cheese to be stored into another array. Ive got the next part of the script sorted but it requires it to be sorted this way.

Does that make anymore sense!? haha lol sorry I'm quite bad at explaining..

Re: extracting info within an array from a checkbox selectio

Posted: Mon Feb 06, 2012 12:03 am
by jacek
Ah okay, you could do that using variable variables
$arrayone = array("pizza" => "random unique stuff", "egg" => "again sumin random", "cheese" => "random");
$arraytwo = array("pizza" => "blah", "egg" => "tee hee", "cheese" => "cough");

$selection = 'arrayone'; // This would be the value from the form.

echo $$selection['pizza'];
This would output "random unique stuff".

It might make a bit more sense to store all of your arrays inside one big array, that way you could avoid using variable variables which have a habit being very confusing !