简体   繁体   English

如何在php中创建一个空白的关联数组

[英]How to create a blank associative array in php

My php script returns data to the web client where it is processed by javaScript. 我的php脚本将数据返回到由JavaScript处理的Web客户端。

If data is found it is stored in an associative array / object. 如果找到数据,则将其存储在关联数组/对象中。 If no data is found I would like to send a blank associative array. 如果没有找到数据,我想发送一个空白的关联数组。

The only example I have seen on line is in the manual where you create an empty class and then instantiate an object from that. 我在线看到的唯一示例是在手册中,您可以在其中创建一个空类,然后从中实例化一个对象。

Below is my code and the results it produces on the web client side 下面是我的代码以及它在Web客户端生成的结果

            $sql = 'select job, client, project from jobs j left join client c on c.key = j.cKey where j.key='.$this->p['reckey'];
        if ( $result = $db->query($sql) )
        {
            if ($result->num_rows > 0)
            {
                $l = mysqli_fetch_all( $result, $resulttype = MYSQLI_ASSOC );
                $this->res_array['info'] = $l[0];
            }else{
                $this->errors[] = 'No such job # '.$this->p['reckey'];
                $this->res_array['info']=[];
            }
        }else{
            $this->errors[] = 'Query failed!';
            $this->res_array['info']=[];
        }
        $this->res_array['errors'] = $this->errors;
        echo json_encode ($this->res_array);

Here are two examples of what the data looks like when it arrives at the web client before it is decoded by JSON. 以下是两个示例,说明数据在被JSON解码之前到达Web客户端时的样子。 Note the difference in the "info" element. 请注意“info”元素的区别。

response { "info":{"job":"999","client":"My Company, Inc. ","project":"This Project"} ,"errors":[]} 响应{ “info”:{“job”:“999”,“client”:“我的公司,公司”,“项目”:“此项目”} ,“错误”:[]}

error response { "info":[ ] ,"errors":["No such job # 0"]} 错误回复{ “info”:[] ,“errors”:[“没有这样的工作#0”]}

In the successful response I have an object/associative array where I would use the 在成功的响应中,我有一个对象/关联数组,我将使用它

for (variable in object) {...}

In the blank response I just get the standard array [ ] square brackets where I would use the 在空白响应中,我只得到标准数组[]方括号,我将使用

for (step = 0; step < info.length; step++) {}

This occurs of course because I am specifying a blank array in the php code above. 这当然是因为我在上面的php代码中指定了一个空白数组。

My question is simple how can I change my php code so a blank associtive array is transmitted? 我的问题很简单,如何更改我的PHP代码,以便传输空白的关联数组?

The only example I have seen on line is in the manual where you create an empty class and then instantiate an object from that. 我在线看到的唯一示例是在手册中,您可以在其中创建一个空类,然后从中实例化一个对象。

Sounds like you've answered your own question! 听起来你已经回答了自己的问题!

Since in JavaScript an object and an associative array are basically the same thing all you have to do is replace this line: 因为在JavaScript中,对象和关联数组基本上是相同的,所以你要做的就是替换这一行:

$this->res_array['info']=[];

With this one: 有了这个:

$this->res_array["info"] = new StdClass;

Or if you want to make the change only before sending the response you could check for an empty info array and replace it with an empty class. 或者,如果您只想在发送响应之前进行更改,则可以检查空信息数组并将其替换为空类。

if(!count($this->res_array["info"]))
    $this->res_array["info"] = new StdClass;

I would suggest to take the best of both worlds, and let PHP generate an array of associative arrays, even though you expect at the most one, but at least you'd use the same structure for when you have none. 我建议尽可能地利用这两个世界,并让PHP生成一个关联数组的数组,即使你最多也期望,但至少你没有使用相同的结构。

So change: 所以改变:

$this->res_array['info'] = $l[0];

to: 至:

$this->res_array['info'] = $l;

And then in JavaScript do: 然后在JavaScript中执行:

for (var step = 0; step < info.length; step++) {
    for (variable in info[step]) {
        // ...
    }
}

... and if you still need to, you can deal with the no-data condition separately in JavaScript. ...如果你仍然需要,你可以在JavaScript中单独处理无数据条件。 Something like: 就像是:

if (errors.length) {
    // deal with this case...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM