gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:39:15 25 4
gpt4 key购买 nike

首先,我想提一下,我在 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;
}
*/
?>

如果有人能帮助我,我将永远感激不已!

最佳答案

<?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"}]

希望这对您有所帮助!

关于PHP如何从数组内的数组中提取信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29574789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com