【问题标题】:how to adjust my array output如何调整我的数组输出
【发布时间】:2018-05-23 20:03:53
【问题描述】:

哎呀……我就是听不懂…… 在线我可以正确设置: http://sandbox.onlinephpfunctions.com/code/492fa3050bf906b4a6c0631bdb66b7f066f23034 但在我的实时服务器上它不工作...... :(

需要帮助。

如果我的数组有单个项目,那么我的响应看起来像这样:

["20180517_xxxxxxxxxx-xxxxxxxxxxx~20180530.123456789.jpg"]

我没关系。

但如果我的数组有多个文件,则响应如下所示:

{"0":"20180517_xxxxxx-xxxxx~20180530.123456789.jpg","2":"20180519_xxxx-xxxxx~20180530.1.jpg"}

如何将我的回复调整为第一个?

["20180517_xxxxxx-xxxxxx~20180530.123456789.jpg","20180519_xxxxx-xxxxx~20180530.1.jpg"]

谢谢!

对不起...这是我的代码:

更多详情:I need to check if certain number is in string of numbers

<?php
header('Access-Control-Allow-Origin: *');
$imagesDir = '';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$find = 1;
//$filtered = preg_grep("/.*?\." . $find . "\./", $images);

$filteredImages = [];
foreach($images as $image) {
    $current_date = date("Ymd");
    $file_date = substr($image, 0, 8);
    if (strcmp($current_date, $file_date)>=0)
        $filteredImages[] = $image;
}
//$result = [];
$filtered = preg_grep("/.*?\.\d*" . $find . "\d*\./", $filteredImages);
//var_dump($filtered);
$result = $filtered;

echo json_encode($result, JSON_UNESCAPED_UNICODE);
?>

【问题讨论】:

  • 代码在代码中看起来如何?它与沙箱中的代码有何不同?将代码直接放在问题中可以极大地帮助我们帮助您解决问题。
  • 所以你没有读过数组?我告诉你阅读有关数组的内容。同样,这是同一件事。
  • @Andreas 是的,并且 iw 制作了一段可以在网上正常工作的代码……但在我的服务器上却不同。我在 PHP 方面不是那么强,所以我在这里寻求帮助... :(
  • 不过,您必须阅读 php.net 中的九个示例才能得到答案。那是什么时候? 15-20 分钟?

标签: php arrays


【解决方案1】:

如果索引是从0 开始的连续整数,json_encode() 只会使用 JSON 数组语法对数组进行编码。否则,它使用对象语法编码,索引作为显式键。

当您使用array_filter 时,保留的元素会保持与原始元素相同的索引。如果你从数组中过滤掉元素13,你会得到索引02,所以它被编码为一个对象。

您可以使用array_values() 获取具有顺序索引的数组:

echo json_encode(array_values($result), JSON_UNESCAPED_UNICODE));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-08
    • 2022-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    相关资源
    最近更新 更多