extracting info within an array from a checkbox selection

Ask about a PHP problem here.
Post Reply
dareskog
Posts: 12
Joined: Sat Jan 21, 2012 2:49 am

extracting info within an array from a checkbox selection

Post 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.
[syntax=php]
$arrayone = array("pizza" => "random unique stuff", "egg" => "again sumin random", "cheese" => "random");
$arraytwo = array("pizza" => "blah", "egg" => "tee hee", "cheese" => "cough");
[/syntax]
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 =]
Last edited by jacek on Tue Jan 31, 2012 7:28 pm, edited 1 time in total.
Reason: code tags...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: extracting info within an array from a checkbox selectio

Post 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 ?
Image
dareskog
Posts: 12
Joined: Sat Jan 21, 2012 2:49 am

Re: extracting info within an array from a checkbox selectio

Post by dareskog »

Cheers Jacek! I'll give this ago! =]
dareskog
Posts: 12
Joined: Sat Jan 21, 2012 2:49 am

Re: extracting info within an array from a checkbox selectio

Post 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..
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: extracting info within an array from a checkbox selectio

Post by jacek »

Ah okay, you could do that using variable variables

[syntax=php]$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'];[/syntax]
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 !
Image
Post Reply