に基づいて、ネストされた関連モデルがあります
Project=>ProjectDocument=>ProjectDocumentSection=>ProjectDocumentSectionVariable=>Codeset=>Code
これらの要素を持つ特定の配列を取得するために、包含可能な動作を使用しています (単一のプロジェクトの場合)。
それぞれのモデルでこれらのモデルの関連付けを設定しました。
foreach を使用すると、ProjectDocumentSectionVariable level.
ただし、Codeset レベルに移動しようとすると、エラーが発生します。
関連するコードは次のとおりです。
foreach ($project['ProjectDocument'] as $project_document):
if ($project_document['document_type'] == 1)
{
foreach ($project_document['ProjectDocumentSection'] as $project_document_section):
echo "<h4><a href=\"P4\">" . $project_document_section['title']. "</a></h4><div><table><th>Variable</th><th>Full text</th><th>Type</th><th>Format</th><th>Codeset</th>" ;
foreach ($project_document_section['ProjectDocumentSectionVariable'] as $project_document_section_var):
echo "<tr><td>" . $project_document_section_var['variable_name']
. "</td><td>".$project_document_section_var['variable_description']
. "</td><td>".$project_document_section_var['variable_type']
."</td><td>".$project_document_section_var['variable_format']
."</td><td>".$project_document_section_var['codeset_id'] ;
foreach($project_document_section_var['Codeset'] as $codeset):
foreach($codeset['Code'] as $pcode):
echo $pcode['codevalue'].", ";
endforeach;
endforeach;
echo "</td></tr>";
endforeach;
echo "</table></div>";
endforeach;
}
endforeach;
しかし、Illegal string offset 'Code' と Invalid argument provided for foreach()
at the line echoを取得します$pcode['codevalue'].", ";
の配列を調べましたが$project_document_section_variable
、コードセット配列に問題があると思いますが、なぜそうなっているのかわかりません。
配列の出力の例を次に示します$project_document_section_variable
。
array(
'id' => '32',
'project_id' => '05a',
'project_document_id' => '3',
'project_document_section_id' => '2',
'variable_id' => '2',
'variable_name' => 'Respondent Education',
'variable_description' => 'B1. What is your highest level of education?',
'variable_type' => 'Categorical',
'variable_format' => 'Quantitive',
'codeset_id' => '43',
'Codeset' => array(
'id' => '43',
'title' => 'Educational level',
'Code' => array(
(int) 0 => array(
'id' => '96',
'codeset_id' => '43',
'codevalue' => '1',
'title' => 'No education'
),
(int) 1 => array(
'id' => '97',
'codeset_id' => '43',
'codevalue' => '2',
'title' => 'Primary'
),
(int) 2 => array(
'id' => '98',
'codeset_id' => '43',
'codevalue' => '3',
'title' => 'Secondary'
),
(int) 3 => array(
'id' => '99',
'codeset_id' => '43',
'codevalue' => '4',
'title' => 'Tertiary'
),
(int) 4 => array(
'id' => '100',
'codeset_id' => '43',
'codevalue' => '7',
'title' => 'Other (describe)'
),
(int) 5 => array(
'id' => '101',
'codeset_id' => '43',
'codevalue' => '8',
'title' => 'Refuses to answer'
),
(int) 6 => array(
'id' => '102',
'codeset_id' => '43',
'codevalue' => '9',
'title' => 'Don’t know'
)
)
)
)
コードセット配列に(int) 0
. おそらくこれは、コードセットに対して返される配列が 1 つだけであるためです (これは正しく、それぞれproject_document_section_variable
に関連するコードセットが 1 つだけです。
ここで私が見逃している明白な何かがありますか?