以下是dlang语言实现php的array_flip函数的代码:

import std.stdio;

/**

  • Flips the keys and values of an associative array.

  • @param arr The array to flip.

  • @return The flipped array. */ auto array_flip(T, U)(in T[U] arr) { auto flipped = std.container.associativeArray!(U, T)();

    foreach(key, value; arr) { if(value in flipped) { // If the value already exists in the flipped array, // overwrite the previous key with the new key. flipped[value] = key; } else { flipped[value] = key; } }

    return flipped; }

void main() { string[] input = ["oranges", "apples", "pears"]; auto flipped = array_flip(input);

writeln(flipped);
dlang语言实现php的array_flip函数最好支持关联数组功能描述如下:array_fliparray $array arrayarray_flip returns an array in flip order ie keys from array become values and values from array become keysNote that the values of

原文地址: https://www.cveoy.top/t/topic/fyHA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录