get array values?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
train
Forum Commoner
Posts: 27
Joined: Wed Aug 16, 2006 3:58 pm
Contact:

get array values?

Post by train »

Hello,

I am looking at posted variables like so:

Code: Select all

foreach ($_POST as $key => $value) {
       echo "$key has a value of $value<br>";
But it is the values in the array that I am after... and the resultant output:

Code: Select all

custom_12 has a value of Array
first_name has a value of George
middle_name has a value of Samuel
Doesnt help me too much. How can I take a look at what is IN those arrays?

The array custom_12 is a series of checkboxes, and depending on which is checked, I want to send an email to a particular person. It will be a bunch of if statements, I guess... if this box is checked, send a mail to this person and if this box is checked, send an email to this person... and so on...

Thanks!
SteveMellor
Forum Commoner
Posts: 25
Joined: Mon Jun 26, 2006 7:43 am

Post by SteveMellor »

I think what you might be after is the following.

Assume that I have sent a form with the following field names:
name
address
postCode

now this form is sent as Post data. When the form is recieved by your script you can refer to the variables thus:

$_POST['name']
$_POST['address']
$_POST['postCode']

Any help??
User avatar
train
Forum Commoner
Posts: 27
Joined: Wed Aug 16, 2006 3:58 pm
Contact:

Post by train »

SteveMellor wrote:I think what you might be after is the following.

Assume that I have sent a form with the following field names:
name
address
postCode

now this form is sent as Post data. When the form is recieved by your script you can refer to the variables thus:

$_POST['name']
$_POST['address']
$_POST['postCode']

Any help??
I get 'Array' back for 'custom_12' and it IS an array... but I need to see what is IN the array.

Thanks a lot...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

You can nest foreach loops if your dealing with arrays containing arrays..

Code: Select all

foreach ($_POST as $key => $data)
{
    if (is_array($data))
    {
        foreach ($data as $key => $value) {
            echo "{$key}={$value}<br />\n";
        }
    } else {
        echo "{$key}={$data}<br />\n";
    }
}
User avatar
train
Forum Commoner
Posts: 27
Joined: Wed Aug 16, 2006 3:58 pm
Contact:

Post by train »

astions wrote:You can nest foreach loops if your dealing with arrays containing arrays..

Code: Select all

foreach ($_POST as $key => $data)
{
    if (is_array($data))
    {
        foreach ($data as $key => $value) {
            echo "{$key}={$value}<br />\n";
        }
    } else {
        echo "{$key}={$data}<br />\n";
    }
}
Now I get

Code: Select all

1=1
2=1
3=1
4=1
5=1
first_name=Chevy
middle_name=
Which is a lot closer... the 2=1 means the second checkbox is the "custom_12" array is checked.

And so I guess if that field is "custom_12" then

"custom_12[2]" would be checked?

But when I try

Code: Select all

echo ($_POST[custom_12[2]]);
It doesnt work...


Thanks!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

You would need to refer to it as $_POST['custom_12'][2] I think, not sure though.

Referring to your original question:
Why not set the value of the checkboxes to the email address you want to send to. It would make things alot easier for you.

You could then take astions code and modify it like so.

Code: Select all

foreach ($_POST as $key => $data)
{
    if (is_array($data))
    {
        foreach ($data as $key => $value) {
            mail($value, 'subject', 'message');
        }
    } else {
        echo "{$key}={$data}<br />\n";
    }
}
Instead of just knowing that the second checkbox was checked (meaning nothing in terms of application logic), you will know who to send the email to.
User avatar
train
Forum Commoner
Posts: 27
Joined: Wed Aug 16, 2006 3:58 pm
Contact:

Post by train »

jayshields wrote:You would need to refer to it as $_POST['custom_12'][2] I think, not sure though.

Referring to your original question:
Why not set the value of the checkboxes to the email address you want to send to. It would make things alot easier for you.

You could then take astions code and modify it like so.

Code: Select all

foreach ($_POST as $key => $data)
{
    if (is_array($data))
    {
        foreach ($data as $key => $value) {
            mail($value, 'subject', 'message');
        }
    } else {
        echo "{$key}={$data}<br />\n";
    }
}
Instead of just knowing that the second checkbox was checked (meaning nothing in terms of application logic), you will know who to send the email to.
I cant change the checkboxes because they are dynamically generated.

That doesnt work... I still cant read $_POST[custom_12][2]

Thanks though. This is getting me closer.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

To see where everything is..

Code: Select all

echo '<pre>' . print_r($_POST, true) . '</pre>';
User avatar
train
Forum Commoner
Posts: 27
Joined: Wed Aug 16, 2006 3:58 pm
Contact:

Post by train »

astions wrote:To see where everything is..

Code: Select all

echo '<pre>' . print_r($_POST, true) . '</pre>';
Yeah, thats really cool. Thanks man.

But still, I need to evaluate the array... so if custom_12[2] is 1, a certain action happens.

I guess I can just

Code: Select all

if (custom_12[1] = 1)
    {
     (do this);
    }
if (custom_12[2] = 1)
    {
     (do this);
if (custom_12[3] = 1)
    {
     (do this);
    }
And so on...

You guys rock. Thanks!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I still cant read $_POST[custom_12][2]
I'm not suprised. I said $_POST['custom_12'][2].

Don't use multiple if's. Use a loop.

Code: Select all

foreach($_POST['custom_12'] as $k => $v) {
  //do this
}
User avatar
train
Forum Commoner
Posts: 27
Joined: Wed Aug 16, 2006 3:58 pm
Contact:

Post by train »

jayshields wrote:
I still cant read $_POST[custom_12][2]
I'm not suprised. I said $_POST['custom_12'][2].

Don't use multiple if's. Use a loop.

Code: Select all

foreach($_POST['custom_12'] as $k => $v) {
  //do this
}
The action will vary depending on which member of the array is true, so I dont think the loop will work well.

Thanks. I do appreciate it.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

You should still use a loop...

Code: Select all

foreach($_POST['custom_12'] as $k => $v) {
  if($v == 1) {
    //do this
  }
}
User avatar
train
Forum Commoner
Posts: 27
Joined: Wed Aug 16, 2006 3:58 pm
Contact:

Post by train »

jayshields wrote:You should still use a loop...

Code: Select all

foreach($_POST['custom_12'] as $k => $v) {
  if($v == 1) {
    //do this
  }
}
Will do. Thank you.
Post Reply