PHP variabelen maken van array met key en value
Ik zou in die foreach de key en values willen uithalen (en in variabelen steken) zoals:
enz...
Kan iemand mij hiermee verder helpen? Ik heb al vanalles geprobeerd maar het wil maar niet lukken.
Laatst heb ik de extract gebruikt maar dit geeft altijd dezelfde waardes terug bij iedere foreach en dat is ook niet de bedoeling.
Code (php)
Gewijzigd op 10/12/2014 20:46:52 door Ozzie PHP
De array ziet er als volgt uit:
Dit is natuurlijk als voorbeeld.
Bekijk mijn voorbeeldje. Je hebt geen foreach loop nodig. (of ik begrijp niet helemaal wat je bedoelt)
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey
$api = new MCAPI($apikey);
$retval = $api->campaigns();
if ($api->errorCode){
echo "Unable to Pull list of Campaign!";
echo "\n\tCode=".$api->errorCode;
echo "\n\tMsg=".$api->errorMessage."\n";
} else {
echo sizeof($retval['total'])." Total Campaigns Matched.\n";
echo sizeof($retval['data'])." Total Campaigns returned:\n";
echo "<br><br>";
foreach($retval['data'] as $c){
echo '<br>';
echo "Campaign Id: ".$c['id']." - ".$c['title']." | ";
echo "Status: ".$c['status']." - type = ".$c['type']." | ";
echo "sent: ".$c['send_time']." to ".$c['emails_sent']." members";
echo "<br>";
$retval2 = $api->campaignStats($c['id']);
//Met dit alleen lukt het wel:
//foreach($retval2 as $k=>$v){
//echo " ".$k." => ".$v."<br>";
//}
//Met dit alleen lukt het dus niet:
extract($retval2);
echo "Verzonden: $emails_sent<br>";
echo "Uitgeschreven: $unsubscribes<br>";
echo "Geopend: $opens<br>";
echo "Hard bounces: $hard_bounces<br>";
echo "Soft bounces: $soft_bounces<br>";
echo "Geopend uniek: $unique_opens<br>";
echo "Clicks: $clicks<br>";
echo "Unieke clicks: $unique_clicks<br>";
echo "users_who_clicked : $users_who_clicked";
}
}
?>
require_once 'inc/MCAPI.class.php';
require_once 'inc/config.inc.php'; //contains apikey
$api = new MCAPI($apikey);
$retval = $api->campaigns();
if ($api->errorCode){
echo "Unable to Pull list of Campaign!";
echo "\n\tCode=".$api->errorCode;
echo "\n\tMsg=".$api->errorMessage."\n";
} else {
echo sizeof($retval['total'])." Total Campaigns Matched.\n";
echo sizeof($retval['data'])." Total Campaigns returned:\n";
echo "<br><br>";
foreach($retval['data'] as $c){
echo '<br>';
echo "Campaign Id: ".$c['id']." - ".$c['title']." | ";
echo "Status: ".$c['status']." - type = ".$c['type']." | ";
echo "sent: ".$c['send_time']." to ".$c['emails_sent']." members";
echo "<br>";
$retval2 = $api->campaignStats($c['id']);
//Met dit alleen lukt het wel:
//foreach($retval2 as $k=>$v){
//echo " ".$k." => ".$v."<br>";
//}
//Met dit alleen lukt het dus niet:
extract($retval2);
echo "Verzonden: $emails_sent<br>";
echo "Uitgeschreven: $unsubscribes<br>";
echo "Geopend: $opens<br>";
echo "Hard bounces: $hard_bounces<br>";
echo "Soft bounces: $soft_bounces<br>";
echo "Geopend uniek: $unique_opens<br>";
echo "Clicks: $clicks<br>";
echo "Unieke clicks: $unique_clicks<br>";
echo "users_who_clicked : $users_who_clicked";
}
}
?>
Gebruik ik de foreach lus ipv de extract functie hier dan werkt het wel. Wat gaat er hier fout.
Gewijzigd op 10/12/2014 21:28:16 door Brecht S
Kun je nog eens uitleggen wat je nu precies wil? Hoe zou je willen dat het werkt?
Ik heb de foreach er terug even als comment ingezet. Als je die alleen gebruikt lukt het wel maar als ik die vervang (zoals in mijn code) met de extract lukt het dus niet. Ik krijg bij de verschillende foreach lussen dezelfde waarden die ik uit de extract haal. Vandaar dacht ik aan een combinatie van een foreach loop met de extract waarde. Ik zou je een online voorbeeld willen tonen maar weet niet goed of er een fiddle progje bestaat om codes te posten zoals bvb jsfiddle (maar is alleen voor html en javascript denk ik).
$retval2 = $api->campaignStats($c['id']);
eens dit:
Wat verschijnt er dan op je scherm?
Code (php)
1
2
2
Campaign Id: 8fdc31614f - LeadMap lanceringscampagne | Status: sent - type = regular | sent: 2014-12-01 03:00:00 to 3232 members
array(21) { ["syntax_errors"]=> float(0) ["hard_bounces"]=> float(26) ["soft_bounces"]=> float(35) ["unsubscribes"]=> float(52) ["abuse_reports"]=> float(1) ["forwards"]=> float(0) ["forwards_opens"]=> float(0) ["opens"]=> float(1373) ["last_open"]=> string(19) "2014-12-10 15:58:41" ["unique_opens"]=> int(876) ["clicks"]=> int(56) ["unique_clicks"]=> int(53) ["users_who_clicked"]=> float(43) ["last_click"]=> string(19) "2014-12-10 15:58:41" ["emails_sent"]=> float(3232) ["unique_likes"]=> float(0) ["recipient_likes"]=> float(0) ["facebook_likes"]=> float(0) ["absplit"]=> array(0) { } ["timewarp"]=> array(0) { } ["timeseries"]=> array(24) { [0]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 03:00:00" ["emails_sent"]=> int(3232) ["unique_opens"]=> int(16) ["recipients_click"]=> int(1) } [1]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 04:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(19) ["recipients_click"]=> int(0) } [2]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 05:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(72) ["recipients_click"]=> int(2) } [3]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 06:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(173) ["recipients_click"]=> int(5) } [4]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 07:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(226) ["recipients_click"]=> int(10) } [5]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 08:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(160) ["recipients_click"]=> int(9) } [6]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 09:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(84) ["recipients_click"]=> int(5) } [7]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 10:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(51) ["recipients_click"]=> int(4) } [8]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 11:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(27) ["recipients_click"]=> int(3) } [9]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 12:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(17) ["recipients_click"]=> int(1) } [10]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 13:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(24) ["recipients_click"]=> int(0) } [11]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 14:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(10) ["recipients_click"]=> int(1) } [12]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 15:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(13) ["recipients_click"]=> int(0) } [13]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 16:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(11) ["recipients_click"]=> int(1) } [14]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 17:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(16) ["recipients_click"]=> int(1) } [15]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 18:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(10) ["recipients_click"]=> int(0) } [16]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 19:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(9) ["recipients_click"]=> int(0) } [17]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 20:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(9) ["recipients_click"]=> int(0) } [18]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 21:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(7) ["recipients_click"]=> int(0) } [19]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 22:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(1) ["recipients_click"]=> int(0) } [20]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 23:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(1) ["recipients_click"]=> int(1) } [21]=> array(4) { ["timestamp"]=> string(19) "2014-12-02 00:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(2) ["recipients_click"]=> int(0) } [22]=> array(4) { ["timestamp"]=> string(19) "2014-12-02 01:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(0) ["recipients_click"]=> int(0) } [23]=> array(4) { ["timestamp"]=> string(19) "2014-12-02 02:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(0) ["recipients_click"]=> int(0) } } }
array(21) { ["syntax_errors"]=> float(0) ["hard_bounces"]=> float(26) ["soft_bounces"]=> float(35) ["unsubscribes"]=> float(52) ["abuse_reports"]=> float(1) ["forwards"]=> float(0) ["forwards_opens"]=> float(0) ["opens"]=> float(1373) ["last_open"]=> string(19) "2014-12-10 15:58:41" ["unique_opens"]=> int(876) ["clicks"]=> int(56) ["unique_clicks"]=> int(53) ["users_who_clicked"]=> float(43) ["last_click"]=> string(19) "2014-12-10 15:58:41" ["emails_sent"]=> float(3232) ["unique_likes"]=> float(0) ["recipient_likes"]=> float(0) ["facebook_likes"]=> float(0) ["absplit"]=> array(0) { } ["timewarp"]=> array(0) { } ["timeseries"]=> array(24) { [0]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 03:00:00" ["emails_sent"]=> int(3232) ["unique_opens"]=> int(16) ["recipients_click"]=> int(1) } [1]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 04:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(19) ["recipients_click"]=> int(0) } [2]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 05:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(72) ["recipients_click"]=> int(2) } [3]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 06:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(173) ["recipients_click"]=> int(5) } [4]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 07:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(226) ["recipients_click"]=> int(10) } [5]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 08:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(160) ["recipients_click"]=> int(9) } [6]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 09:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(84) ["recipients_click"]=> int(5) } [7]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 10:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(51) ["recipients_click"]=> int(4) } [8]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 11:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(27) ["recipients_click"]=> int(3) } [9]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 12:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(17) ["recipients_click"]=> int(1) } [10]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 13:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(24) ["recipients_click"]=> int(0) } [11]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 14:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(10) ["recipients_click"]=> int(1) } [12]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 15:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(13) ["recipients_click"]=> int(0) } [13]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 16:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(11) ["recipients_click"]=> int(1) } [14]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 17:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(16) ["recipients_click"]=> int(1) } [15]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 18:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(10) ["recipients_click"]=> int(0) } [16]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 19:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(9) ["recipients_click"]=> int(0) } [17]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 20:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(9) ["recipients_click"]=> int(0) } [18]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 21:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(7) ["recipients_click"]=> int(0) } [19]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 22:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(1) ["recipients_click"]=> int(0) } [20]=> array(4) { ["timestamp"]=> string(19) "2014-12-01 23:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(1) ["recipients_click"]=> int(1) } [21]=> array(4) { ["timestamp"]=> string(19) "2014-12-02 00:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(2) ["recipients_click"]=> int(0) } [22]=> array(4) { ["timestamp"]=> string(19) "2014-12-02 01:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(0) ["recipients_click"]=> int(0) } [23]=> array(4) { ["timestamp"]=> string(19) "2014-12-02 02:00:00" ["emails_sent"]=> int(0) ["unique_opens"]=> int(0) ["recipients_click"]=> int(0) } } }
Regel 1 geeft de eerste waarden uit $retval weer en regel 2 alles wat in $retval2 zit, dus het detail van $retval. Alleen heb ik hier ook veel dubbele waarden die volledig fout zijn.
Gewijzigd op 10/12/2014 22:36:58 door Brecht S
Omdat PHP geen block scoping heeft, blijven de door extract() aangemaakte variabelen bestaan nadat de foreach-loop is beëindigd, en dat kan ongewenste bijeffecten hebben.
Gewijzigd op 11/12/2014 15:14:09 door Brecht S