php - How foreach this multidimensional array -
my array this:
$data = array( array('name' => 'john', 'surname' =>'doe', 'car' => 'ford'), array('name' => 'john', 'surname' =>'doe', 'position' => 'manager'), array('name' => 'john', 'surname' =>'doe', 'location' => 'la'), array('name' => 'michael', 'surname' =>'smith', 'car' => 'toyota'), array('name' => 'michael', 'surname' =>'smith', 'position' => 'salesman'), array('name' => 'michael', 'surname' =>'smith', 'location' => 'tx') );
how echo array table:
name | surname | car | position | location john | doe | ford | manager | la michael | smith | toyota | salesman | tx
maybe database tables can give idea
persons
id | name | surname
1 | john | doe
2 | michael | smith
properties
id | properties_name
1 | car
2 | position
3 | location
properties_data
id | person_id | properties_id | value
1|1|1|ford
2|1|2|manager
3|1|3|la
4|2|1|toyota
5|2|2|salesman
6|2|3|tx
this work case:
$newarray = array(); foreach ($data $innerarray) { foreach($innerarray $key => $value) { if (isset($newarray[$key])) { if (array_search($value, $newarray[$key]) !== false) { continue; } } $newarray[$key][] = $value; } } var_dump($newarray);
better construct proper sql query.
Comments
Post a Comment