简体   繁体   English

如何使用 PHP 从 json 数组中获取特定数量的项目

[英]How to get a specific number of items from a json array with PHP

I have a json document with the US states, in this form:我有一份美国各州的 json 文件,格式如下:

[
    {
        "name" : "Alabama",
        "abbreviation" : "AL"
    },
    {
        "name" : "Alaska",
        "abbreviation" : "AK"
    },
    {
        "name" : "Arizona",
        "abbreviation" : "AZ"
    },
    {
        "name" : "Arkansas",
        "abbreviation" : "AR"
    },
    {
        "name" : "California",
        "abbreviation" : "CA"
    },
    {
        "name" : "Colorado",
        "abbreviation" : "CO"
    },
    {
        "name" : "Connecticut",
        "abbreviation" : "CT"
    },
    {
        "name" : "Delaware",
        "abbreviation" : "DE"
    }
]

What I did is to decode the json document, then split the smaller arrays in their respective elements, by name and abbreviation.我所做的是解码 json 文档,然后按名称和缩写将较小的 arrays 拆分为各自的元素。 It echos the whole list of arrays properly.它正确地回显了 arrays 的整个列表。

$arrays = json_decode(file_get_contents('states.json'));
foreach ($arrays as $array) {
    $state_name = $array->{'name'};
    $state_abbreviation = $array->{'abbreviation'};
    echo '<p>' . $state_name . ' abbreviates ' . $state_abbreviation . '</p>';
}

But I need to be able to generate a specific number of random arrays from the big array which is the json file.但我需要能够从 json 文件的大数组中生成特定数量的随机 arrays。 Tried array_rand and there was no result.试过 array_rand 没有结果。 If the list has more than 50 items, from what I've read, mt_rand would be appropiate, because it might be faster and randomizes better.如果列表中的项目超过 50 个,根据我的阅读,mt_rand 将是合适的,因为它可能更快并且随机化更好。 Only I didn't find too much info on mt_rand, except for numeric arrays.只有我没有找到太多关于 mt_rand 的信息,除了数字 arrays。

How do I generate only 4, for example?例如,我如何只生成 4 个?

Thanks in advance!提前致谢!

And this is the solution with a small twist (slice factor), provided by @EinLinuus .这是@EinLinuus提供的带有小扭曲(切片因子)的解决方案。 Thanks, EinLinuus & Michel .谢谢,EinLinuus 和米歇尔

$arrays = json_decode(file_get_contents('states.json'));
shuffle($arrays);
$slice_factor = 4;
$arrays = array_slice($arrays, 0, $slice_factor);
foreach ($arrays as $array) {
    $state_name = $array->{'name'};
    $state_abbreviation = $array->{'abbreviation'};
    echo '<p>' . $state_name . ' abbreviates ' . $state_abbreviation . '</p>';
}

If you have your array stored in the variable $array , first shuffle the array with shuffle($array) .如果您将数组存储在变量$array中,请先使用shuffle($array)对数组进行随机排序。 Now you have a random array and you can get the first x elements with array_slice($array, 0, x) ;现在你有了一个随机数组,你可以用array_slice($array, 0, x)得到前x元素;

Example:例子:

$array = [0, 1, 2, 3, 4, 5, 6]; // Your array
shuffle($array); // Shuffle the array, it could be now [4, 1, 6, 2, 5, 0, 3]
$result = array_slice($array, 0, 3); // Get first 3 elements, in this case [4, 1, 6];

You can also use the array_rand function to get randomly selected elements from the array.No shuffling or slicing.您还可以使用array_rand function 从数组中随机选择元素。没有洗牌或切片。

$arr = range( 1, 20 );
$pick_random_items = 4;
$randoms = array_rand( $arr, $pick_random_items );

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

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