stefano861
08-02-2014, 19:39
Ciao a tutti, ho un array multidimensionale come questo:
array $friends:
Array (
[0] => User Object (
[id:User:private] => 6
[username:User:private] => amy
[password:User:private] => fa7f8ff03a1b3d214913bbce87b4b1a1a95cbf79
[email:User:private] => amy@gmail.com
[first_name:User:private] => Amy
[last_name:User:private] => Ross
[city:User:private] => Los Angeles
[gender:User:private] => F
[date:User:private] => 01/01/2000
)
[1] => User Object (
[id:User:private] => 12
[username:User:private] => antony
[password:User:private] => 1799296195846afeef14df836a5db11994803780
[email:User:private] => antony@example.com
[first_name:User:private] => Antony
[last_name:User:private] => Hevia
[city:User:private] => New York
[gender:User:private] => M
[date:User:private] => 01/01/2003
)
[2] => User Object (
[id:User:private] => 17
[username:User:private] => janis
[password:User:private] => e74f86fd784cbedb3cf1e4e1f685a12470fff7bc
[email:User:private] => janis@example.com
[first_name:User:private] => Janis
[last_name:User:private] => Jane
[city:User:private] => //
[gender:User:private] => F
[date:User:private] => ../../....
)
[3] => User Object (
[id:User:private] => 19
[username:User:private] => alex
[password:User:private] => e74f86fd784cbedb3cf1e4e1fa5db11994803780
[email:User:private] => alex@example.com
[first_name:User:private] => Alex
[last_name:User:private] => Smith
[city:User:private] => //
[gender:User:private] => F
[date:User:private] => ../../....
)
}
Voglio ordinare l'array secondo il campo username e first_name.
Ho provato cosė:
$this->sort($this->friends, array('username', 'first_name'));
public function sort($array, $cols) {
$colarr = array();
foreach($cols as $col => $order) {
$colarr[$col] = array();
foreach($array as $k => $row) {
$colarr[$col]['_'.$k] = strtolower($row[$col]); -->line 112
}
}
$eval = 'array_multisort(';
foreach($cols as $col => $order) {
$eval .= '$colarr[\''.$col.'\'],'.$order.',';
}
$eval = substr($eval,0,-1).');';
eval($eval);
$ret = array();
foreach($colarr as $col => $arr) {
foreach($arr as $k => $v) {
$k = substr($k,1);
if(!isset($ret[$k]))
$ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
}
return $ret;
}
Ma genera il seguente errore:
Fatal error: Cannot use object of type User as array in ... on line 112
Come posso risolvere?
Altrimento ho pensato di ordinare l'array al momento della celect al database.
Questa č la mia query:
$stmt = $db->prepare("SELECT * "
. "FROM `users` "
. "INNER JOIN `friend` ON (`username` = `user_req`) "
. "WHERE (`user_acc` = ? AND `status` = '1') "
. "UNION DISTINCT "
. "SELECT * "
. "FROM `users` "
. "INNER JOIN `friend` ON (`username` = `user_acc`) "
. "WHERE (`user_req` = ? AND `status` = '1')");
Se aggiungo un ORDER BY, in questo modo, in funziona pių...
$stmt = $db->prepare("(SELECT * "
. "FROM `users` "
. "INNER JOIN `friend` ON (`username` = `user_req`) "
. "WHERE (`user_acc` = ? AND `status` = '1') "
. "UNION DISTINCT "
. "SELECT * "
. "FROM `users` "
. "INNER JOIN `friend` ON (`username` = `user_acc`) "
. "WHERE (`user_req` = ? AND `status` = '1')) ORDER BY `username`");
E' indifferente come viene ordinato l'array (a livello sql o php), l'importante č che sia ordinato (almeno) secondo l'username.
Come posso fare?
array $friends:
Array (
[0] => User Object (
[id:User:private] => 6
[username:User:private] => amy
[password:User:private] => fa7f8ff03a1b3d214913bbce87b4b1a1a95cbf79
[email:User:private] => amy@gmail.com
[first_name:User:private] => Amy
[last_name:User:private] => Ross
[city:User:private] => Los Angeles
[gender:User:private] => F
[date:User:private] => 01/01/2000
)
[1] => User Object (
[id:User:private] => 12
[username:User:private] => antony
[password:User:private] => 1799296195846afeef14df836a5db11994803780
[email:User:private] => antony@example.com
[first_name:User:private] => Antony
[last_name:User:private] => Hevia
[city:User:private] => New York
[gender:User:private] => M
[date:User:private] => 01/01/2003
)
[2] => User Object (
[id:User:private] => 17
[username:User:private] => janis
[password:User:private] => e74f86fd784cbedb3cf1e4e1f685a12470fff7bc
[email:User:private] => janis@example.com
[first_name:User:private] => Janis
[last_name:User:private] => Jane
[city:User:private] => //
[gender:User:private] => F
[date:User:private] => ../../....
)
[3] => User Object (
[id:User:private] => 19
[username:User:private] => alex
[password:User:private] => e74f86fd784cbedb3cf1e4e1fa5db11994803780
[email:User:private] => alex@example.com
[first_name:User:private] => Alex
[last_name:User:private] => Smith
[city:User:private] => //
[gender:User:private] => F
[date:User:private] => ../../....
)
}
Voglio ordinare l'array secondo il campo username e first_name.
Ho provato cosė:
$this->sort($this->friends, array('username', 'first_name'));
public function sort($array, $cols) {
$colarr = array();
foreach($cols as $col => $order) {
$colarr[$col] = array();
foreach($array as $k => $row) {
$colarr[$col]['_'.$k] = strtolower($row[$col]); -->line 112
}
}
$eval = 'array_multisort(';
foreach($cols as $col => $order) {
$eval .= '$colarr[\''.$col.'\'],'.$order.',';
}
$eval = substr($eval,0,-1).');';
eval($eval);
$ret = array();
foreach($colarr as $col => $arr) {
foreach($arr as $k => $v) {
$k = substr($k,1);
if(!isset($ret[$k]))
$ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
}
return $ret;
}
Ma genera il seguente errore:
Fatal error: Cannot use object of type User as array in ... on line 112
Come posso risolvere?
Altrimento ho pensato di ordinare l'array al momento della celect al database.
Questa č la mia query:
$stmt = $db->prepare("SELECT * "
. "FROM `users` "
. "INNER JOIN `friend` ON (`username` = `user_req`) "
. "WHERE (`user_acc` = ? AND `status` = '1') "
. "UNION DISTINCT "
. "SELECT * "
. "FROM `users` "
. "INNER JOIN `friend` ON (`username` = `user_acc`) "
. "WHERE (`user_req` = ? AND `status` = '1')");
Se aggiungo un ORDER BY, in questo modo, in funziona pių...
$stmt = $db->prepare("(SELECT * "
. "FROM `users` "
. "INNER JOIN `friend` ON (`username` = `user_req`) "
. "WHERE (`user_acc` = ? AND `status` = '1') "
. "UNION DISTINCT "
. "SELECT * "
. "FROM `users` "
. "INNER JOIN `friend` ON (`username` = `user_acc`) "
. "WHERE (`user_req` = ? AND `status` = '1')) ORDER BY `username`");
E' indifferente come viene ordinato l'array (a livello sql o php), l'importante č che sia ordinato (almeno) secondo l'username.
Come posso fare?