Autor | Zpráva | ||
---|---|---|---|
etexweb Profil |
#1 · Zasláno: 21. 7. 2011, 01:18:03 · Upravil/a: etexweb
Ako zistiť počet mien v tomto object cez PHP ?
Events Object ( [Records] => Array ( [0] => Events Object ( [Records] => Array ( ) [ID] => 491741522 [UserID] => Petr [SendDate] => 2011-07-20 [nazov] => TEXT 1 [popis] => ... ) [1] => Events Object ( [Records] => Array ( ) [ID] => 491741569 [UserID] => Petr [SendDate] => 2011-07-20 [nazov] => TEXT 2 [popis] => ... ) ) ) Napr. vypísalo by: 1. Petr 2. Petr <-- Peter je tu - 2 x Skúšal som substr_count() aj array_count_values() ale marne... ĎAKUJEM ZA KAŽDÚ POMOC! :) |
||
Majkl578 Profil |
#2 · Zasláno: 21. 7. 2011, 05:34:50
Procházet takto objekty není zrovna triviální záležitost, o to hůř rekurzivně a na takovém ne zrovna ideálně vypadajícím vzoru, který jsi uvedl.
$prototype = new stdClass; $prototype->records = array(); $a = new stdClass; $a->records = array(); $a->UserID = 'Foo'; $prototype->records[] = $a; $b = new stdClass; $b->records = array(); $b->UserID = 'Bar'; $prototype->records[] = $b; $c = new stdClass; $c->records = array(); $c->UserID = 'Foo'; $b->records[] = $c; $d = new stdClass; $d->records = array(); $d->UserID = 'Baz'; $c->records[] = $d; var_dump($prototype); $people = array(); $walker = function ($item) use (&$people, &$walker) { if ($item->records) { array_walk($item->records, $walker); } if (!isset($people[$item->UserID])) { $people[$item->UserID] = 1; } else { $people[$item->UserID] += 1; } }; array_walk($prototype->records, $walker); var_dump($people); |
||
Časová prodleva: 13 let
|
0