2次元の既知の可能なキーを使用して多次元配列を作成および初期化したいのですが、値はありません。
この配列はevent_ids
(動的に入力されて)格納され、各event_id
配列に対して正確に4つの異なるカウント(これも動的に入力されます)を持ちます。
作りたい構造
Array
(
[0] => Array =================> This index will be the event_id
(
[invitation_not_sent_count] =>
[no_response_yet_count] =>
[will_not_attend_count] =>
[will_attend_count] =>
)
)
私はこれまで何をしましたか?
$DataArray = array();
$DataArray[] = array('invitation_not_sent_count' => '',
'no_response_yet_count' => '',
'will_not_attend_count' => '',
'will_attend_count' => '');
そして、ループ内では、次のように動的にデータを入力しています。
$DataArray[$result->getId()]['no_response_yet_count'] = $NRCount;
私が得るものは:
Array
(
[0] => Array
(
[invitation_not_sent_count] =>
[no_response_yet_count] =>
[will_not_attend_count] =>
[will_attend_count] =>
)
[18569] => Array
(
[no_response_yet_count] => 2
)
[18571] => Array
(
[no_response_yet_count] => 1
)
)。
私が欲しいのは、値が反復で利用できない場合、初期化時に定義されているように、そのエントリは空でなければならないということです。したがって、を除くデータ内の他のすべてのカウントが空の場合no_response_yet_count
、配列は次のようになります。
期待される出力
Array
(
[18569] => Array
(
[invitation_not_sent_count] =>
[no_response_yet_count] => 2
[will_not_attend_count] =>
[will_attend_count] =>
)
[18571] => Array
(
[invitation_not_sent_count] =>
[no_response_yet_count] => 1
[will_not_attend_count] =>
[will_attend_count] =>
)
)。
私は通常、その時点でマッピング関数を使用します。
function pre_map(&$row) {
$row = array
(
'invitation_not_sent_count' => '',
'no_response_yet_count' => '',
'will_not_attend_count' => '',
'will_attend_count' => ''
);
}
次に、while / forループで:
{
$id = $result->getId();
if (!isset($DataArray[$id])) { pre_map($DataArray[$id]); }
$DataArray[$id]['no_response_yet_count'] = $NRCount;
}
if (!isset($DataArray[$id]))
あなたは、同じIDで再ループに起こる場合には、必ずそれは同じインデックス行を一掃しないようにすることです。したがって、マップは1回だけで、ループ内で再びマップされることはありません。
array_map()を使用するなど、他にも1行のショートカットがいくつかありますが、万が一の場合に備えて、完全な柔軟性のために長いバージョンを示していました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加