doujuyang1764 2015-04-11 06:18
浏览 38
已采纳

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

First, I would like to mention that I'm terrible at PHP but I'm trying my best to work with it. For some reason I can get information from an array in PHP but I just can't extract information from an array within an array..

So here's the page that var_dumps the array: http://www.farahfa.com/iplanner/webservice/assign_blah.php

And I want to extract all the information in that inner array but I can't figure out how to do it..

Here's my code so far.

<?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;
}
*/
?>

If anyone can help me with this I'd be eternally thankful!

  • 写回答

2条回答 默认 最新

  • duanchuan6350 2015-04-15 02:36
    关注
    <?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);
    

    Then your json file will have this

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

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题