/    Sign up×
Community /Pin to ProfileBookmark

Is this possible to $_POST?

I want to know if I can, through the $_POST method, send a multi-dimensional array.

If not, how can I send a multidimensional array from one PHP page to another?

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperNov 29.2009 — Unless I'm missing something obvious, in any multiple selection form element there is only one name or id so would you need more than 1D?

Just append [] to the form element name and it ends up as $_POST['name'] as an array after submit, i.e.

[CODE]<input type='checkbox' name='options[]' value='1' /> Item one
<input type='checkbox' name='options[]' value='2' /> Item two[/CODE]


[code=php]print_r($_POST['options']);[/code]

Aside from form submission, you can pass arrays of any dimension using session variables from page to page if you wish. Just remember -- unlike a real PHP multidimensional array, $_SESSION keys at the root level must be valid variable names, not numbers.

-jim
Copy linkTweet thisAlerts:
@NogDogNov 29.2009 — Within the same domain, the session option suggested above is probably more efficient than sending the data via post. But if you do need to send it via post for some reason (such as a cURL request to another site or something), you can [url=http://php.net/serialize]serialize[/url]() the array and send the resulting string as a single post field, then unserialize() it in the receiving script.
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorNov 29.2009 — Unless I'm missing something obvious, in any multiple selection form element there is only one name or id so would you need more than 1D?[/QUOTE]

Yes. I am generating an XML page, and what is entered on the form on one page dictates if the generated XML reads:

[code=html] <BoxScore
p_id="PL_Yalenchka_Kasa"
g_pos="START"
ch_pos="G"
min="39:14"
fg="11/20"
thrpt="3/5"
ft="4/4"
plmn="-14"
off="0"
reb="4"
ast="7"
to="5"
stl="2"
bs="0"
ba="2"
pf="3"
pts="29"
top="TOP"
/>
[/code]


or

[code=html] <BoxScore
p_id="PL_Yalenchka_Kasa"
g_pos="BENCH"
dnp="Coach's Decision"
/>
[/code]


It's for a fantasy basketball site; dnp stands for "Did not play". The keys of the sent array will pop up as the names of elements and attributes.
Copy linkTweet thisAlerts:
@opifexNov 29.2009 — edit: Got distracted while posting so I missed the details, but this should help some anyway.

In general the answer is yes, for example if you have
[code=html]
<input name="full_name[]" type="text" id="full_name[]" value="<?php echo $value0; ?>" />
<input name="year_salary[]" type="text" id="year_salary[]" value="<?php echo $value1; ?>" />
<input name="job_title[]" type="text" id="job_title[]" value="<?php echo $value2; ?>" />
<input name="co_name[]" type="text" id="co_name[]" value="<?php echo $value3; ?>" />
[/code]

and you wanted to pull just the "full_name" you can
[code=php]
foreach ($_POST['full_name'] as $name) {
echo $name."n";
}
[/code]

and you get
Aaron Whistler Jr.

Wendy Buttons[/QUOTE]

and if you want to pull the "full_name" AND "year_salary" AND "job_title" AND "co_name" you can do this by using one of them to get the indexes
[code=php]
for ($i=0; $i < count($_POST['full_name']); $i++) {
echo $_POST['full_name'][$i]." earns ";
echo $_POST['year_salary'][$i]." per year as ";
echo $_POST['job_title'][$i]." at ";
echo $_POST['co_name'][$i].".n";
}
[/code]

and you get
Aaron Whistler Jr. earns $1,000,000 per year as assistant mailboy at Whoopee Incorporated.

Wendy Buttons earns $40,000 per year as SysAdmin at WeRtheBest DotCOMM.[/QUOTE]


But, there are other options depending on what you need to do.
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorNov 29.2009 — I did figure out a way around it: Create a seperator.

[code=php]
define('sep' , '|');
$inf = array(
'Game' => array(),
'Home' => array(),
'Away' => array()
);


foreach(array_keys($_POST) as $pkey){
$sep1 = strpos($pkey, sep);
$d1 = substr($pkey, 0, $sep1);
$d2 = substr($pkey, ($sep1+1));
if(strpos($d2, sep)){
$sep2 = strpos($pkey, sep, ($sep1+1));
$d2 = substr($pkey, ($sep1+1), ($sep2 - $sep1 - 1));
$d3 = substr($pkey, ($sep2 + 1));
$inf[$d1][$d2][$d3] = $_POST[$pkey];
} else {
$inf[$d1][$d2] = $_POST[$pkey];
}
}

[/code]
Copy linkTweet thisAlerts:
@opifexNov 29.2009 — Like [I]they[/I] say, "There's more than one way to skin a cat."

[I]But I never could figure out why somebody would want to skin a cat in the first place.[/I]
Copy linkTweet thisAlerts:
@SrWebDeveloperNov 29.2009 — I'd serialize vs. using the separator method.
Copy linkTweet thisAlerts:
@opifexNov 29.2009 — @Mr Initial Man

Since you're generating an xml anyway, you're way ahead of the game!

Just parse the xml for your stats box info... no sense in doing more work than you have to.

[I]...or am I missing something?[/I]
Copy linkTweet thisAlerts:
@Mr_Initial_ManauthorNov 29.2009 — I'd serialize vs. using the separator method.[/QUOTE]

How do you do that with a bunch of form names?

@Mr Initial Man

Since you're generating an xml anyway, you're way ahead of the game!

Just parse the xml for your stats box info... no sense in doing more work than you have to.

[I]...or am I missing something?[/I][/QUOTE]


Okay, what's going on is this is part of a larger update setup: guys who know how to do statistics but not XML use the form to in the numbers. The script generates the XML. They send me the XML, and I add it to a larger XML file.
Copy linkTweet thisAlerts:
@SrWebDeveloperNov 30.2009 — How do you do that with a bunch of form names?[/quote]

Refer to reply #3 for command help - If you have XML multi-dimensional array data prior to generating the form serialize it and pass it as one value, the on form submit in your processing script unserialize and you get back the original data.

-jim
×

Success!

Help @Mr_Initial_Man spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...