php - Best way to use a large array in function

I need to define an array which has more than 15000 values in the wordpress functions file: $validpin = array(110019,111

I need to define an array which has more than 15000 values in the wordpress functions file:

$validpin = array(110019,111222,112233);

I have written only 3 values to explain but the actual array will have more than 15000 values. How should I go about doing, as writing 15000 values is making the functions file very large?

I need to define an array which has more than 15000 values in the wordpress functions file:

$validpin = array(110019,111222,112233);

I have written only 3 values to explain but the actual array will have more than 15000 values. How should I go about doing, as writing 15000 values is making the functions file very large?

Share Improve this question asked Jun 15, 2019 at 7:03 dc09dc09 1952 silver badges14 bronze badges 2
  • What are you going to do with the values? Your variable name suggest it's "valid pins", but surely there's a rule you could implement, rather than just listing 15000 values. – Jacob Peattie Commented Jun 15, 2019 at 7:11
  • The values are postcodes on which I want to restrict certain payment methods, there's no pattern as such as post codes are random. I would need to list post codes individually, need the bast possible way to do so. – dc09 Commented Jun 15, 2019 at 7:14
Add a comment  | 

1 Answer 1

Reset to default 2

If it's just a list of numbers, I'd suggest just saving them in a text file, with a number on each line.

110019
111222
112233

Then when you need the file, read its contents with PHP, and use preg_split() to turn it into an array:

$file_path     = plugin_dir_path( 'postcodes.txt', __FILE__ ); // Or wherever you've placed it.
$file_contents = file_get_contents( $file_path );
$postcodes     = preg_split( "/\r\n|\n|\r/", $file_contents );

if ( in_array( $postcode, $postcodes ) ) {

}

That preg_split() method for splitting text based on newlines is taken from here.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745399782a4626047.html

相关推荐

  • php - Best way to use a large array in function

    I need to define an array which has more than 15000 values in the wordpress functions file: $validpin = array(110019,111

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信