How to combine two arrays together? - php

Is there a quick way to combine one arrays values as the other array's keys?
Input:
array A => Array (
[0] => "cat"
[1] => "bat"
[2] => "hat"
[3] => "mat"
)
array B => Array (
[0] => "fur"
[1] => "ball"
[2] => "clothes"
[3] => "home"
)
Expected output:
array C => Array (
[cat] => "fur"
[bat] => "ball"
[hat] => "clothes"
[mat] => "home"
)
How could I do that?

array_combine() will exactly do what you want.
Quoting the manual:
array array_combine ( array $keys , array $values )
Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
In your case, you'd have to do something like this:
$array['C'] = array_combine($array['A'], $array['B']);
While of course you could also use various combinations of loops to do that, array_combine() is probably the simplest solution.

You can do this simply with array_combine:
// First parameter will be used as the keys, the second for the values
$new_array = array_combine($keys_array, $values_array);

Try this: array_combine($a, $b);

Related

How to get the array values from different arrays into a single using PHP?

I have an array-like:
Array
(
[0] => 4
[1] => 1861
)
Array
(
[0] => 4
[1] => 1938
)
Array
(
[0] => 4
[1] => 1452
)
Array
(
[0] => 4
[1] => 1938
)
Array
(
[0] => 21
)
This is a single array contain this array of elements.
I need the result like :
$arr = array(4,1861,1938,1452,21);
That means I need the only array unique values from these arrays. For this, I am using array_walk_recursive(), array_merge() etc. But I didn't get my results.
Merge all the arrays and then call array_unique().
$result = array_unique(array_merge(...$array));
...$array spreads the elements of the original array into arguments to array_merge().
If you're using an old version of PHP before ellipsis, use call_user_func_array() instead.
$result = array_unique(call_user_func_array('array_merge', $array));
you need to use array_merge then array_unique
$array = array_unique (array_merge ($array1, $array2));
check this please https://stackoverflow.com/a/10572576/2429434

How to find difference between two arrays in PHP?

Here is array 1:
Array ( [ABC01] => 10.123.456.78
[ABC02] => 10.123.456.79
[ABC03] => 10.123.456.80
[ZYX99] => 10.123.456.81
)
Here is array 2:
Array ( [0] => ABC01
[1] => ABC02
[2] => ABC03
)
I'm trying to find the difference between these two arrays and return the following (as you can see, the host name and then the corresponding ip address of an item not found in array 2):
Array ( [ZYX99] => 10.123.456.81)
I've been looking through the different PHP array functions and am overwhelmed by the amount of them: http://www.w3schools.com/php/php_ref_array.asp
This should work for you:
(Here I just used array_diff_key() to get the difference of the keys. The second array I flipped with array_flip() so to change the values to keys)
<?php
$arr1 = array(
"ABC01" => "10.123.456.78",
"ABC02" => "10.123.456.79",
"ABC03" => "10.123.456.80",
"ZYX99" => "10.123.456.81"
);
$arr2 = array("ABC01", "ABC02", "ABC03");
$result = array_diff_key ($arr1, array_flip($arr2));
print_r($result);
?>
Output:
Array ( [ZYX99] => 10.123.456.81 )

Convert an array of 2-element arrays to an array making 2 elements as key => value

Is it possible to convert the following array using some PHP built-in functions to a array that contain the value of id as key and the value of label as the associated value? If not what's the efficient way?
Thanks.
Input Array:
Array
(
[0] => Array
(
[id] => 2
[label] => MTD-589
)
[1] => Array
(
[id] => 3
[label] => MTD-789
)
)
Output Array:
Array
(
[2] => MTD-589,
[3] => MTD-789,
)
I don't know any built in function, but I'll do it this way:
assuming $originalArray as the array you want to convert
$newArray = array();
foreach ($originalArray as $element)
$newArray[$element["id"]] = $element["label"];
output the result
var_dump($newArray);
Introducing array_column (still in PHP 5.5 Beta).
$new_array = array_column($your_array 'label', 'id');
OUTPUT:
Array
(
[2] => MTD-589,
[3] => MTD-789,
)
Using array_walk.
array_walk($array, function($a) use (&$return) { $return[$a['id']] = $a['label']; });
print_r($return);
if you can make it so that one array has your IDs and one array has your labels, you can use array_combine to merge the two as key/value http://php.net/manual/en/function.array-combine.php

Add array to an array with the same indexes not being merged [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Combine Two Arrays with numerical keys without overwriting the old keys
OK guys, was searching about this one with no luck - it always points only to array_merge or array_push or array_combine functions which are useless for my purpose.
Here are two arrays (number indexed):
Array (
[0] => 12345
[1] => "asdvsdfsasdfsdf"
[2] => "sdgvsdfgsdfbsdf"
)
Array (
[0] => 25485
[1] => "tyjfhgdfsasdfsdf"
[2] => "mojsbnvgsdfbsdf"
)
and I need to create one "joined" (unioned) array, so it will look like:
Array (
[0] => 12345
[1] => "asdvsdfsasdfsdf"
[2] => "sdgvsdfgsdfbsdf"
[3] => 25485
[4] => "tyjfhgdfsasdfsdf"
[5] => "mojsbnvgsdfbsdf"
)
As I found nothing on this problem I tried by myself ($arr1 and $arr2 are the two small arrays):
$result_array = $arr1;
foreach($arr2 as $v) {
$result_array[] = $v;
}
This is, of course, working fine but I don't like this approach - imagine the situation when there will not be just 3 elements in second array...
Question: is there a better approach or at the best some built-in function (I do not know about)???
array_merge will work without any problem as your using numeric keys ... see the explanation below from the docs
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
emphasis mine
Array merge works fine for your numerically indexed arrays:
<?php
$arrayOne = array(
0 => 12345
,1 => "asdvsdfsasdfsdf"
,2 => "sdgvsdfgsdfbsdf"
);
$arrayTwo = array(
0 => 25485
,1 => "tyjfhgdfsasdfsdf"
,2 => "mojsbnvgsdfbsdf"
);
$arrayMerged = array_merge($arrayOne, $arrayTwo);
print_r($arrayMerged);
?>
output:
Array
(
[0] => 12345
[1] => asdvsdfsasdfsdf
[2] => sdgvsdfgsdfbsdf
[3] => 25485
[4] => tyjfhgdfsasdfsdf
[5] => mojsbnvgsdfbsdf
)

Order an array with arrays inside of it by the first arrays keys

So lets say I have an array that I want to organize by the keys. I thought I would just use ksort, but that does not work on the array below. Essentially I want to organize it so it would be A,R,Z. if I do ksort on this array it just returns 1.
array
(
[Z] => array
(
[dked] => asddadff
[fettyda] => dfdf
[feqed] => aasdf
)
[A] => array
(
[fdkded] => asddadff
[athgda] => dfdf
)
[R] => array
(
[fadfded] => asddadff
[adfthgda] => dfdf
[gadfhd] => aasdf
[gadfhd] => aasdf
)
)
if you are getting 1 as response, then you might be trying this
$array = ksort($array);
But ksort's return value is true or false, not the sorted array.
ksort($array);
print_r ($array);
This is enough. ksort receives the parameter as a reference, so you don't want to assign it back.
Read more here. ksort
sample working code:
<?php
$var = array('Z'=>array('dked'=>'asddadff','fettyda'=>'dfdf'),'A'=>array('fdkded'=>'asddadff','athgda'=>'dfdf'),'R'=>array('fadfded'=>'asddadff','adfthgda'=>'dfdf'));
ksort($var);
print_r($var);
?>
Your array declaration needs strings for the indices and values. Also notice the use of commas:
array
(
"Z" => array
(
"dked" => "asddadff",
"fettyda" => "dfdf",
"feqed" => "aasdf",
),
"A" => array
(
"fdkded" => "asddadff",
"athgda" => "dfdf",
),
"R" => array
(
"fadfded" => "asddadff",
"adfthgda" => "dfdf",
"gadfhd" => "aasdf",
"gadfhd" => "aasdf",
)
)
Use ksort with the above array.
Here's a good reference on array literal notation in PHP: http://php.net/manual/en/language.types.array.php

Categories