php values of one array to key of another array [duplicate] - php

This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 10 months ago.
I have 2 arrays
$arr1 = Array
(
[0] => 12
[1] => 4
[2] => 8
[3] => xx
[4] => 1
[5] => 1year
[6] => 7
)
$arr2 = Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)
I want to create a new array with the values of a2 as keys in $arr1.
My resultant array should be like this
$arr3 = Array
(
[1] => 12
[2] => 4
[3] => 8
[4] => xx
[5] => 1
[6] => 1year
[7] => 7
)

$arr3 = array_combine($arr2, $arr1);
print_r($arr3);
Next time please consult the manual first.

You need array_combine.

whats about looking into php manual? and find array_combine($keys, $values)

Related

How to combine or merge multiple values inside the single array? [duplicate]

This question already has answers here:
How to remove duplicate values from an array in PHP
(26 answers)
Closed 3 years ago.
I have an array values. Inside the array some multiple data but how to combine or merge these multiple records.
some values inside the single array are multiples . How to merge these ?
Array
(
[0] => plxliupp
[1] => plxliupp
[2] => dg_w12
[3] => col4312
[4] => Iphone6s
[5] => Ndry_milk
[6] => smj7
[7] => ovn 02 o3
[8] => allwin6406f
)
After remove multiples values. array like this
Array
(
[0] => plxliupp
[1] => dg_w12
[2] => col4312
[3] => Iphone6s
[4] => Ndry_milk
[5] => smj7
[6] => ovn 02 o3
[7] => allwin6406f
)
thanks for answering
$array = array("a" => "moon", "star", "b" => "moon", "star", "sky");
// Deleting the duplicate items
$result = array_unique($array);
print_r($result);

How to combine two arrays like this?

$blueswards array:
Array ( [0] => 3 [1] => 8 [2] => 1 [3] => 4 [4] => 9 )
$redswards array:
Array ( [0] => 2 [1] => 9 [2] => 3 [3] => 6 [4] => 9 )
What i try to do:
Array ( [0] => 3 [1] => 8 [2] => 1 [3] => 4 [4] => 9 [5] => 2 [6] => 9 [7] => 3 [8] => 6 [9] => 9 )
I cant do it with array_merge.
EDIT = Sorry everyone i did it with array merge now its working. :[
I used array_merge() using your array values. And, it worked.
<?php
$new_array[] = array();
$blueswards = array(3,8,4,9);
$redswards = array(2,9,3,6,9);
$new_array = array_merge($blueswards,$redswards);
print_r($new_array);
?>
OUTPUT:
Array ( [0] => 3 [1] => 8 [2] => 4 [3] => 9 [4] => 2 [5] => 9 [6] => 3 [7] => 6 [8] => 9 )
Why don't you just run them in a loop once they're populated.
$newarray=0;
$arrayCounter=0;
for (i=0; i<count($redswards)-1 ; i++){
$newarray[$arrayCounter] = $redswards[i];
$arrayCounter++
}
for (i=0; i<count($blueswards)-1 ; i++){
$newarray[$arrayCounter] = $blueswards[i];
$arrayCounter++
}
There... That will do it correctly by simply adding on to the new array. It will also count the number of times it has to loop so you don't have to hardcode that.
I mean, that's really simple, and you may have to change the hardcoded i values. But that's the basic idea.
Used durbnpolsns code and edited it to work.
$newarray=0;
for ($i=0; $i<4 ; $i++){
$newarray[$i] = $redswards[$i];
}
$j=0;
for ($i=5; $i<8 ; $i++){
$newarray[$i] = $blueswards[$j];
$j++;
}
I have not tested the code and formatting may be impossible, but I'm typing on my phone.
Sorry for that.

php move keys from array into another array

So I have this array:
[0] => 3
[1] => 9
[2] => 4
[3] => 6
[4] => 69
[5] => 8
[6] => 9
[7] => 12
[8] => 9
[9] => 7
And this one
[Far] => 1
[far] => 3
[away] => 1
[behind] => 1
[the] => 23
[word] => 2
[mountains] => 1
[from] => 3
[countries] => 1
[Vokalia] => 1
I would like that the values of the first array will overwrite the values of the second array without changing the keys of the second array.
I have already tried fiddling with the foreach function, but no prevail.
So in the end I would like it to look like this:
[Far] => 3
[far] => 9
[away] => 4
[behind] => 6
[the] => 69
[word] => 8
[mountains] => 9
[from] => 12
[countries] => 9
[Vokalia] => 7
does anyone know how to do that? And if yes, can that person give a bit more information how it works in the foreach function?
Assuming your arrays are $array1 and $array2:
$keys = array_keys($array2);
$result = array_combine($keys, $array1);
Documentation:
array_keys()
array_combine()
Online demo

How to get only duplicate values out of all keys of an associative array

i want to extract only duplicates out of all keys of an associative array..
The array structure is:
Array
(
[bank_users] => Array
(
[0] => 8
[1] => 8
[2] => 8
[3] => 28
)
[bank_link] => Array
(
[0] => 8
[1] => 8
[2] => 8
[3] => 28
[4] => 28
[5] => 28
[6] => 28
[7] => 28
[8] => 73
[9] => 73
[10] => 73
)
[banks] => Array
(
[0] => 8
[1] => 28
)
)
Now out of this array, i want a function that should check in each of the key and give me the duplicates.. Like as per the above example 8 and 28 should come out as a result as these values are available in all three keys.. bank_users, bank_link, banks.
Pls help...
You need array_intersect to get the common elements in each array and array_unique to reduce them to just one each.
Try
$result = array_unique(
array_intersect($arr['bank_users'], $arr['bank_link'], $arr['banks'])
);
array_unique — Removes duplicate values from an array

PHP - count frequency of array values

Is there a way in php to count how often a value exists in a large array?
So if I have an array like this:
$array = "1,2,3,4,joe,1,2,3,joe,joe,4,5,1,6,7,8,9,joe";
is there a way to output a new array that tells me (and sorts) which is used most and how many for each?
$result = array(
[joe] => 4
[1] => 3
[2] =>2
etc...
)
I've seen the php array_count_values, but can this be sorted by most -> least? or is there an easier way?
Thanks everyone!
Sort them after counting them with arsort()
$result = array_count_values(explode(',', $array));
arsort($result);
Array
(
[joe] => 4
[1] => 3
[2] => 2
[4] => 2
[3] => 2
[9] => 1
[8] => 1
[5] => 1
[6] => 1
[7] => 1
)

Categories