我有以下数组
$competency = array(
array(
"assessment_id" => 2,
"activity_id" => 1,
"grade" => 10,
"competency_grade" => array(
array(
"competency_id" => 1,
"grade" => 10
) ,
array(
"competency_id" => 1,
"grade" => 10
)
)
) ,
array(
"assessment_id" => 2,
"activity_id" => 1,
"grade" => 10,
"competency_grade" => array(
array(
"competency_id" => 1,
"grade" => 10
) ,
array(
"competency_id" => 1,
"grade" => 10
)
)
)
);
我将其打印为
echo "<pre>";
print_r($competency);
此行将结果显示为
Array
(
[0] => Array
(
[assessment_id] => 2
[activity_id] => 1
[grade] => 10
[competency_grade] => Array
(
[0] => Array
(
[competency_id] => 1
[grade] => 10
)
[1] => Array
(
[competency_id] => 1
[grade] => 10
)
)
)
[1] => Array
(
[assessment_id] => 2
[activity_id] => 1
[grade] => 10
[competency_grade] => Array
(
[0] => Array
(
[competency_id] => 1
[grade] => 10
)
[1] => Array
(
[competency_id] => 1
[grade] => 10
)
)
)
)
现在我尝试在第一个表中的两个表中插入数据我使用密钥assessment_id,activity_id,基于它返回插入ID的等级,并在第二个表中使用此插入的id作为下面的代码
foreach ($competency as $key => $value) {
foreach ($value as $innerkey=>$innervalue)
{
//echo "<pre>"; print_r($innerkey[0]);
$query = "insert into grading set user_id = '".$user_id."',
assessment_id = '".$innervalue['assessment_id']."',
activity_id = '".$innervalue['activity_id']."',
date = now(),
grade = '".$innervalue['grade']."'";
$result = mysql_query($query);
$grading_id= mysql_insert_id();
if($innerkey=="competency_grade"){
//foreach ($innervalue as )
foreach ($innervalue as $mostinnerkey =>$mostinnervalue)
{
$query1 = "insert into competency_grade set
competency_id = '".$mostinnervalue['competency_id']."',
grade = '".$mostinnervalue['grade']."' ,
grading_id = $grading_id ";
$result1 = mysql_query($query1);
echo mysql_error();
//print_r($mostinnervalue['competency_id']); echo "<br>";
//print_r($mostinnervalue['grade']);echo "<br>";
}
}
}
}
但我无法找到解决方案PLZ帮助我
此致
答案 0 :(得分:2)
****your error might fixed use following script****
foreach ($competency as $key => $value) {
$query = "insert into grading set user_id = '".$user_id."',
assessment_id = '".$value['assessment_id']."',
activity_id = '".$value['activity_id']."',
date = now(),
grade = '".$value['grade']."'";
$result = mysql_query($query);
$grading_id= mysql_insert_id();
foreach ($value as $innerkey => $innervalue)
{
if($innerkey=="competency_grade"){
foreach ($innervalue as $mostinnerkey =>$mostinnervalue)
{
$query1 = "insert into competency_grade set competency_id = '".$mostinnervalue['competency_id']."',
grade = '".$mostinnervalue['grade']."' ,
grading_id = $grading_id ";
$result1 = mysql_query($query1);
echo mysql_error();
}
}
}
答案 1 :(得分:1)
你不需要第二个循环删除它并替换
$innervalue['assessment_id']
使用:
$value['assessment_id']
您的完整代码可能如下所示:
foreach ($competency as $key => $value) {
$query = "insert into grading set user_id = '".$user_id."',
assessment_id = '".$value['assessment_id']."',
activity_id = '".$value['activity_id']."',
date = now(),
grade = '".$value['grade']."'";
$result = mysql_query($query);
$grading_id= mysql_insert_id();
$query1 = "insert into competency_grade set
competency_id = '".$value['competency_grade']['competency_id']."',
grade = '".$value['competency_grade']['grade']."' ,
grading_id = $grading_id ";
$result1 = mysql_query($query1);
echo mysql_error();
}
答案 2 :(得分:0)
你可以这样做
function traversearray($array)
{
foreach($array as $key=>$value)
{
if(is_array($value))
{
traversearray($value);
}
else
{
echo "key = $key value = $value<br/>";
}
}
}
traversearray($competency);
答案 3 :(得分:0)
您可以使用递归函数或array_walk_recursive或迭代器 尝试下面的手动遍历代码
<?php
function array_traverse($competency){
foreach($competency as $key => $val){
if(is_array($val)){
array_traverse($val);
} else{
echo $val;
}
}
}
?>
答案 4 :(得分:0)
fun($competency);
function fun($value)
{
foreach ($value as $val)
{
if( is_array($val))
fun($val);
echo $val;
}
}
试试这个......!