【问题标题】:如何从常量文件中获取值并将其存储在 php 中的数组中?
【发布时间】:2022-01-23 15:41:58
【问题描述】:

我有一个包含所有状态详细信息的常量文件,我想从常量文件中获取值并将其以索引数组的形式存储在一个数组中,你能给我一些想法如何做到这一点。 .

BooksConstants.php

class BooksConstants{

const PAID = 'settled';

const BOOK_FAILED_STATUSES = [
        self::cancelled_by_customer,
        self::FAILED,
        self::FAILED_BY_GATEWAY,
        self::INVALID_OTP
    ];
 const BOOK_SUCCESS_STATUSES = [
        self::PAID,
        self::SUCCESS,
        self::ON_THE_WAY,
        self::PROGRESS
    ];
}

Controller.php

$array=[];
array_push($array,BooksConstants::BOOK_SUCCESS_STATUSES);
array_push($array,BooksConstants::BOOK_FAILED_STATUSES);

它存储第 0 个索引,其中包含 BOOK_SUCCESS_STATUES 数组的所有数据,第一个索引存储 BOOK_FAILED_STATUES 但我的要求是

$array=['failed','settled','failed by gateway'....);

【问题讨论】:

  • 这样做是否足够:$array = array_merge(BooksConstants::BOOK_SUCCESS_STATUSES, BooksConstants::BOOK_FAILED_STATUSES); ?

标签: php arrays


【解决方案1】:

使用 array_push,您实际上会将一个数组推送到第 0 索引,然后再将另一个数组推送到第 1 索引。

如评论中所述,此处可以使用 array_merge() 代替,因为它只会将数组的内容彼此并排放置在返回的数组中。

$array = array_merge(BooksConstants::BOOK_SUCCESS_STATUSES, BooksConstants::BOOK_FAILED_STATUSES);

【讨论】:

    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2023-01-19
    • 2023-03-11
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多