I have two arrays:
$arr1 = array("123" => "abc");
$arr2 = array("123" => "xyz", "456" => "lmn");
I want the resultant array to be:
$arr = array("123" => "abc,xyz", "456" => "lmn");
I know I can write some code to fetch the values corresponding to keys and then concat with a separator like ';' or ',', but I want to know is there any efficient way to do this?
An in-built function maybe?
Simple foreach will do! Check inline comments
$arr1 = ["123" => "abc"];
$arr2 = ["123" => "xyz", "456" => "lmn"];
foreach ($arr2 as $key => $value) {
if(array_key_exists($key, $arr1)) // Check if key exists in array
$arr1[$key] .= ",$value"; // If so, append
else
$arr1[$key] = $value; // otherwise, add
}
print_r($arr1);
Prints
Array
(
[123] => abc,xyz
[456] => lmn
)
Check this Eval
try this:
$arr1 = array("123" => "abc");
$arr2 = array("123" => "xyz", "456" => "lmn");
$o = [];
foreach($arr1 as $k => $v)
{
$o[$k][] = $v;
}
foreach($arr2 as $k => $v)
{
$o[$k][] = $v;
}
$result = array_map(function($v){implode(',', array_unique($v));}, $o);
Related
I want to implode values in to a comma-separated string if they are an array:
I have the following array:
$my_array = [
"keywords" => "test",
"locationId" => [ 0 => "1", 1 => "2"],
"industries" => "1"
];
To achieve this I have the following code:
foreach ($my_array as &$value)
is_array($value) ? $value = implode(",", $value) : $value;
unset($value);
The above will also change the original array. Is there a more elegant way to create a new array that does the same as the above?
I mean, implode values if they are an array in a single line of code? perhaps array_map()? ...but then I would have to create another function.
Just create a new array and set the elements (-;
<?php
...
$new_array = [];
foreach ($my_array as $key => $value)
$new_array[$key] = is_array($value) ? implode(",", $value) : $value;
Just append values to new array:
$my_array = [
"keywords" => "test",
"locationId" => [ 0 => "1", 1 => "2"],
"industries" => "1",
];
$new_Array = [];
foreach ($my_array as $value) {
$new_Array[] = is_array($value) ? implode(",", $value) : $value;
}
print_r($new_Array);
And something that can be called a "one-liner"
$new_Array = array_reduce($my_array, function($t, $v) { $t[] = is_array($v) ? implode(",", $v) : $v; return $t; }, []);
Now compare both solutions and tell which is more readable.
You don't need to write/iterate a conditional statement if you type the strings (non-arrays) as single-element arrays before imploding them.
With array_map(): (Demo)
$my_array = [
"keywords" => "test",
"locationId" => [ 0 => "1", 1 => "2"],
"industries" => "1"
];
var_export(
array_map(
function($v) {
return implode(',', (array)$v);
},
$my_array
)
);
Or from PHP7.4, array_map() with arrow function syntax: (Demo)
var_export(
array_map(fn($v) => implode(',', (array)$v), $my_array)
);
Or array_walk() and modification by reference (Demo)
array_walk(
$my_array,
function(&$v) {
$v = implode(',', (array)$v);
}
);
var_export($my_array);
Or a foreach loop: (Demo)
foreach ($my_array as &$v) {
$v = implode(',', (array)$v);
}
var_export($my_array);
All snippets will output:
array (
'keywords' => 'test',
'locationId' => '1,2',
'industries' => '1',
)
$arr = array( 'key1' => array( 'one', 'key2' => array( 'two',
'three' ) ), 'four', 'five' );
$finalData = [];
array_walk_recursive(
$arr,
function( $subArr ) use ( &$finalData ) {
$finalData[] = $subArr;
}
);
$str = is_array( $finalData ) ? implode( ",", $finalData ) : $finalData;
print_r( $str ); // one,two,three,four,five
I have two array which i want to merge in a specific way in php.
So i need your help in helping me with it as i tried and failed.
So say i have two arrays:
$array1= array(
"foo" => 3,
"bar" => 2,
"random1" => 4,
);
$array2= array(
"random2" => 3,
"random3" => 4,
"foo" => 6,
);
Now when during merging i would like the common key's values to be added.
So like foo exists in array1 and in array2 so when merging array1 with array 2 i should get "foo" => "9"
I better illustration would be the final array which looks like this:
$array1= array(
"foo" => 9,
"bar" => 2,
"random1" => 4,
"random2" => 3,
"random3" => 4,
);
So again i would like the values of the common keys to be added together and non common keys to be added to array or a new array
I hope i was clear enough
Thanks,
Vidhu
Something like that:
function mergeValues() {
$result = array();
$arraysToMerge = func_get_args();
foreach ($arraysToMerge as $array) {
foreach($array as $key => $value) {
$result[$key] += $value;
}
}
return $result;
}
$res = mergeValues($array1, $array2, $array3); // Can pass any ammount of arrays to a function.
foreach($array1 as $k => $v)
{
If (isset($array2[$k]))
$array1[$k] += $array2[$k];
}
foreach($array2 as $k => $v)
{
If (!isset($array1[$k]))
$array1[$k] = $array2[$k];
}
I have an array that may look like
$arr = array(
array(
'test1' => 'testing1'
),
array(
'test2' => array(
1 =>'testing2
)
);
and I want to turn it into
$newArr = array(
'test1' => 'testing1',
'test2' => array(
1 => 'testing2'
)
);
so i have been trying to shift all array elements up one level.
eidt:
this is my method that combines 2 array together:
public function arrayMerge($arr1, $arr2)
{
foreach($arr2 as $key => $value) {
$condition = (array_key_exists($key, $arr1) && is_array($value));
$arr1[$key] = ($condition ? $this->arrayMerge($arr1[$key], $arr2[$key]) : $value);
}
return $arr1;
}
It's somewhat trivial, many ways are possible.
For example using the array union operator (+)Docs creating the union of all arrays inside the array:
$newArr = array();
foreach ($arr as $subarray)
$newArr += $subarray;
Or by using array_mergeDocs with all subarrays at once via call_user_func_arrayDocs:
$newArray = call_user_func_array('array_merge', $arr);
Try
$arr = array(
array('test1' => 'testing1' ),
array('test2' => array(1 =>'testing2'))
);
$new = array();
foreach($arr as $value) {
$new += $value;
}
var_dump($new);
Output
array
'test1' => string 'testing1' (length=8)
'test2' =>
array
1 => string 'testing2' (length=8)
I'm trying to merge these 2 arrays
$arr1 = array('a' => "1", 'b' => "2", 'c' => "3");
$arr2 = array('a' => "9", 'b' => "8", 'd' => "7");
into an array that looks like this
$arr1 = array(
'a' => array("1", "9"),
'b' => array("2", "8"),
'c' => array("3", ""),
'd' => array("", "7")
);
The tricky part is the blanks. I need to preserve them in place.
Thanks
function merge()
{
$array_of_arrays = func_get_args();
//get all the unique keys
$final_array_keys = array_keys( call_user_func_array( "array_merge", $array_of_arrays ) );
//make final array
$final_array = array();
foreach( $final_array_keys as $key ) {
foreach( $array_of_arrays as $current_array ) {
$final_array[$key][] = array_key_exists( $key, $current_array ) ? $current_array[$key] : "";
}
}
return $final_array;
}
Try this:
$arr1 = array('a' => "1", 'b' => "2", 'c' => "3");
$arr2 = array('a' => "9", 'b' => "8", 'd' => "7");
$keys = array();
$merged = array()
for($arr1 as $key=>$val)
{
array_push($keys,$key);
}
for($arr2 as $key=>$val)
{
array_push($keys,$key);
}
for($key in keys)
{
$merged[$key] = array("","");
if(isset($arr1[$key])) $merged[$key][0] = $arr1[$key];
if(isset($arr2[$key])) $merged[$key][1] = $arr2[$key];
}
foreach (array_merge($arr1, $arr2) as $key => $val)
{
$result[$key] = array("{$arr1[$key]}", "{$arr2[$key]}");
}
var_dump($result);
here's my suggestion. It'll combine an arbitrary number of arrays according to what you described.
error_reporting(E_ALL | E_STRICT);
header('Content-Type: text/plain');
$arr1 = array('a' => "1", 'b' => "2", 'c' => "3");
$arr2 = array('a' => "9", 'b' => "8", 'd' => "7");
$arr = combine($arr1, $arr2);
print_r($arr);
function combine() {
$keys = array();
foreach (func_get_args() as $arr) {
if (is_array($arr)) {
$keys += $arr;
}
}
$keys = array_keys($keys);
$values = array_pad(array(), count($keys), array());
$ret = array_combine($keys, $values);
foreach (func_get_args() as $arr) {
foreach ($keys as $k) {
$v = array_key_exists($k, $arr) ? $arr[$k] : '';
array_push($ret[$k], $v);
}
}
return $ret;
}
Output:
Array
(
[a] => Array
(
[0] => 1
[1] => 9
)
[b] => Array
(
[0] => 2
[1] => 8
)
[c] => Array
(
[0] => 3
[1] =>
)
[d] => Array
(
[0] =>
[1] => 7
)
)
I like cletus's approach, so I've just made sure it works :)
function combine() {
$keys = array();
foreach (func_get_args() as $arr) {
if (is_array($arr)) {
$keys = array_merge($keys, array_keys($arr));
}
}
$keys = array_unique($keys);
$values = array_pad(array(), count($keys), array());
$ret = array_combine($keys, $values);
foreach (func_get_args() as $arr) {
foreach ($keys as $k) {
$v = '';
if (array_key_exists($k, $arr)){
$v = $arr[$k];
}
array_push($ret[$k], $v);
}
}
return $ret;
}
How would you flip 90 degrees (transpose) a multidimensional array in PHP? For example:
// Start with this array
$foo = array(
'a' => array(
1 => 'a1',
2 => 'a2',
3 => 'a3'
),
'b' => array(
1 => 'b1',
2 => 'b2',
3 => 'b3'
),
'c' => array(
1 => 'c1',
2 => 'c2',
3 => 'c3'
)
);
$bar = flipDiagonally($foo); // Mystery function
var_dump($bar[2]);
// Desired output:
array(3) {
["a"]=>
string(2) "a2"
["b"]=>
string(2) "b2"
["c"]=>
string(2) "c2"
}
How would you implement flipDiagonally()?
Edit: this is not homework. I just want to see if any SOers have a more creative solution than the most obvious route. But since a few people have complained about this problem being too easy, what about a more general solution that works with an nth dimension array?
i.e. How would you write a function so that:
$foo[j][k][...][x][y][z] = $bar[z][k][...][x][y][j]
?(ps. I don't think 12 nested for loops is the best solution in this case.)
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
Or if you're using PHP 5.6 or later:
function transpose($array) {
return array_map(null, ...$array);
}
With 2 loops.
function flipDiagonally($arr) {
$out = array();
foreach ($arr as $key => $subarr) {
foreach ($subarr as $subkey => $subvalue) {
$out[$subkey][$key] = $subvalue;
}
}
return $out;
}
I think you're referring to the array transpose (columns become rows, rows become columns).
Here is a function that does it for you (source):
function array_transpose($array, $selectKey = false) {
if (!is_array($array)) return false;
$return = array();
foreach($array as $key => $value) {
if (!is_array($value)) return $array;
if ($selectKey) {
if (isset($value[$selectKey])) $return[] = $value[$selectKey];
} else {
foreach ($value as $key2 => $value2) {
$return[$key2][$key] = $value2;
}
}
}
return $return;
}
Transposing an N-dimensional array:
function transpose($array, &$out, $indices = array())
{
if (is_array($array))
{
foreach ($array as $key => $val)
{
//push onto the stack of indices
$temp = $indices;
$temp[] = $key;
transpose($val, $out, $temp);
}
}
else
{
//go through the stack in reverse - make the new array
$ref = &$out;
foreach (array_reverse($indices) as $idx)
$ref = &$ref[$idx];
$ref = $array;
}
}
$foo[1][2][3][3][3] = 'a';
$foo[4][5][6][5][5] = 'b';
$out = array();
transpose($foo, $out);
echo $out[3][3][3][2][1] . ' ' . $out[5][5][6][5][4];
Really hackish, and probably not the best solution, but hey it works.
Basically it traverses the array recursively, accumulating the current indicies in an array.
Once it gets to the referenced value, it takes the "stack" of indices and reverses it, putting it into the $out array. (Is there a way of avoiding use of the $temp array?)
Codler's answer fails for a single-row matrix (e.g. [[1,2]]) and also for the empty matrix ([]), which must be special-cased:
function transpose(array $matrix): array {
if (!$matrix) return [];
return array_map(count($matrix) == 1 ? fn ($x) => [$x] : null, ...$matrix);
}
(note: PHP 7.4+ syntax, easy enough to adapt for older versions)
I got confronted with the same problem. Here is what i came up with:
function array_transpose(array $arr)
{
$keys = array_keys($arr);
$sum = array_values(array_map('count', $arr));
$transposed = array();
for ($i = 0; $i < max($sum); $i ++)
{
$item = array();
foreach ($keys as $key)
{
$item[$key] = array_key_exists($i, $arr[$key]) ? $arr[$key][$i] : NULL;
}
$transposed[] = $item;
}
return $transposed;
}
I needed a transpose function with support for associative array:
$matrix = [
['one' => 1, 'two' => 2],
['one' => 11, 'two' => 22],
['one' => 111, 'two' => 222],
];
$result = \array_transpose($matrix);
$trans = [
'one' => [1, 11, 111],
'two' => [2, 22, 222],
];
And the way back:
$matrix = [
'one' => [1, 11, 111],
'two' => [2, 22, 222],
];
$result = \array_transpose($matrix);
$trans = [
['one' => 1, 'two' => 2],
['one' => 11, 'two' => 22],
['one' => 111, 'two' => 222],
];
The array_unshift trick did not work NOR the array_map...
So I've coded a array_map_join_array function to deal with record keys association:
/**
* Similar to array_map() but tries to join values on intern keys.
* #param callable $callback takes 2 args, the intern key and the list of associated values keyed by array (extern) keys.
* #param array $arrays the list of arrays to map keyed by extern keys NB like call_user_func_array()
* #return array
*/
function array_map_join_array(callable $callback, array $arrays)
{
$keys = [];
// try to list all intern keys
array_walk($arrays, function ($array) use (&$keys) {
$keys = array_merge($keys, array_keys($array));
});
$keys = array_unique($keys);
$res = [];
// for each intern key
foreach ($keys as $key) {
$items = [];
// walk through each array
array_walk($arrays, function ($array, $arrKey) use ($key, &$items) {
if (isset($array[$key])) {
// stack/transpose existing value for intern key with the array (extern) key
$items[$arrKey] = $array[$key];
} else {
// or stack a null value with the array (extern) key
$items[$arrKey] = null;
}
});
// call the callback with intern key and all the associated values keyed with array (extern) keys
$res[$key] = call_user_func($callback, $key, $items);
}
return $res;
}
and array_transpose became obvious:
function array_transpose(array $matrix)
{
return \array_map_join_array(function ($key, $items) {
return $items;
}, $matrix);
}
We can do this by using Two foreach. Traveling one array and another array to create new arrayLike This:
$foo = array(
'a' => array(
1 => 'a1',
2 => 'a2',
3 => 'a3'
),
'b' => array(
1 => 'b1',
2 => 'b2',
3 => 'b3'
),
'c' => array(
1 => 'c1',
2 => 'c2',
3 => 'c3'
)
);
$newFoo = [];
foreach($foo as $a => $k){
foreach($k as $i => $j){
$newFoo[$i][]= $j;
}
}
Check The Output
echo "<pre>";
print_r($newFoo);
echo "</pre>";
Before I start, I'd like to say thanks again to #quazardus for posting his generalised solution for tranposing any two dimenional associative (or non-associative) array!
As I am in the habit of writing my code as tersely as possible I went on to "minimizing" his code a little further. This will very likely not be to everybody's taste. But just in case anyone should be interested, here is my take on his solution:
function arrayMap($cb, array $arrays) // $cb: optional callback function
{ $keys = [];
array_walk($arrays, function ($array) use (&$keys)
{ $keys = array_merge($keys, array_keys($array)); });
$keys = array_unique($keys); $res = [];
foreach ($keys as $key) {
$items = array_map(function ($arr) use ($key)
{return isset($arr[$key]) ? $arr[$key] : null; },$arrays);
$res[$key] = call_user_func(
is_callable($cb) ? $cb
: function($k, $itms){return $itms;},
$key, $items);
}
return $res;
}
Now, analogous to the PHP standard function array_map(), when you call
arrayMap(null,$b);
you will get the desired transposed matrix.
This is another way to do the exact same thing which #codler s answer does. I had to dump some arrays in csv so I used the following function:
function transposeCsvData($data)
{
$ct=0;
foreach($data as $key => $val)
{
//echo count($val);
if($ct< count($val))
$ct=count($val);
}
//echo $ct;
$blank=array_fill(0,$ct,array_fill(0,count($data),null));
//print_r($blank);
$retData = array();
foreach ($data as $row => $columns)
{
foreach ($columns as $row2 => $column2)
{
$retData[$row2][$row] = $column2;
}
}
$final=array();
foreach($retData as $k=>$aval)
{
$final[]=array_replace($blank[$k], $aval);
}
return $final;
}
Test and output reference: https://tutes.in/how-to-transpose-an-array-in-php-with-irregular-subarray-size/
Here is array_walk way to achieve this,
function flipDiagonally($foo){
$temp = [];
array_walk($foo, function($item,$key) use(&$temp){
foreach($item as $k => $v){
$temp[$k][$key] = $v;
}
});
return $temp;
}
$bar = flipDiagonally($foo); // Mystery function
Demo.
Here's a variation of Codler/Andreas's solution that works with associative arrays. Somewhat longer but loop-less purely functional:
<?php
function transpose($array) {
$keys = array_keys($array);
return array_map(function($array) use ($keys) {
return array_combine($keys, $array);
}, array_map(null, ...array_values($array)));
}
Example:
<?php
$foo = array(
"fooA" => [ "a1", "a2", "a3"],
"fooB" => [ "b1", "b2", "b3"],
"fooC" => [ "c1", "c2", "c3"]
);
print_r( transpose( $foo ));
// Output like this:
Array (
[0] => Array (
[fooA] => a1
[fooB] => b1
[fooC] => c1
)
[1] => Array (
[fooA] => a2
[fooB] => b2
[fooC] => c2
)
[2] => Array (
[fooA] => a3
[fooB] => b3
[fooC] => c3
)
);