简体   繁体   中英

How to find and echo out a value in a php array

I have a foreach loop that loops through recent games and within that loop i would like to show data from each game. The array (or part of the array) i have to my disposal from each game is the following:

"array": [
{
    "statType": "TOTAL_TIME_SPENT_DEAD",
    "dataVersion": 0,
    "value": 636,
    "futureData": null
},
{
    "statType": "MAGIC_DAMAGE_DEALT_PLAYER",
    "dataVersion": 0,
    "value": 156407,
    "futureData": null
},
{
    "statType": "ITEM1",
    "dataVersion": 0,
    "value": 3040,
    "futureData": null
},
{
    "statType": "MINIONS_KILLED",
    "dataVersion": 0,
    "value": 186,
    "futureData": null
},

What i want is to echo out the value of a specific "statType". For an example i want the echo out the value of "MINIONS_KILLED" and ignore the rest. But the place of "MINIONS_KILLED" can be pretty much anywhere in the array so using array[4] won't work because it can be array[13] in a different game.

How can i solve this?

What i've been able to do up till now is to echo every statType and value by using the code below. The "$data" refers to the array pasted above. But it's quite a lot of data that is irrelevant which i rather ignore.

<ul>
    <?php foreach($data as $Property) { ?>

        <li><?php echo $data[$Property->statType] = $Property->statType; ?> : <?php echo $data[$Property->statType] = $Property->value; ?></li>

    <?php }; ?>
</ul>

Kind regards from a beginner on php.

function get_stat_index($data, $type) {
  for($i = 0; $i < count($data); $i++) {
    if($data[$i]->statType === $type) return $i;
  }
  return -1;
}

You can then use it like this:

$index = get_stat_index($data, 'MINIONS_KILLED');
echo $data[$index]->value;

index might be -1 if no such entry was found.

If you already know what you're going to echo , then it doesn't make sense to search the array for it. If you're trying to search the array and check if it exists in the array, then you can use the following function:

function array_multi_search($needle, $haystack)
{
    foreach ($haystack as $key => $data) {
        if(in_array($needle, $data, TRUE)) return true;
    }
    return false;
}

Can be used like below:

$exists = array_multi_search('MINIONS_KILLED', $data);
var_dump($exists); // bool(true)

If you're trying to find the sub-array that contains the search term, then you can use array_filter() with a custom callback function, like so:

$result = array_filter($data, function($item) {
   return ($item['statType'] == 'MINIONS_KILLED';
});
print_r($result);

This will output something like below:

Array
(
    [3] => Array
        (
            [statType] => MINIONS_KILLED
            [dataVersion] => 0
            [value] => 186
            [futureData] => 
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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