Questions tagged [array-flip]

A PHP function that exchanges all keys with their associated values in an array.

PHP array_flip() function exchanges all keys with their associated values in an array and returns an array in flip order. manual

Example

$input = array("oranges", "apples", "pears");
$flipped = array_flip($input);
print_r($flipped);

Result

Array
(
    [oranges] => 0
    [apples] => 1
    [pears] => 2
)
17 questions
32
votes
4 answers

array_unique vs array_flip

If I had an array of signed integers e.g: Array ( [0] => -3 [1] => 1 [2] => 2 [3] => 3 [4] => 3 ) To get unique values I would instinctively use array_unique but after consideration I could perform array_flip twice which would…
Lizard
  • 43,732
  • 39
  • 106
  • 167
4
votes
2 answers

PHP - Make an associative array unique, key -> value and value -> key

I have a little problem in php, which i find hard to explain in words. I have an associative array which contains key-value. I would like to make a function (or if there is already one) which would take an array as input and remove the duplicates…
StringerB
  • 75
  • 1
  • 5
4
votes
3 answers

Flip associative array and store new values in subarrays to prevent losing duplicated values

I have a flat associative array which may contain duplicate values. Array ( [for-juniors] => product_category [for-men] => product_category [coats] => product_category [for-women] => product_category [7-diamonds] => brand ) I…
Ahmed Fouad
  • 2,963
  • 10
  • 30
  • 54
3
votes
2 answers

PHP array_intersect + array_flip with array that has values multiple times

I have two arrays: $arr1 = array(101 => 250, 102 => 250, 103 => 250, 104 => 500, 105 => 500, 106 => 500,); and $arr2 = array(0 => 103, 1 => 104, 2 => 105) The result I want to get is Array (103 => 250, 104 => 500) I have tried working with…
Tris
  • 151
  • 1
  • 10
2
votes
1 answer

Flip Every Alternate Row in 2-D array- Python

I am a beginner to programming and was trying how to learn to flip every alternate row in my 2-D array(python) For Example: `Input a=[[1,2,3,4,5], [10,9,8,7,6], [11,12,13,14,15], [20,19,18,17,16], [21,22,23,24,25], …
2
votes
2 answers

Missing values from the same key after using array_flip function

I'm trying to flip array but it missed value from the key with the same name. What do I have to use, to add couple values to key which occur multiple times in array? For example, for [ "Input.txt" => "Randy", "Code.py" => "Stan", …
elBrambore
  • 110
  • 2
  • 8
1
vote
2 answers

equivalent of php's array_flip in flash actionscript 3?

Is there an equivalent of php's array_flip() in flash actionscript 3? Here's the definition for array_flip: array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys. If not, what is the…
John
  • 32,403
  • 80
  • 251
  • 422
1
vote
2 answers

PHP - Is it possible to flip only a specific part of a key value array?

Lets say I have the following array: $array_1 = [ 'Green' => 'Tea', 'Apple' => 'Juice', 'Black' => 'Coffee', ]; How do I flip the key => value of only one part of the array to something like this: …
1
vote
1 answer

Using array_flip With or Without a Value

Code used for processing form $_POST submissions is working well on most forms but suddenly broke on a new set of forms. I can't see any difference in the forms themselves as it's based purely on the posted values and I have it fixed but I am…
DonP
  • 725
  • 1
  • 8
  • 27
1
vote
2 answers

Dealing with array PHP

I am trying to sort the following arrays. Array(s) $fruits = array( 'mango', 'mango red', 'mango yellow', 'orange', 'banana', 'apple', 'apple red', 'apple green', ); What I have done: $data = array_flip( $fruits );…
sasa
  • 43
  • 5
0
votes
2 answers

Using for and array_flip to simulate multiple foreach. Is there a better way?

Is there a better way to do something like this? This is fine but I'd think there's probably a shortcut to get the same results. I guess to add more arrays easily I could just turn it into a function and have the for loop outside it but still. Just…
user1159454
  • 3,267
  • 3
  • 19
  • 25
0
votes
4 answers

Create flat, associative array from 2nd level values and 1st level keys of a 2d array

I'm looking for a way to replace nested foreach loops with a functional programming approach. Here is the sample data: $mode[1] = [1, 2, 5, 6]; $mode[0] = [3, 4, 7, 8]; Currently my code is this: foreach ($mode as $key => $value): foreach…
0
votes
1 answer

array_flip() expects parameter 1 to be array, null given issue

This is my code. $param = array('email'); $this->getMapper()->copyfrom('POST',function($val) { return array_intersect_key($val, array_flip($param)); }); And I get the error in title array_flip() expects parameter 1 to be array, null given…
sineverba
  • 5,059
  • 7
  • 39
  • 84
0
votes
2 answers

array_flip of a key value for letter and numbers

I have an array like this array(123=>'c', 125=>'b', 139=>'a', 124=>'c', 135=>'c', 159=>'b'); and I want to flip the key/values so that duplicate values become an index for an array. array( 'a'=>array(139), 'b'=>array(125, 159), …
tester2001
  • 1,053
  • 3
  • 14
  • 24
1
2