tags:

views:

48

answers:

6

hello, i need help building a function that display an associative array and i want to insert them in a variable. for example i have this assoc array :

$array[ID] = 1;
$array[Name] = John;
$array[age] = 12;

$array[ID]=2;
$array[Name] = Mary;
$array[age] = 14;

i need to make the ID,name,age as variables so i can display it in a table. when i loop through them each time it fills the table row. it has to be each one a variable coz i need to insert them into a gird and display the grid .. where then i can update delete the info

A: 
 foreach($array as $row) {
    echo $row['ID'],$row['Name'],$row['age'];
 }
Mchl
it dosnt return to me as a variable ... i need to use them in a variable coz i need to insert them into a grid where i can edit update delete the information in the database
Mohamed
Your array is aleady a variable. You just need to iterate over it to get the values stored within.
Mchl
@Mohamed $row['Name'] is the same variable as $name. Use it as well
Col. Shrapnel
thanks alot dudes you have just saved me :)
Mohamed
A: 

Im not sure what you want to do exactly, but maybe it is the extract function you are looking for.

Codler
A: 
foreach($array as $key => $value){
  echo $key. ' = '.$value.";
}

would give you

ID = 1 Name = John age = 12

etc

Also be sure to do $array["ID"] = ... instead of $array[ID] = ...

sjobe
A: 

Currently it looks as if you are overwriting the values of the first person with the values of the second person.

What you're looking for is an array structure with more than one dimension, like this:

$people = array(
   1 => array('name' => 'John', 'age' => 12),
   2 => array('name' => 'Mary', 'age' => 14)
);

Then it'll be easy to print out table rows:

foreach($people as $id => $person){
   print '<tr><td>'.$id.'</td><td>'.$person['name'].'</td><td>'.$person['age'].'</td></tr>';
}//foreach

Hope this helps!

sprain
A: 

You can do:

foreach($array as $user) {
    list($age, $name, $id) = array_values($user);
    echo $id, $name, $age;
}

But like others already pointed out, this is pointless because you can much more easily read the values directly from the array through their associative keys. You also wouldnt have to run array_values to assign the array values before being able to assign them with list.

Gordon
`extract()` function, mentioned above, seems more elegant
Col. Shrapnel
@ColShrapnel with `extract` you wouldnt have to do `array_values` but the variable declaration would be implicit. With `list` you can name them whatever you want. Just showing alternatives though. IMO using the associative keys is the easiest and most efficient solution.
Gordon
+1  A: 

I'd use one of the answers provided but if you really _really_ want to (again, i don't see a reason but what the hell do i know) use extract()

<?php
$people = array(
    array('id' => 1, 'name' => 'John', 'age' => 12),
    array('id' => 2, 'name' => 'Mary', 'age' => 14) 
);
foreach($people as $row) {
    extract($row);
    echo "Id: $id, Name: $name, Age: $age\n";
}
//Prints:
//Id: 1, Name: John, Age: 12
//Id: 2, Name: Mary, Age: 14

~

edorian