PHP.mk документација

array_flip

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.array-flip.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.array-flip.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
array_flip

Референца за `function.array-flip.php` со подобрена типографија и навигација.

function.array-flip.php

array_flip

(PHP 4, PHP 5, PHP 7, PHP 8)

array_flipГи разменува сите клучеви со нивните соодветни вредности во низа

= NULL

array_flip(array $array): array

array_flip() враќа array во обратен редослед, т.е. клучевите од array стануваат вредности и вредностите од array стануваат клучеви.

Забележете дека вредностите на array треба да бидат валидни клучеви, т.е. треба да бидат или int or string. Ќе се издаде предупредување ако вредноста има погрешен тип, а парот клуч/вредност што е предмет на прашање нема да биде вклучен во резултатот.

Ако една вредност има повеќе појавувања, последниот клуч ќе се користи како нејзина вредност, а сите други ќе бидат изгубени.

Параметри

array

Низа од парови клуч/вредност што треба да се превртат.

Вратени вредности

Враќа превртена низа.

Примери

Пример #1 array_flip() example

<?php
$input
= array("oranges", "apples", "pears");
$flipped = array_flip($input);

print_r($flipped);
?>

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

Array
(
    [oranges] => 0
    [apples] => 1
    [pears] => 2
)

Пример #2 array_flip() пример : судир

<?php
$input
= array("a" => 1, "b" => 1, "c" => 2);
$flipped = array_flip($input);

print_r($flipped);
?>

Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред

Array
(
    [1] => b
    [2] => c
)

Види Исто така

  • array_values() - Врати ги сите вредности од низа
  • array_keys() - Враќање на сите клучеви или подмножество од клучевите на низа
  • array_reverse() - Врати низа со елементи во обратен редослед

Белешки од корисници 16 белешки

Финално
пред 14 години
I find this function vey useful when you have a big array and you want to know if a given value is in the array. in_array in fact becomes quite slow in such a case, but you can flip the big array and then use isset to obtain the same result in a much faster way.
Тони Х
12 години пред
This function is useful when parsing a CSV file with a heading column, but the columns might vary in order or presence:

<?php

$f = fopen("file.csv", "r");

/* Take the first line (the header) into an array, then flip it
so that the keys are the column name, and values are the
column index. */
$cols = array_flip(fgetcsv($f));

while ($line = fgetcsv($f))
{
    // Now we can reference CSV columns like so:
    $status = $line[$cols['OrderStatus']];
}

?>

I find this better than referencing the numerical array index.
Боб Реј
пред 9 години
array_flip will remove duplicate values in the original array when you flip either an associative or numeric array. As you might expect it's the earlier of two duplicates that is lost:

<?php
    $a = array('one', 'two', 'one'); 
    print_r($a); 

    $b = array_flip($a); 
    print_r($b);
?>

Result:

array(3) {
  [0]  => string(3) "one"
  [1]  =>  string(3) "two"
  [2]  =>  string(3) "one"
}

array(2) {
  'one' => int(2)
  'two' => int(1)
}

This may be good or bad, depending on what you want, but no error is thrown.
Прабас Гупте
пред 11 години
array_flip() does not retain the data type of values, when converting them into keys. :( 

<?php                                                                                                                                                                                                           
$arr = array('one' => '1', 'two' => '2', 'three' => '3');
var_dump($arr);
$arr2 = array_flip($arr);
var_dump($arr2);
?>

This code outputs this:
array(3) {
  ["one"]=>
  string(1) "1"
  ["two"]=>
  string(1) "2"
  ["three"]=>
  string(1) "3"
}
array(3) {
  [1]=>
  string(3) "one"
  [2]=>
  string(3) "two"
  [3]=>
  string(5) "three"
}

It is valid expectation that string values "1", "2" and "3" would become string keys "1", "2" and "3".
pinkgothic на gmail точка com
пред 18 години
In case anyone is wondering how array_flip() treats empty arrays:

<?php
print_r(array_flip(array()));
?>

results in:

Array
(
)

I wanted to know if it would return false and/or even chuck out an error if there were no key-value pairs to flip, despite being non-intuitive if that were the case. But (of course) everything works as expected. Just a head's up for the paranoid.
kjensen на iaff106 dot com
пред 14 години
I needed a way to flip a multidimensional array and came up with this function to accomplish the task.  I hope it helps someone else.

<?php
function multi_array_flip($arrayIn, $DesiredKey, $DesiredKey2=false, $OrigKeyName=false) { 
$ArrayOut=array();
foreach ($arrayIn as $Key=>$Value) 
    {
        // If there is an original key that need to be preserved as data in the new array then do that if requested ($OrigKeyName=true)
        if ($OrigKeyName) $Value[$OrigKeyName]=$Key;
        // Require a string value in the data part of the array that is keyed to $DesiredKey
        if (!is_string($Value[$DesiredKey])) return false;

        // If $DesiredKey2 was specified then assume a multidimensional array is desired and build it
        if (is_string($DesiredKey2)) 
        {
            // Require a string value in the data part of the array that is keyed to $DesiredKey2
            if (!is_string($Value[$DesiredKey2])) return false;

            // Build NEW multidimensional array
            $ArrayOut[$Value[$DesiredKey]][$Value[$DesiredKey2]]=$Value;
        }

            // Build NEW single dimention array
        else $ArrayOut[$Value[$DesiredKey]][]=$Value;
    }
return $ArrayOut;
}//end multi_array_flip
?>
snaury на narod dot ru
21 години пред
When you do array_flip, it takes the last key accurence for each value, but be aware that keys order in flipped array will be in the order, values were first seen in original array. For example, array:

    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 3
    [5] => 2
    [6] => 1
    [7] => 1
    [8] => 3
    [9] => 3

After flipping will become:
(first seen value -> first key)

    [1] => 7
    [2] => 5
    [3] => 9

And not anything like this:
(last seen value -> last key)

    [2] => 5
    [1] => 7
    [3] => 9

In my application I needed to find five most recently commented entries. I had a sorted comment-id => entry-id array, and what popped in my mind is just do array_flip($array), and I thought I now would have last five entries in the array as most recently commented entry => comment pairs. In fact it wasn't (see above, as it is the order of values used). To achieve what I need I came up with the following (in case someone will need to do something like that):

First, we need a way to flip an array, taking the first encountered key for each of values in array. You can do it with:

  $array = array_flip(array_unique($array));

Well, and to achieve that "last comments" effect, just do:

  $array = array_reverse($array, true);
  $array = array_flip(array_unique($array));
  $array = array_reverse($array, true);

In the example from the very beginning array will become:

    [2] => 5
    [1] => 7
    [3] => 9

Just what I (and maybe you?) need. =^_^=
Хејли Вотсон
пред 8 години
Don't use this function for filtering or searching an array - PHP already has functions for exactly those purposes. If nothing else, array_flip will trash the array's elements if they're anything other than integers or non-decimal-integer strings.
Анонимен
пред 13 години
Similarly, if you want the last value without affecting the pointer, you can do:

<?php

$array = array("one","two","three");

echo next($array); // "two"

$last = array_pop(array_keys(array_flip($array)));

echo $last;  // "three"

echo current($array); // "two"

?>
dash
пред 7 години
Notice : array_flip can turn string into integer
Хејли Вотсон
пред 17 години
Finding the longest string in an array?

<?php
function longest_string_in_array($array)
{
    $mapping = array_combine($array, array_map('strlen', $array));
    return array_keys($mapping, max($mapping));
}
?>

Differences are obvious: returns an array of [i]all[/i] of the longest strings, instead of just picking one arbitrarily. Doesn't do the stripslashing or magic stuff because that's another job for for another function.
dan на aoindustries dot com
пред 17 години
From an algorithmic efficiency standpoint, building an entire array of lengths to then sort to only retrieve the longest value is unnecessary work.  The following should be O(n) instead of O(n log n).  It could also be:

<?php
function get_longest_value($array) {
    // Some don't like to initialize, I do
    $longest = NULL;
    $longestLen = -1;
    foreach ($array $value) {
        $len = strlen($value);
        if($len>$longestLen) {
            $longest = $value;
            $longestLen = $len;
        }
    }
    $longest = str_replace("\r\n", "\n", $longest);
    if (get_magic_quotes_gpc()) { return stripslashes($longest); }
    return $longest;
}
?>
Ахамар Јасин
пред 7 години
<?php

$arr = array('one' => ['four' => 4], 'two' => '2', 'three' => '3');
var_dump($arr);

$arr2 = array_flip($arr);
var_dump($arr2);

?>

The above example will output:

array(3) {
  ["one"]=>
  array(1) {
    ["four"]=>
    int(4)
  }
  ["two"]=>
  string(1) "2"
  ["three"]=>
  string(1) "3"
}

Warning: array_flip(): Can only flip STRING and INTEGER values! in /root/test.php on line 4
array(2) {
  [2]=>
  string(3) "two"
  [3]=>
  string(5) "three"
}
grimdestripador на hotmail точка ком
12 години пред
<?php
function array_flip_into_subarray($input){
$output = array();
foreach ($input as $key=>$values){
    foreach ($values as $value){
        $output[$value][] = $key;
    }
}
return $output;
}
mmulej на gmail точка com
пред 4 години
If you don't want to lose duplicates, and you're ok, with having the values in the flipped array in an array as well, you may use this:

PHP 7.4 - ^8

<?php

function array_flip_safe(array $array) : array
{
    return array_reduce(array_keys($array), function ($carry, $key) use (&$array) {
        $carry[$array[$key]] ??= [];
        $carry[$array[$key]][] = $key;
        return $carry;
    }, []);
}

?>

PHP 7.0 - ^7.3 (Time to upgrade to PHP 8 ^^)

<?php

function array_flip_safe(array $array) : array
{
    return array_reduce(array_keys($array), function ($carry, $key) use (&$array) {
        $carry[$array[$key]] = $carry[$array[$key]] ?? [];
        $carry[$array[$key]][] = $key;
        return $carry;
    }, []);
}

?>

PHP 5.4 - ^5.6 (Just don't)

<?php

function array_flip_safe(array $array)
{
    return array_reduce(array_keys($array), function ($carry, $key) use (&$array) {
        if (!isset($carry[$array[$key]])
                $carry[$array[$key]] = [];
        $carry[$array[$key]][] = $key;
        return $carry;
    }, []);
}

?>
info на sabastore точка нет
пред 9 години
note :: array_flip is a changer for key and value and a auto unique like array_unique :

<?php
/*
sabastore
 */
$intArray1 = array(-4,1,1,3);
print_r($intArray1);
$intArray1 = array_flip($intArray1);
print_r($intArray1);
?>
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.