0

A page is posting an array to me like this:

<input type="text" name="fields[email_address][value]" value="1" />
<input type="text" name="fields[first_name][value]" value="jim" />
<input type="text" name="fields[zip_code][value]" value="45254" />...

An array.

I can loop through it like this easy enough

    foreach ( $_POST['fields'] as $key => $field ) {

        echo $key." ".$field['value'] ;
        }

Result of above:

first_name jim
email_address 1
address_postal_code 45254

But what I really need to do is reference just the zip (45254) out of the array, maybe like:

echo $_POST['fields']['zip_code']; //or
echo $_POST['fields']['zip_code']['value']; 

result: 45254

Is this possible?

5
  • 1
    not seeing an issue, why not echo $_POST['fields']['zip_code']['value'] ? Commented Mar 26, 2015 at 23:12
  • or drop the [value] from your input names Commented Mar 26, 2015 at 23:13
  • that's the first thing I tried. notice my sting value: echo $key." ".$field['value'] Commented Mar 26, 2015 at 23:16
  • again, I have no control over the post its from a wordpress plugin. So they post this jsonish array and one of the arrays is named 'fields' Commented Mar 26, 2015 at 23:19
  • how do I get field->zip_code->value ?? Commented Mar 26, 2015 at 23:22

2 Answers 2

2

Update

<input type="text" name="fields[zip_code][value]" value="45254" />

to be

<input type="text" name="fields[zip_code]" value="45254" />

Edit: I wasn't aware you can't modify the html, that wasn't specified in the original question.

The only thing you can really do is do:

$_POST['fields']['zip_code'] = $_POST['fields']['zip_code']['value'];

However at that point, you might as well just assign $_POST['fields']['zip_code']['value'] to a variable and use that.

If you can't update the html of the form, all you can do is manipulate the data after it's been assigned to the $_POST superglobal like it's any other array

Edit 2: Adding a complete snippet to try:

If you do, what do you get?:

<?php
foreach ( $_POST['fields'] as $key => $field ) {
    echo $key." ".$field['value'] ."<br />";
}
echo $_POST['fields']['zip_code']['value'] . "<br />";
$_POST['fields']['zip_code'] = $_POST['fields']['zip_code']['value'];
echo $_POST['fields']['zip_code'];
?>

I just tried that with a simple form of:

<form method="post" action="test.php">
  <input type="text" name="fields[email_address][value]" value="1" />
  <input type="text" name="fields[first_name][value]" value="jim" />
  <input type="text" name="fields[zip_code][value]" value="45254" />
  <input type="submit" />
</form>

And it works as expected.

Sign up to request clarification or add additional context in comments.

7 Comments

please quote array keys
no, it shouldn't have quotes. This is an html input name, adding quotes will add quotes to the keys in the POST data, but i'll update to include the full html tag
I am not making the post and have no control over it. Its coming from a wordpress plugin
foreach ( $_POST['fields'] as $key => $field ) { echo $key." ".$field['value']."<br>" ; }
returns: first_name jim email_address 1 address_postal_code 45254 }
|
0

This seems sloppy but it worked:

foreach ( $_POST['fields'] as $key => $field ) {

$$key = $field['value'] ;
        }

echo $address_postal_code;

45254

echo $_POST['fields']['zip_code']['value'] 

returns nothing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.