PHP如何从数组中的数组中提取信息?

时间:2015-04-11 06:18:04

标签: php mysql for-loop

首先,我想提一下我在PHP上的表现非常糟糕,但我正在尽力使用它。出于某种原因,我可以从PHP中的数组中获取信息,但我无法从数组中的数组中提取信息。

所以这里是var_dumps数组的页面:http://www.farahfa.com/iplanner/webservice/assign_blah.php

我想提取内部数组中的所有信息,但我无法弄清楚如何做到这一点。

到目前为止,这是我的代码。

<?php

include_once "../config/config.php";

// Expected from the app: student_id & assignment_date

$student_id = $_POST['student_id'];
$assignment_date = $_POST['assignment_date'];

$return=[];

$sql = "SELECT DISTINCT
        s1.name AS subject,
        a.id AS assignmentID,
        sa.id AS subjectAssignID,
        sa.content AS content,
        sa.due_date AS dueDate,
        a.general_teacher_note AS generalNote,
        sn.note_content AS specificNote,
        sub.isSigned AS isSigned
        FROM student AS s
        LEFT JOIN enrollment AS e ON e.id = e.student_id
        LEFT JOIN classroom AS c ON c.id = e.classroom_id
        LEFT JOIN assignment AS a ON c.id = a.classroom_id
        LEFT JOIN submission AS sub ON a.id = sub.assignment_id
        LEFT JOIN specific_notes AS sn ON a.id = sn.assignment_id
        LEFT JOIN subject_assignment AS sa ON a.id = sa.assignment_id
        LEFT JOIN subject_class AS sc ON sc.id = sa.subject_class_id
        LEFT JOIN subject AS s1 ON s1.id = sc.subject_id
        WHERE e.student_id = 2 AND a.date = '2015-04-07'";

$result = $conn->query($sql); // Execute the query


if ($result->rowCount() >= 1) // If there are data returned then do the following
{ 
    $subject_assignments = $result->fetchAll(); // Get all the rows that are returned
    var_dump($subject_assignments);
    for($i = 0; $i < sizeof($subject_assignments); ++$i )
    {
        for($j = 0; $j < sizeof($i); ++$j )
        {
             /*[
                ['subjectAssignID' => $subject_assignments[$i][$j]['subjectAssignID'],
                'subject' => $subject_assignments[$i][$j]['subject'],
                'content' => $subject_assignments[$i][$j]['content'],
                'dueDate' => $subject_assignments[$i][$j]['dueDate']],
                ['subject' => $subject_assignments[$i][$j]['subject'],
                'content' => $subject_assignments[$i][$j]['content'],
                'dueDate' => $subject_assignments[$i][$j]['dueDate']],
                ['generalNote' => $subject_assignments[$i][$j]['generalNote'],
                'specificNote' => $subject_assignments[$i][$j]['specificNote'],
                'assignmentID' => $subject_assignments[$i][$j]['assignmentID'],
                'isDone' => $subject_assignments[$i][$j]['isDone']]
            ]*/
        }
    }
}

/*
 [
  [
   'subjectAssignID' => '1',
   'subject' => 'subjectHere',
   'content' => '1',
   'dueDate' => 'sdfds'
  ],
  ['subject' => 'subjectHere','content' => '1','duedate' => 'sdfds'],
 ],
 'teacherNote' => 'Whatever',
 'parentNote' => 'Note Parent'
 'assignmentID' => '1'

echo json_encode($return); //Makes it a json array
} else {
    echo 0;
}
*/
?>

如果有人能帮助我,我会永远感激!

2 个答案:

答案 0 :(得分:1)

我假设你的sql命令是正确的 我没有检查这段代码,但想法是使用嵌套的foreach循环

<?php

include_once "../config/config.php";

// Expected from the app: student_id & assignment_date
if($_POST['student_id'] && $_POST['assignment_date'])//only except the code if those conditions are true
{

$return=[];

$sql = "SELECT DISTINCT
        s1.name AS subject,
        a.id AS assignmentID,
        sa.id AS subjectAssignID,
        sa.content AS content,
        sa.due_date AS dueDate,
        a.general_teacher_note AS generalNote,
        sn.note_content AS specificNote,
        sub.isSigned AS isSigned
        FROM student AS s
        LEFT JOIN enrollment AS e ON e.id = e.student_id
        LEFT JOIN classroom AS c ON c.id = e.classroom_id
        LEFT JOIN assignment AS a ON c.id = a.classroom_id
        LEFT JOIN submission AS sub ON a.id = sub.assignment_id
        LEFT JOIN specific_notes AS sn ON a.id = sn.assignment_id
        LEFT JOIN subject_assignment AS sa ON a.id = sa.assignment_id
        LEFT JOIN subject_class AS sc ON sc.id = sa.subject_class_id
        LEFT JOIN subject AS s1 ON s1.id = sc.subject_id
        WHERE e.student_id = 2 AND a.date = '2015-04-07'";

$result = $conn->query($sql); // Execute the query


if ($result->rowCount() >= 1) // If there are data returned then do the following
{ 
    $subject_assignments = $result->fetchAll(); // Get all the rows that are returned
    var_dump($subject_assignments);
    foreach($subject_assinments as $subject )
    {
        foreach($subject as $assinment )
        {
           //put your code here
             echo $assinment;

        }  
    }
 }

}

答案 1 :(得分:1)

<?php
//Lets say your $result looks something like this
$result[]= array("subjectAssignID"=> 111, "isSigned"=> "yes", "assignmentID"=>222,
                "subjectName"=>"subject", "content"=>"test", "dueDate"=>"April",
                "generalNote"=>"general", "specificNote"=>"specific"
);

$file = file_get_contents('json.json');
unset($file);
file_put_contents('json.json', json_encode($result));
unset($result);

然后你的json文件会有这个

[{"subjectAssignID":111,"isSigned":"yes","assignmentID":222,"subjectName":"subject","content":"test","dueDate":"April","generalNote":"general","specificNote":"specific"}]

希望这有帮助!