tags:

views:

796

answers:

12
foreach ($_GET as $field => $label)
{
    $datarray[]=$_GET[$field];

    echo "$_GET[$field]";
    echo "<br>";
}
print_r($datarray);


This is the output I am getting. I see the data is there in datarray but when I echo $_GET[$field] I only get "Array"

But print_r($datarray) prints all the data. Any idea how I pull those values?

OUTPUT

Array Array ( [0] => Array ( [0] => Grade1 [1] => ln [2] => North America [3] => yuiyyu [4] => iuy [5] => uiyui [6] => yui [7] => uiy [8] => 0:0:5 ) ) Array

A: 

Use <pre> tags before print_r, then you will have a tree printed (or just look at the source. From this point you will have a clear understanding of how your array is and will be able to pull the value you want.

I suggest further reading on $_GET variable and arrays, for a better understanding of its values

Fernando Barrocal
this is what i get Warning: Invalid argument supplied for foreach() in /home/public_html/process.php on line 27
A: 

You need to edit this question to have a proper name/title. "php problem" is way too generic.

lo_fye
A: 

Perhaps the GET variables are arrays themselves? i.e. http://site.com?var[]=1&amp;var[]=2

Joshua
A: 

calling echo on an array will always output "Array". print_r (from the PHP manual) prints human-readable information about a variable.

ksuralta
A: 

foreach ($_GET[0] as $field => $label) { $datarray[]=$_GET[$field];

echo "$_GET[$field]";
echo "<br>";

} print_r($datarray);

RESULT:

Warning: Invalid argument supplied for foreach() in /home/public_html/process.php on line 27

Yup, because you don't have a nested array. Don't let those guys confuse you :)
Aeon
A: 

It looks like your GET argument is itself an array. It would be helpful to have the input as well as the output.

smo
+1  A: 

Use var_export($_GET) to more easily see what kind of array you are getting.

From the output of your script I can see that you have multiple nested arrays. It seems to be something like:

$_GET = array( array( array("Grade1", "ln", "North America", "yuiyyu", "iuy", "uiyui", "yui","uiy","0:0:5")))

so to get those variables out you need something like:

echo $_GET[0][0][0]; // => "Grade1"
jackbravo
he doesn't have nested arrays; the first Array is the output of echo "$_GET[$field]", the second is the output of print_r($datarray);
Aeon
A: 

I think we can understand more if you show us the link which gives such strange $_GET

Giuda
A: 

I changed the code in the flash file and now i only get the last value in the array

here is the link

http://hofstrateach.org/Roberto/story_1.html

+1  A: 

EDIT: When I completed your test, here was the final URL:

http://hofstrateach.org/Roberto/process.php?keys=Grade1&amp;keys=Nathan&amp;keys=North%20America&amp;keys=5&amp;keys=3&amp;keys=no&amp;keys=foo&amp;keys=blat&amp;keys=0%3A0%3A24

This is probably a malformed URL. When you pass duplicate keys in a query, PHP makes them an array. The above URL should probably be something like:

http://hofstrateach.org/Roberto/process.php?grade=Grade1&amp;schoolname=Nathan&amp;region=North%20America&amp;answer[]=5&amp;answer[]=3&amp;answer[]=no&amp;answer[]=foo&amp;answer[]=blat&amp;time=0%3A0%3A24

This will create individual entries for most of the fields, and make $_GET['answer'] be an array of the answers provided by the user.

Bottom line: fix your Flash file.

Nathan Strong
PHP only makes keys into an array when they have [] at the end; otherwise it just overwrites the duplicate key with the last specified value.
Aeon
Thanks Aeon, updated my example accordingly.
Nathan Strong
A: 

Try this:

foreach ($_GET as $field => $label)
{
    $datarray[]=$_GET[$field];

    echo $_GET[$field]; // you don't really need quotes

    echo "With quotes: {$_GET[$field]}"; // but if you want to use them

    echo $field; // this is really the same thing as echo $_GET[$field], so

    if($label == $_GET[$field]) {
         echo "Should always be true<br>";
    }
    echo "<br>";
}
print_r($datarray);
SeanDowney
A: 

It's printing just "Array" because when you say

 echo "$_GET[$field]";

PHP can't know that you mean $_GET element $field, it sees it as you wanting to print variable $_GET. So, it tries to print it, and of course it's an Array, so that's what you get. Generally, when you want to echo an array element, you'd do it like this:

echo "The foo element of get is: {$_GET['foo']}";

The curly brackets tell PHP that the whole thing is a variable that needs to be interpreted; otherwise it will assume the variable name is $_GET by itself.

In your case though you don't need that, what you need is:

foreach ($_GET as $field => $label)
{
    $datarray[] = $label;
}

and if you want to print it, just do

echo $label; // or $_GET[$field], but that's kind of pointless.

The problem was not with your flash file, change it back to how it was; you know it was correct because your $dataarray variable contained all the data. Why do you want to extract data from $_GET into another array anyway?

Aeon