如何附加到 PHP 中的多维数组?
How to append to multidimensional array in PHP?
所以,我使用 API 以 JSON 格式输出信息,(基本上)格式如下:
{"linkId":"1234asdf","clickData"
["member":{"userId":"asdf1234","email":"email@email.com","firstName":"Joe","lastName":"Schmoe"}]},
["member":{"userId":"fdsa4321","email":"liame@liame.com","firstName":"Moreo","lastName":"Hater"}],
-- 对于电子邮件活动中的每个 link。我想通过嵌套的 for 循环提取这些数据并像这样组织它:
$linkCount = count($linkArray); //from previous API call
for ($i = 0; $i < $linkCount; $i++) {
$clickReport[] = //API call here; omitted for brevity
//parse the JSON here to get $linkid, $url; omitted for brevity
$memberCount = substr_count($clickReport[$i],'"member":'); //to count number of people listed in JSON response
for ($j = 0; $j < $memberCount; $j++) {
//parse the JSON here to get $euid, $fname, $lname, $email; omitted for brevity
$clickers[] = array (
$euid[$i] => array (
'fname' => $fname[$j],
'lname' => $lname[$j],
'email' => $email[$j],
'links' => array (
'linkid' => $linkid[$i],
'url' => $url[i],
),
),
);
}
}
我遇到的问题是,如果一个成员单击多个 link,嵌套的 for 循环 会覆盖 前一个 'links' => array()
。我怎样才能这样写 links
added 而不是 overwritten?
单独附加即可。
for ($j = 0; $j < $memberCount; $j++) {
$clickers[] = array(
$euid[$i] => array(
'fname' => $fname[$j],
'lname' => $lname[$j],
'email' => $email[$j]
)
);
$euid[$i]['links'][] = array (
'linkid' => $linkid[$i],
'url' => $url[i],
);
}
所以,我使用 API 以 JSON 格式输出信息,(基本上)格式如下:
{"linkId":"1234asdf","clickData"
["member":{"userId":"asdf1234","email":"email@email.com","firstName":"Joe","lastName":"Schmoe"}]},
["member":{"userId":"fdsa4321","email":"liame@liame.com","firstName":"Moreo","lastName":"Hater"}],
-- 对于电子邮件活动中的每个 link。我想通过嵌套的 for 循环提取这些数据并像这样组织它:
$linkCount = count($linkArray); //from previous API call
for ($i = 0; $i < $linkCount; $i++) {
$clickReport[] = //API call here; omitted for brevity
//parse the JSON here to get $linkid, $url; omitted for brevity
$memberCount = substr_count($clickReport[$i],'"member":'); //to count number of people listed in JSON response
for ($j = 0; $j < $memberCount; $j++) {
//parse the JSON here to get $euid, $fname, $lname, $email; omitted for brevity
$clickers[] = array (
$euid[$i] => array (
'fname' => $fname[$j],
'lname' => $lname[$j],
'email' => $email[$j],
'links' => array (
'linkid' => $linkid[$i],
'url' => $url[i],
),
),
);
}
}
我遇到的问题是,如果一个成员单击多个 link,嵌套的 for 循环 会覆盖 前一个 'links' => array()
。我怎样才能这样写 links
added 而不是 overwritten?
单独附加即可。
for ($j = 0; $j < $memberCount; $j++) {
$clickers[] = array(
$euid[$i] => array(
'fname' => $fname[$j],
'lname' => $lname[$j],
'email' => $email[$j]
)
);
$euid[$i]['links'][] = array (
'linkid' => $linkid[$i],
'url' => $url[i],
);
}