26. september 2008 - 23:48
Der er
31 kommentarer og 1 løsning
Tilføj noget til array i foreach
Hej derude... har en forech der kører et array igennem... og der vil jeg gerne opbygge et nyt array... $ar = array ( "$p->name" => array("Start" => $start, "End" => $end, "Total" => $total) ); men hvorden for jeg den til at adde til arrayet? har prøvet at sætte $ar ind som; $ar = array ( "$p->name" => array("Start" => $start, "End" => $end, "Total" => $total), $ar ); men så efter 40 gange i en foreach så bliver dataen skubbet helt ud... så hvad forslår i?
Annonceindlæg fra HP
27. september 2008 - 00:23
#1
$ar["$p->name"] = array("Start" => $start, "End" => $end, "Total" => $total); ?
27. september 2008 - 00:24
#2
<?php $a = array(); for($i=0; $i<100; $i++) { $a[$i] = $i; } print_r($a); ?> Forstår ikke helt hvad du skriver, men jeg er også php noob, men det kan løses sådan her.
27. september 2008 - 00:24
#3
I øvrigt er linie 3 ligegyldig
27. september 2008 - 00:33
#4
$a = array(); <- Den? Den er ikke ligegyldig. Prøv at køre med error_reporting( E_ALL )
27. september 2008 - 11:18
#5
Hm ja den er der næsten... men den driller lidt... sætter arrayet som du sagde og de ser ud til at fungere... foreach ($players->player as $p) { foreach ($p->attendance->event as $a) { $end = $a->end; } $start = $parser->players->player->attendance->event->start; $diff = strtotime($end) - strtotime($start); $timer = floor($diff/3600) % 60; $min = floor($diff/60) % 60; $total = "Total play time: ".$timer." hours & ".$min." minutes"; $ar["$p->name"] = array("Start" => $start, "End" => $end, "Total" => $total); } for Array ( [Aenerion] => Array ( [Start] => SimpleXMLElement Object ( [0] => 2008-09-07T18:53:18Z ) [End] => SimpleXMLElement Object ( [0] => 2008-09-07T23:07:47Z ) [Total] => Total play time: 4 hours & 14 minutes ) [Branwhen] => Array ( [Start] => SimpleXMLElement Object ( [0] => 2008-09-07T18:53:18Z ) [End] => SimpleXMLElement Object ( [0] => 2008-09-07T21:27:02Z ) [Total] => Total play time: 2 hours & 33 minutes ) osv... men kan ikke rigtigt outputte det fornuftigt... prøvede med følgende kode men den giver mig bare de samme data på nær navnene; foreach ( $ar as $key => $value) { print 'Name: ' . $key . ' Start: ' . $start[0] . ' End: ' . $end[0] . ' Total: ' . $total[0] . '<br>'; }
27. september 2008 - 14:41
#6
Prøv: foreach ( $ar as $playername => $values ) { echo "$playername "; foreach ( $values as $key => $val ) { print 'Start: ' . $start[0] . ' End: ' . $end[0] . ' Total: ' . $total[0] . '<br>'; } }
27. september 2008 - 15:20
#7
Nej det fungere heller ikke hehe :) for at sammenligne.. her er et af de arrays; Array ( [Aenerion] => Array ( [Start] => SimpleXMLElement Object ( [0] => 2008-09-07T18:53:18Z ) [End] => SimpleXMLElement Object ( [0] => 2008-09-07T23:07:47Z ) [Total] => Total play time: 4 hours & 14 minutes ) } og med output bliver det til; Aenerion Start: 2008-09-07T18:53:18Z End: 2008-09-07T23:08:34Z Total: T Start: 2008-09-07T18:53:18Z End: 2008-09-07T23:08:34Z Total: T Start: 2008-09-07T18:53:18Z End: 2008-09-07T23:08:34Z Total: T
27. september 2008 - 15:43
#8
foreach ( $ar as $playername => $val ) { echo "$playername "; print 'Start: ' . $val['start'][0] . ' End: ' . $val['end'][0] . ' Total: ' . $val['total'] . '<br>'; }
27. september 2008 - 15:44
#9
foreach ( $ar as $playername => $val ) { print $playername . ' Start: ' . $val['start'][0] . ' End: ' . $val['end'][0] . ' Total: ' . $val['total'] . '<br>'; }
27. september 2008 - 16:02
#10
Hehe så for jeg; Aenerion Start: End: Total: :D
27. september 2008 - 16:07
#11
Altså plus resten af navnene i arrayet men linjer ser sådan ud
27. september 2008 - 16:17
#12
Giver ingen mening :) Prøv lige: foreach ( $ar as $playername => $val ) { echo "<pre>"; var_dump( $val ); echo "</pre>"; print $playername . ' Start: ' . $val['start'][0] . ' End: ' . $val['end'][0] . ' Total: ' . $val['total'] . '<br>'; }
27. september 2008 - 16:32
#13
array(3) { ["Start"]=> object(SimpleXMLElement)#13 (1) { [0]=> string(20) "2008-09-07T18:53:18Z" } ["End"]=> object(SimpleXMLElement)#11 (1) { [0]=> string(20) "2008-09-07T23:07:47Z" } ["Total"]=> string(37) "Total play time: 4 hours & 14 minutes" } Aenerion Start: End: Total:
27. september 2008 - 17:11
#14
Kan ikke forstå det... har det noget at gære med at det er et SimpleXMLElement?
27. september 2008 - 17:21
#15
hehehe: foreach ( $ar as $playername => $val ) { print $playername . ' Start: ' . $val['Start'][0] . ' End: ' . $val['End'][0] . ' Total: ' . $val['Total'] . '<br>'; } Hvis du kørte med error_reporting(E_ALL) ville du have fået notices om at 'start' var et "undefined index" ;)
27. september 2008 - 18:12
#16
Haha for fanden da :P Super ... smid et svar coderdk.. og TAK ;o)
27. september 2008 - 18:24
#17
:)
27. september 2008 - 18:33
#18
Nej nej nej... kan man nu ikke gemme et array i mysql? :P
27. september 2008 - 18:45
#19
<ole> $arr = array("en", "to", "tre"); $sArr = json_encode($arr); // Array'et er nu serialiseret og kan gemmes i en DB $arr = json_decode($sArr); // Strengen er nu et array igen /mvh </bole>
27. september 2008 - 18:47
#20
27. september 2008 - 21:51
#21
Hm ole... hvorfor kan jeg ikke få det til at fungere? Smider midt array som vidst tidligere i en json_encode; $sArr = json_encode($ar); og laver; $ar = json_decode($row[players]); men så for jeg denne fejl når jeg prøver at bruge den som array i den kode v lavede med coderdk tidligere; Fatal error: Cannot use object of type stdClass as array Den har gemt den fint nok i databasen; {"Aenerion":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:07:47Z"},"Total":"Total play time: 4 hours & 14 minutes"},"Branwhen":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T21:27:02Z"},"Total":"Total play time: 2 hours & 33 minutes"},"Damagedealer":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:08:46Z"},"Total":"Total play time: 4 hours & 15 minutes"},"Docmortuus":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Equiilibrium":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:07:30Z"},"Total":"Total play time: 4 hours & 14 minutes"},"Excruciate":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:03:39Z"},"Total":"Total play time: 4 hours & 10 minutes"},"Gtx":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T20:32:49Z"},"Total":"Total play time: 1 hours & 39 minutes"},"Gu00edzmo":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Helik":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Igel":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Ilmatari":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:03:28Z"},"Total":"Total play time: 4 hours & 10 minutes"},"Inimicus":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:07:40Z"},"Total":"Total play time: 4 hours & 14 minutes"},"Keiner":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Kylk":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Leilu":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:03:34Z"},"Total":"Total play time: 4 hours & 10 minutes"},"Lichess":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:08:51Z"},"Total":"Total play time: 4 hours & 15 minutes"},"Munchy":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:07:35Z"},"Total":"Total play time: 4 hours & 14 minutes"},"Necromaniac":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Oakers":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T21:04:32Z"},"Total":"Total play time: 2 hours & 11 minutes"},"Osator":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Platos":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:04:35Z"},"Total":"Total play time: 4 hours & 11 minutes"},"Ponta":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Raytu":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T21:27:07Z"},"Total":"Total play time: 2 hours & 33 minutes"},"Rugdush":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Senara":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:07:18Z"},"Total":"Total play time: 4 hours & 14 minutes"},"Shamotastisc":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:10:26Z"},"Total":"Total play time: 4 hours & 17 minutes"},"Taypeh":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:02:11Z"},"Total":"Total play time: 4 hours & 8 minutes"},"Xerses":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:08:16Z"},"Total":"Total play time: 4 hours & 14 minutes"},"Yukionna":{"Start":{"0":"2008-09-07T18:53:18Z"},"End":{"0":"2008-09-07T23:08:34Z"},"Total":"Total play time: 4 hours & 15 minutes"}}
27. september 2008 - 22:18
#22
Arrrhhh ... det er et associativt array. Sorry, det havde jeg ikke set =) JavaScript - hvis objekt notation, JSON er baseret på - findes ikke associative arrays, hvor PHP's associative arrays oversættes til objekter. Når man så oversætter tilbage, oversætter json_decode naturligvis objekterne til PHP-objekter (stdClass). Så må du bruge serialize og unserialize i stedet =)
27. september 2008 - 22:19
#23
- sikke noget vrøvl :D "I JavaScript - hvis objekt notation, JSON er baseret på - findes ikke associative arrays, hvorfor PHP's associative arrays oversættes til objekter"
27. september 2008 - 22:31
#24
Warning: unserialize() [function.unserialize]: Node no longer exists [Aenerion] => Array ( [Start] => SimpleXMLElement Object Warning: print_r() [function.print-r]: Node no longer exists in C:\wamp\www\wow\raid.php on line 92 ( ) [End] => SimpleXMLElement Object Warning: print_r() [function.print-r]: Node no longer exists in C:\wamp\www\wow\raid.php on line 92 ( ) [Total] => Total play time: 4 hours & 14 minutes )
27. september 2008 - 22:32
#25
Går ud fra mit array skal sættes på en anden måde så? $ar["$name"] = array("Start" => $start, "End" => $end, "Total" => $total);
27. september 2008 - 22:34
#26
Du kan kun bruge unserialize på en streng, der er serialiseret med serialize. Du kan ikke bruge unserialize på den JSON-streng, du viser i (27/09-2008 21:51:07)
27. september 2008 - 22:41
#27
Lav din $ar["$p->name"] = array("Start" => $start, "End" => $end, "Total" => $total); om til $ar["$p->name"] = array("Start" => $start[0], "End" => $end[0], "Total" => $total); og lav print $playername . ' Start: ' . $val['Start'][0] . ' End: ' . $val['End'][0] . ' Total: ' . $val['Total'] . '<br>'; om til print $playername . ' Start: ' . $val['Start'] . ' End: ' . $val['End'] . ' Total: ' . $val['Total'] . '<br>'; Giver den dig mere bøvl, så lav $ar["$p->name"] = array("Start" => $start[0], "End" => $end[0], "Total" => $total); om til $ar["$p->name"] = array("Start" => (string)$start[0], "End" => (string)$end[0], "Total" => $total); :)
28. september 2008 - 19:53
#28
$ar["$p->name"] = array("Start" => (string)$start[0], "End" => (string)$end[0], "Total" => $total); med print $playername . ' Start: ' . $val['Start'] . ' End: ' . $val['End'] . ' Total: ' . $val['Total'] . '<br>'; = Yay :o) Hehe takker begge 2 for jeres super stædighed! :o) det fungere bare perfekt nu! Smid et svar begge 2.. og endnu engang TAK! :)
28. september 2008 - 19:54
#29
Hov åndsvagt så har jeg lukket kan jeg se... ønsker i nogen point olebole/coderdk? Så opretter jeg gerne et nyt spørgsmål..
28. september 2008 - 20:04
#30
Nej, ellers tak. Bare du får løst problemet - jeg har længe vidst, tråden var lukket. Men det er udtryk for god E-stil, du tænker på det ;o)
28. september 2008 - 20:07
#31
- og så var det jo forøvrigt også 'coder-dyret', der endte med at redde orgiet ;D
28. september 2008 - 20:11
#32
hehe og jeg har faktisk fået point :)
Vi tilbyder markedets bedste kurser inden for webudvikling