I have two arrays like this:
$arr1 = Array ( [1] => ab [2] => cd [at] => ef )
$arr2 = Array ( [1] => gh [2] => ij [at] => kl )
I want to combine these two arrays to this array:
$arr3 = Array ( [1] => ab-gh [2] => cd-ij [at] => ef-kl )
The keys are the same in the two arrays so I imagine that it is possible to join the values of the arrays with a "-" between like above.
I have googled and tried almost anything (merge, combine, join) but I can't figure it out. Any suggestions?
If you can guarantee these two arrays will always have the same keys:
$arr3 = array();
foreach ($arr1 as $key => $val1) {
$val2 = $arr2[$key];
$arr3[$key] = $val1 . "-" . $val2;
}
Now, assuming the two have different keys and you want to do a "joined" array with only the keys that exist in both arrays:
$arr3 = array();
foreach (array_intersect(array_keys($arr1), array_keys($arr2)) asĀ $key) {
$arr3[$key] = $arr1[$key] . "-" . $arr2[$key];
}
This is similar to a SQL inner join.
If you want to do a "joined" array with the keys in either of the arrays, hyphen-joining only the ones that exist in both:
$arr3 = array();
foreach (array_merge(array_keys($arr1), array_keys($arr2)) as $key) {
if (isset($arr1[$key]) && isset($arr2[$key])) {
$arr3[$key] = $arr1[$key] . "-" . $arr2[$key];
} else {
$arr3[$key] = (isset($arr1[$key]) ? $arr1[$key] : $arr2[$key]);
}
}
This is similar to a SQL outer join.
Try all of the above online!
Let's say you've got two arrays with the same amount of values and the same keys.
This should help:
$arr1 = Array ("ab", "cd", "at" => "ef");
$arr2 = Array ("gh", "ij", "at" => "kl");
$combinedArray = array();
function combineValues(array $array1, array $array2)
{
$array3 = array();
foreach($array1 as $key => $value)
{
$array3[$key] = $value . "-" . $array2[$key];
}
return $array3;
}
$combinedArray = combineValues($arr1, $arr2);
I really hope this helps.
Regards.
Although accepted answer is perfectly ok, here is a more general solution, that can be used for any number of arrays:
$arr1 = [1 => 'ab', 2 => 'cd', 'at' => 'ef'];
$arr2 = [1 => 'gh', 2 => 'ij', 'at' => 'kl'];
$arr3 = [1 => 'mn', 2 => 'op', 'at' => 'qr'];
$arr = [$arr1, $arr2, $arr3];
$keys = array_intersect(...array_map('array_keys', $arr));
$result = array_combine($keys, array_map(function ($key) use ($arr) {
return implode('-', array_column($arr, $key));
}, $keys));
Here is the demo.
Related
I have two arrays:
Array
(
[1] = 1575255
[2] = 1575258
)
Array
(
[Aantal kleuren opdruk] = Array
(
[1575252] = 1 kleur
[1575253] = 2 kleuren
[1575254] = 3 kleuren
[1575255] = 4 kleuren
)
[Opdrukpositie] = Array
(
[1575256] = Borst
[1575258] = Borst en rug
[1575257] = Rug
)
)
How can I compare the value of array 2 with the value of array 1 in their current order?
using array_column :
$arr = [];
foreach ($arr1 as $key => $value) {
$arr[] = array_column($arr2 ,$value);
}
print_r($arr);
assuming that the first array is $arr1 and the second is $arr2
EDIT
the one line solution using array_column and array_map
$arr = array_map(function($value) use ($arr2) {return array_column($arr2, $value);} ,$arr1);
print_r($arr);
OK so the first array has this. Take it as $a:
$a = ["1575255", "1575258"];
$second = ["Aantal kleuren opdruk" => [
"1575252" => "1 kleur",
"1575253" => "2 kleuren",
"1575254" => "3 kleuren",
"1575255" => "4 kleuren"
],
"Opdrukpositie" => [
"1575256" => "Borst",
"1575258" => "Borst en rug",
"1575257" => "Rug"
]
];
foreach($second as $val){
foreach($val as $key => $v){
if(in_array($key, $a)){
echo $v."<br>";
}
}
}
I have an bidimensional array like this (all the values are strings)
Array (
[0]=>Array([ENE] => 22 [FEB] => 24)
[1]=>Array( [MAR] => 16 [ABR] => 33 )
[2]=>Array([MAY] => 18 [JUN] => 19)
)
But I need to make that string values into integer.
I found the following answer that works just with simple arrays.
If you have array like:
$runners = ["1","2","3","4"];
And if you want to covert them into integers and keep within array, following should do the job:
$newArray = array_map( create_function('$value', 'return (int)$value;'),
$runners);
Do you have any idea about how to do the same but with bidimensional arrays?
You will have to loop the array and convert every value to in with like this
for($i=0;$i<count($array);$i++){
$elements = $array[$i];
$values = array_map(function($arrayElement) { return (int)$arrayElement; },$elements);
$array[$i] = $values;
}
$runners = array(
array('ENE' => "22", 'FEB' => "24"),
array('MAR' => "16", 'ABR' => "33"),
array('MAY' => "18", 'JUN' => "19")
);
$func = function($subarray){
$tempArray = array();
foreach($subarray as $key => $val)
{
$tempArray[$key] = (int)$val;
}
return $tempArray;
}
$new_array = array_map($func, $runners);
This should work fine.
How to merge multiple arrays from a single array variable ? lets say i have this in one array variable
Those are in one variable ..
$array = array(array(1), array(2));
Array
(
[0] => 1
)
Array
(
[0] => 2
)
how to end up with this
Array
(
[0] => 1
[1] => 2
)
This is the PHP equivalent of javascript Function#apply (generate an argument list from an array):
$result = call_user_func_array("array_merge", $input);
demo: http://3v4l.org/nKfjp
Since PHP 5.6 you can use variadics and argument unpacking.
$result = array_merge(...$input);
It's up to 3x times faster than call_user_func_array.
This may work:
$array1 = array("item1" => "orange", "item2" => "apple", "item3" => "grape");
$array2 = array("key1" => "peach", "key2" => "apple", "key3" => "plumb");
$array3 = array("val1" => "lemon");
$newArray = array_merge($array1, $array2, $array3);
foreach ($newArray as $key => $value) {
echo "$key - <strong>$value</strong> <br />";
}
array_merge can do the job
$array_meged = array_merge($a, $b);
after the comment
If fixed indexs you can use:
$array_meged = array_merge($a[0], $a[1]);
A more generic solution:
$array_meged=array();
foreach($a as $child){
$array_meged += $child;
}
$resultArray = array_merge ($array1, $array1);
$result = array();
foreach ($array1 as $subarray) {
$result = array_merge($result, $subarray);
}
// Here it is done
Something good to read:
http://ca2.php.net/manual/en/function.array-merge.php
Recursive:
http://ca2.php.net/manual/en/function.array-merge-recursive.php
$arr1 = array(0=>1);
$arr2 = array(0=>2);
$merged = array_merge($arr1,$arr2);
print_r($merged);
array_merge is what you need.
$arr = array_merge($arr1, $arr2);
Edit:
$arr = array_merge($arr1[0], $arr1[1]);
I want to find the first occurrence of any of several values in an array.
$sentence = array(I, want, to, go, to, the, market);
if(in_array(array('swim','fly','go'), $sentence)) {
// Should return KEY = 3 as 'go' was found in the third key of the array
}
I'm sure this must be fairly common, how can it be done?
http://tr.php.net/manual/en/function.array-keys.php
I think more specifically you are looking for this type of functionality?
<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
$array = array("color" => array("blue", "red", "green"),
"size" => array("small", "medium", "large"));
print_r(array_keys($array));
?>
The above example will output:
Array
(
[0] => 0
[1] => color
)
Array
(
[0] => 0
[1] => 3
[2] => 4
)
Array
(
[0] => color
[1] => size
)
first of all you should read manual properly of in_array function , you were passing parameters in wrong order
$sentence = array(I, want, to, go, to, the, market);
$found_key = array();
foreach($sentence as $key => $word)
{
if(in_array($word,array('swim','fly','go')))
{ $found_keys[] = $key;}
}
print_r($found_keys);
I hope this is what you need
Please use this function array_search it will return key of the element if element found in the array
Example
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
array_keys would work a treat.
$keys = array_keys($sentence,array('swim','fly','go');
Then $keys will be a list of keys where swim, fly and go appear in $sentence
Just as the code below will return a list of keys that have empty values
$array = array('one','','three');
$keys = array_keys($array,"");
function find_in_array($needles,$array) {
$pos = false;
foreach ($needles as $key => $value) {
if (in_array($value, $array)) {
if(!$pos || array_search($value, $array) < $pos) {
$pos = array_search($value, $array);
}
}
}
return $pos;
}
echo find_in_array($values,$sentence);
This is what I am using right now, I'm testing array_keys as suggested by Darian Brown
I have a few associative arrays that I need to merge together based on there key.
so:
array1:
[person1] => tony
[person2] => sandra
array2:
[person1] => london
[person2] => paris
needs to be :
array 3
[person1] => tony , london
[person2] => sandra , paris
The issue I'm having though is that the key could be any value , so it could be 'person1' or it could be 'hairyOtter' and the array is of varaible size.
Assuming, that every is not multi-dimensional
$merged = array_merge_recursive($array1, $array2);
foreach ($merged as &$entry) {
if (is_array($entry)) {
$entry = implode(', ', $entry);
}
}
The idea is, that array_merge_recursive() creates a new array, if it find two values with the same key. Everything else stays untouched.
I'm sure there are more efficient ways to accomplish this, but for now, this works:
<?php
function combine( $keys, $first, $second ) {
$args = func_get_args( );
$keys = array_shift( $args );
$arrays = $args;
$result = array( );
foreach( $keys as $key ) {
foreach( $arrays as $array ) {
if( isset( $array[$key] ) ) {
$result[$key][] = $array[$key];
}
}
}
return $result;
}
$first = array(
'person1' => 'tony',
'person2' => 'sandra'
);
$second = array(
'person1' => 'london',
'person2' => 'paris'
);
/**
* To make sure you get *every* key out of both arrays.
*/
$keys = array_unique( array_merge( array_keys( $first ), array_keys( $second ) ) );
$combined = combine( $keys, $first, $second );
var_dump( $combined );
Two loops should do it. Iterate over each of array1 and array2, initialising array3's keys as you go to an array, and pushing into that array. Here's the first loop
$array3 = array();
foreach (array_keys($array1) as $key) {
if(!array_key_exists($key, $array3)) {
$array3[$key] = array();
}
array_push($array3[$key], $array1[$key]);
}
$array1 = array('a' => 1, 'b' => 2);
$array2 = array('a' => 3, 'b' => 4, 'c' => 5);
foreach ($array1 as $k => $v) {
$tArray[$k][] = $v;
}
foreach ($array2 as $k => $v) {
$tArray[$k][] = $v;
}
foreach ($tArray as $k => $v) {
$tArray[$k] = implode(',',$tArray[$k]);
}
I would create a multi-dimensional array instead, unless there is a specific reason you can't use one. This way, you would have an array that looks like:
Array
(
[Person1] => Array
(
[Name] => Tony
[City] => London
)
[Person2] => Array
(
[Name] => Sandra
[City] => Paris
)
)