简体   繁体   English

PHP中的空数组-> JSON-> JS = Javascript中的空数组。 如何获取空对象呢?

[英]Empty array in PHP -> JSON -> JS = empty array in Javascript. How to get empty Object instead?

So here is the basis for building my JSON response back to my JS. 因此,这是将JSON响应构建回JS的基础。

$this->_response['vendor'] = array();

foreach ($_results as $_row) {
    $this->_response['vendor'][$_row['id']] = $_row;
}

echo(json_encode($this->_response));

This is fine and builds objects in javascript fine, unless there were no results. 很好,除非没有结果,否则就可以使用javascript构建对象。 In that case, the php sees it as an empty numeric array, instead of an associative array. 在这种情况下,php将其视为空的数字数组,而不是关联数组。 This then comes down to javascript and converts to an empty array instead of an empty object. 然后将其归结为javascript并转换为空数组而不是空对象。

I know I can fix this in a number of ways by checking things, pre-declaring the variables as objects in javascript etc. What I'm wondering is if there is a way to declare an empty associative array in php, or some other way to force json_encode to create an object ("{}") instead. 我知道我可以通过检查事物,将变量预声明为javascript中的对象等多种方式来解决此问题。我想知道的是,是否有办法在php中声明一个空的关联数组,或者其他方法强制json_encode创建一个对象(“ {}”)。

It's a known limitation of PHP - since it doesn't support JavaScript types natively (eg. objects), decoding and then encoding again may lead to data loss. 这是PHP的一个已知限制-由于它本身不支持JavaScript类型(例如,对象),因此解码然后再次编码可能会导致数据丢失。

I use a workaround like this to keep JSON in tact: 我使用这样的变通方法来保持JSON不变:

/**
 * @param $input
 * @return array
 */
public static function safeDecode($input) {
    // Fix for PHP's issue with empty objects
    $input = preg_replace('/{\s*}/', "{\"EMPTY_OBJECT\":true}", $input);

    return json_decode($input, true);
}

/**
 * @param array|object $input
 * @return string
 */
public static function safeEncode($input) {
    return preg_replace('/{"EMPTY_OBJECT"\s*:\s*true}/', '{}', json_encode($input));
}

It's not ideal but it works. 这不是理想的方法,但是可行。 Note that you can: 请注意,您可以:

  • json_decode() to objects, which is default manner. json_decode()转换为对象,这是默认方式。 Then empty objects are not lost, but most modern frameworks REQUIRE arrays because most collection manipulation tools work with arrays. 这样,空对象就不会丢失,但是大多数现代框架都需要数组,因为大多数集合操作工具都可以使用数组。 Therefore, in most cases it's not a viable option; 因此,在大多数情况下,这不是一个可行的选择。
  • You could force json_encode() to encode empty arrays to objects but then you will lose empty arrays - not a nice option either. 您可以强制json_encode()将空数组编码为对象,但随后您将丢失空数组-也不是一个好选择。

Therefore, a work around like mine shown above is perhaps the only option at this moment. 因此,目前上面显示的解决方法可能是唯一的选择。 In short, you have to extend PHPs json_encode() and json_decode(). 简而言之,您必须扩展PHP的json_encode()和json_decode()。 Unfortunately PHP doesn't allow built-in function overloading so you have to define functions with different names. 不幸的是,PHP不允许内置函数重载,因此您必须使用不同的名称定义函数。

I suppose the most reasonable answer here is that there's probably no point in delivering an empty array or object in the first place. 我想这里最合理的答案是,最初交付空数组或对象可能没有意义。 Simple checks for existence in the javascript would then handle the problem. 简单检查javascript中是否存在即可解决该问题。

if (count($_results)) {

    $this->_response['vendor'] = array();

    foreach ($_results as $_row) {
        $this->_response['vendor'][$_row['id']] = $_row;
    }
}
echo(json_encode($this->_response));

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

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