javascript - 将 php 数组编码为 js 变量缺少一些数据?

标签 javascript php json object multidimensional-array

<分区>

我无法从 angularjs 中的 php 数组获取 interview_docs 数据。我尝试了很多 json 编码选项,但没有成功。如何在 js 中获取 interview_docs 数据 这是我的数组:

Array(
[id] => 1
[tag] => 2
[title] => Test
[job_submission_id] => 0
[job_id] => 14
[candidate_id] => 55
[mail_to] => test@gmail.com
[cc_to] => test@gmail.com
[interviewer] => test@gmail.com
[interview_type] => 1
[interview_date] => 2017-10-04
[from_time] => 00:00:00
[to_time] => 01:00:00
[to_date] => 
[location] => 
[time_zone] => Asia/Kolkata
[status] => 1
[comments] => dwedfwef
[created_by] => 1
[modified_by] => 1
[created_at] => 2017-10-03 12:21:13
[updated_at] => 2017-10-03 12:21:13
[job_title] => Test
[candidate_name] => New t

[candidates] => Array
    (
        [id] => 55
        [first_name] => New
        [last_name] => t
    )

[jobs] => Array
    (
        [id] => 14
        [code] => 10
        [title] => Test
    )

[interview_docs] => Array
    (
        [0] => Array
            (
                [id] => 1
                [interview_id] => 1
                [unique_name] => 2017_10_roa2XKlIDc9gmzmO7TK37rsfuX3YRigqUxbvIYWe.txt
                [name] => first.png
            )

        [1] => Array
            (
                [id] => 2
                [interview_id] => 1
                [unique_name] => 2017_10_6RaAm3MU5ZZ1x4l3q8VLYNhUkgvS3v2ljNZVizm1.png
                [name] => Screenshot (15).png
            )

    )

)

当我这样做时: 变量数据 = <?php echo json_encode($array); ?>; 控制台日志(数据); 我明白了

interview_docs:Array(0)
length:0
 __proto__:Array(0)

我无法从 angularjs 中的 php 数组获取 interview_docs 数据。我尝试了很多 json 编码选项,但它没有醒来。如何获取 interview_docs 数据

最佳答案

如果你想对你的数据进行编码,那么你的数组应该像这样......

如果您想访问数组的任何元素,我还注释了一行。

我在几分钟前测试过它,现在你只需要使用来自 angularjs 的 $http 服务来连接它来连接这个后端。

希望对您有所帮助:)

<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

$array=array(
    "id"=>1,
    "tag"=>2,
    "title"=>"Test",
    "job_submission_id"=>0,
    "job_id"=>14,
    "candidate_id"=>55,
    "mail_to"=>"test@gmail.com",
    "cc_to"=>"test@gmail.com",
    "interviewer"=>"test@gmail.com",
    "interview_type"=>1,
    "interview_date"=>"2017-10-04",
    "from_time"=>"00:00:00",
    "to_time"=>"01:00:00",
    "to_date"=>"",
    "location"=>"",
    "time_zone"=>"Asia/Kolkata",
    "status"=>1,
    "comments"=>"dwedfwef",
    "created_by"=>1,
    "modified_by"=>1,
    "created_at"=>"2017-10-03 12:21:13",
    "updated_at"=>"2017-10-03 12:21:13",
    "job_title"=>"Test",
    "candidate_name"=>"New t",
    "candidates"=>array(
        "id"=>55,
        "first_name"=>"New",
        "last_name"=>"t"
    ),
    "jobs"=>array(
        "id"=>14,
        "code"=>10,
        "title"=>"Test"
    ),
    "interview_docs"=>array(
        "0"=>array(
                "id"=>1,
                "interview_id"=>1,
                "unique_name"=>"2017_10_roa2XKlIDc9gmzmO7TK37rsfuX3YRigqUxbvIYWe.txt",
                "name"=>"first.png"
        ),
        "1"=>array(
                "id"=>2,
                "interview_id"=>1,
                "unique_name"=>"2017_10_6RaAm3MU5ZZ1x4l3q8VLYNhUkgvS3v2ljNZVizm1.png",
                "name"=>"Screenshot (15).png"
        )
    )
);
// echo "unique_name from interview_docs is: <b>".$array["interview_docs"][0]["unique_name"]."</b><br><br>"; // You can access to data like this :)
echo json_encode($array);
?>

关于javascript - 将 php 数组编码为 js 变量缺少一些数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46560245/

相关文章:

java - 使用 JSON key 文件而不是 p12 的 Google 服务帐户域范围委托(delegate)集服务帐户

javascript - 允许用户将多个不同的对象重新排序在一起

javascript - 如果在对象中找到值,则返回键

javascript - 检查 Facebook session 加载情况

php - 有人可以解释一下缓存动态 PHP 页面吗?

php - 什么是读取文件更快的功能?

php - 如何检查远程服务器是否支持 PHP?

javascript - 在控制台中显示 JSON 键

c# - 使用 HttpClient 发布 JSON 数据

javascript - 样式化通过 JS 生成的 SVG