我如何遍历这个荒谬乏味的数组?
array (size=6)
0 =>
array (size=1)
'Question' =>
array (size=2)
'id' => string 'q_1' (length=3)
'question_desc' => string 'Is this correct?)' (length=15)
1 =>
array (size=1)
'Question' =>
array (size=2)
'id' => string 'q_10' (length=4)
'question_desc' => string 'Do you weigh less than 45 kilograms OR more than 160 kilograms.' (length=63)
这是一个会话数据的var_dump !我需要从每个'Question'
数组对象中获取question_desc
字段。
这个数组的结构有一个目的,但是我理解你的沮丧,因为我在修改之前分享了它!
$flattened_data = array();
foreach($your_main_array as $question)
{
foreach($question['Question'] as $question_param)
{
if($question_param == 'question_desc')
{
$flattened_data[] = $question_param;
// if you want to be really cool you can do this instead
// this will list the array with the question id as the key.
// $flattened_data[$question[id]] = $question_param;
}
}
}
// now flattened data has only what you require
return $flattened_data;
一旦你理解了cake的ORM以及它是如何使用模型关系的,它的数据表单就更有意义了。它实际上是一个管理数据的强大工具,但在您需要所有这些功能之前,它看起来确实像是简单任务的累赘。