I can't understand how to combine these arrays.
$data = array("a", "b", "c")
$array = array(0 => Array(1 , 2, 3), 1 => Array(4, 5, 6))
I tried different functions such as merge, combine, map..
Result has to be:
array(
'a' => array(1, 4),
'b' => array(2, 5),
'c' => array(3, 6),
)
This should work for you:
<?php
$data = array("a", "b", "c");
$array = array(array(1 , 2, 3), array(4, 5, 6));
$result = array();
foreach($data as $key => $value) {
foreach($array as $innerKey => $innerValue)
$result[$value][] = $innerValue[$key];
}
print_r($result);
?>
Output:
Array (
[a] => Array ( [0] => 1 [1] => 4 )
[b] => Array ( [0] => 2 [1] => 5 )
[c] => Array ( [0] => 3 [1] => 6 )
)
A solution that uses the function array_column() available since PHP 5.5:
$data = array("a", "b", "c");
$array = array(0 => Array(1 , 2, 3), 1 => Array(4, 5, 6));
$result = array();
foreach($data as $i => $v) {
$result[$v] = array_column($array, $i);
}
If you are stuck with a previous version then use Rizier123's solution (it does the same thing with just a little more code.
Related
In a piece of software, I merge two arrays with array_merge function. But I need to add the same array (with the same keys, of course) to an existing array.
The problem:
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
array_merge($A, $B);
// result
[a] => 1 [b] => 2 [c] => 4 [d] => 5
As you see, 'c' => 3 is missed.
So how can I merge all of them with the same keys?
You need to use array_merge_recursive instead of array_merge. Of course there can only be one key equal to 'c' in the array, but the associated value will be an array containing both 3 and 4.
Try with array_merge_recursive
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$c = array_merge_recursive($A,$B);
echo "<pre>";
print_r($c);
echo "</pre>";
will return
Array
(
[a] => 1
[b] => 2
[c] => Array
(
[0] => 3
[1] => 4
)
[d] => 5
)
$arr1 = array(
"0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
"1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
"2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
"3" => array("fid" => 1, "tid" => 7, "name" => "Xi"),
"4" => array("fid" => 2, "tid" => 9, "name" => "Xigua")
);
if you want to convert this array as following:
$arr2 = array(
"0" => array(
"0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
"1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
"2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
"3" => array("fid" => 1, "tid" => 7, "name" => "Xi")
),
"1" => array(
"0" =>array("fid" => 2, "tid" => 9, "name" => "Xigua")
)
);
so, my answer will be like this:
$outer_array = array();
$unique_array = array();
foreach($arr1 as $key => $value)
{
$inner_array = array();
$fid_value = $value['fid'];
if(!in_array($value['fid'], $unique_array))
{
array_push($unique_array, $fid_value);
unset($value['fid']);
array_push($inner_array, $value);
$outer_array[$fid_value] = $inner_array;
}else{
unset($value['fid']);
array_push($outer_array[$fid_value], $value);
}
}
var_dump(array_values($outer_array));
hope this answer will help somebody sometime.
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$C = array_merge_recursive($A, $B);
$aWhere = array();
foreach ($C as $k=>$v) {
if (is_array($v)) {
$aWhere[] = $k . ' in ('.implode(', ',$v).')';
}
else {
$aWhere[] = $k . ' = ' . $v;
}
}
$where = implode(' AND ', $aWhere);
echo $where;
I just wrote this function, it should do the trick for you, but it does left join
public function mergePerKey($array1,$array2)
{
$mergedArray = [];
foreach ($array1 as $key => $value)
{
if(isset($array2[$key]))
{
$mergedArray[$value] = null;
continue;
}
$mergedArray[$value] = $array2[$key];
}
return $mergedArray;
}
Two entries in an array can't share a key, you'll need to change the key for the duplicate
In a piece of software, I merge two arrays with array_merge function. But I need to add the same array (with the same keys, of course) to an existing array.
The problem:
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
array_merge($A, $B);
// result
[a] => 1 [b] => 2 [c] => 4 [d] => 5
As you see, 'c' => 3 is missed.
So how can I merge all of them with the same keys?
You need to use array_merge_recursive instead of array_merge. Of course there can only be one key equal to 'c' in the array, but the associated value will be an array containing both 3 and 4.
Try with array_merge_recursive
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$c = array_merge_recursive($A,$B);
echo "<pre>";
print_r($c);
echo "</pre>";
will return
Array
(
[a] => 1
[b] => 2
[c] => Array
(
[0] => 3
[1] => 4
)
[d] => 5
)
$arr1 = array(
"0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
"1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
"2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
"3" => array("fid" => 1, "tid" => 7, "name" => "Xi"),
"4" => array("fid" => 2, "tid" => 9, "name" => "Xigua")
);
if you want to convert this array as following:
$arr2 = array(
"0" => array(
"0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
"1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
"2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
"3" => array("fid" => 1, "tid" => 7, "name" => "Xi")
),
"1" => array(
"0" =>array("fid" => 2, "tid" => 9, "name" => "Xigua")
)
);
so, my answer will be like this:
$outer_array = array();
$unique_array = array();
foreach($arr1 as $key => $value)
{
$inner_array = array();
$fid_value = $value['fid'];
if(!in_array($value['fid'], $unique_array))
{
array_push($unique_array, $fid_value);
unset($value['fid']);
array_push($inner_array, $value);
$outer_array[$fid_value] = $inner_array;
}else{
unset($value['fid']);
array_push($outer_array[$fid_value], $value);
}
}
var_dump(array_values($outer_array));
hope this answer will help somebody sometime.
$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$C = array_merge_recursive($A, $B);
$aWhere = array();
foreach ($C as $k=>$v) {
if (is_array($v)) {
$aWhere[] = $k . ' in ('.implode(', ',$v).')';
}
else {
$aWhere[] = $k . ' = ' . $v;
}
}
$where = implode(' AND ', $aWhere);
echo $where;
I just wrote this function, it should do the trick for you, but it does left join
public function mergePerKey($array1,$array2)
{
$mergedArray = [];
foreach ($array1 as $key => $value)
{
if(isset($array2[$key]))
{
$mergedArray[$value] = null;
continue;
}
$mergedArray[$value] = $array2[$key];
}
return $mergedArray;
}
Two entries in an array can't share a key, you'll need to change the key for the duplicate
I have this code
$array = array('o' => 'one', 't' => 'three', 'f' => 'four');
array_push($array, array('s' => 'six'));
print_r($array);
that insert new key and value into the array , but when i print the new $array it returned this
Array ( [o] => one [t] => three [f] => four [0] => Array ( [s] => six ) )
i need to return like this
Array ( [o] => one [t] => three [f] => four [s] => six )
how to remove [0] => Array () from the array ?
array_push is intended for lists.
$arr = array(5, 6, 7);
array_push($arr, 8); // array(5, 6, 7, 8);
You can add elements to arrays in many ways, this is one:
$array = array('o' => 'one', 't' => 'three', 'f' => 'four');
$array["s"] = "six";
Here is another:
$array = array_merge($array, array("s" => "six"));
PHP treats lists like array(1, 2, 3); differently than associative arrays like array("foo" => "bar");. The differences are minor, but they show up with functions like array_push.
I have multiple associative arrays, similar to the following:
$arr1 = array(0 => 12, 5 => 10, 19 => 48);
$arr2 = array(0 => 14, 7 => 9, 12 => 11, 19 => 30);
I would like to merge these arrays so that I have a single set of keys (0, 5, 7, 12, 19) and each points to an array with the values from the original arrays, and null if the value doesn't exist in the original array:
$merge = array(
0 => array(12, 14),
5 => array(10, null),
7 => array(null, 9),
12 => array(null, 11),
19 => array(48, 30)
);
I need to be able to do this for an arbitrary number of arrays. I'm not sure where to start with this.
I could, I suppose, iterate through each array, append it's value to the result - but I'd have to check to see if I have the requisite number of elements in each resulting array before appending the current value to that index - which isn't terribly efficient.
Ideas or pointers?
$arr1 = array(0 => 12, 5 => 10, 19 => 48);
$arr2 = array(0 => 14, 7 => 9, 12 => 11, 19 => 30);
$keys = array_merge(array_keys($arr1), array_keys($arr2));
$merged = array();
foreach ($keys as $key) {
$merged[$key] = array();
$merged[$key][] = isset($arr1[$key]) ? $arr1[$key] : null;
$merged[$key][] = isset($arr2[$key]) ? $arr2[$key] : null;
}
ksort($merged);
echo '<pre>', var_dump($merged), '</pre>';
modified for an arbitrary number of arrays
$arrays = array(
array(0 => 12, 5 => 10, 19 => 48),
array(0 => 14, 7 => 9, 12 => 11, 19 => 30),
// ... more arrays
);
$keys = array();
foreach ($arrays as $arr) {
$keys = array_merge($keys, array_keys($arr));
}
$merged = array();
foreach ($keys as $key) {
$merged[$key] = array();
foreach ($arrays as $arr) {
$merged[$key][] = isset($arr[$key]) ? $arr[$key] : null;
}
}
ksort($merged);
echo '<pre>', var_dump($merged), '</pre>';
EDIT
<?php
$arr1 = array(0 => 12, 5 => 10, 19 => 48);
$arr2 = array(0 => 14, 7 => 9, 12 => 11, 19 => 30);
foreach($arr1 as $k => $v){
if(array_key_exists($k, $arr2)){
$newarr[$k][] = $v;
$newarr[$k][] = $arr2[$k];
}else{
$newarr[$k][] = $v;
$newarr[$k][] = 'NULL';
}
}
foreach($arr2 as $k => $v){
if(!array_key_exists($k, $arr1)){
$newarr[$k][] = 'NULL';
$newarr[$k][] = $v;
}
}
ksort($newarr);
echo '<pre>';
print_r($newarr);
?>
Output:
Array
(
[0] => Array
(
[0] => 12
[1] => 14
)
[5] => Array
(
[0] => 10
[1] => NULL
)
[7] => Array
(
[0] => NULL
[1] => 9
)
[12] => Array
(
[0] => NULL
[1] => 11
)
[19] => Array
(
[0] => 48
[1] => 30
)
)
I think this is what you're looking for.
/*Merge function*/
function merge($a1, $a2)
{
foreach($a1 as $key => $val)
$a1[$key] = Array($a1[$key], null);
foreach($a2 as $key => $val)
$a1[$key] = Array((isset($a1[$key]))? $a1[$key][0]: null, $val);
return ksort($a1);
}
/*Test*/
$arr1 = array(0 => 12, 5 => 10, 19 => 48);
$arr2 = array(0 => 14, 7 => 9, 12 => 11, 19 => 30);
foreach(merge($arr1, $arr2) as $key => $val){
echo "<br />$key --";
print_r($val);
}
/*output*/
0 --Array ( [0] => 12 [1] => 14 )
5 --Array ( [0] => 10 [1] => )
7 --Array ( [0] => [1] => 9 )
12 --Array ( [0] => [1] => 11 )
19 --Array ( [0] => 48 [1] => 30 )
Try this function:
function multimerge ($array1, $array2) {
if (is_array($array2) && count($array2)) {
foreach ($array2 as $k => $v) {
if (is_array($v) && count($v)) {
$array1[$k] = multimerge($array1[$k], $v);
} else {
$array1[$k] = $v;
}
}
} else {
$array1 = $array2;
}
return $array1;
}
http://php.net/manual/en/function.array-merge.php
I have two array, let's say
$array1 = array(0 => 10, 1 => 21, 2 => 34, 'somekey' => 45, 'otherkey' => 15);
$array2 = array(0 => 9, 1 => 10, 2 => 14, 'otherkey' => 15, 'somekey' => 43);
I need to return an array with only the values contained by both arrays, independent of their keys. In this case, the resulting array would contain value 10 at key 0, value 15 at key 1
Use array_intersect():
$array3 = array_intersect( $array1, $array2);
If you want to get rid of the keys, run that array through array_values():
$array3 = array_values( $array3);
This will set $array3 to:
Array
(
[0] => 10
[1] => 15
)
<?php
$array1 = array(0 => 10, 1 => 21, 2 => 34, 'somekey' => 45, 'otherkey' => 15);
$array2 = array(0 => 9, 1 => 10, 2 => 14, 'otherkey' => 15, 'somekey' => 43);
$array1 = array_values($array1);
$array2 = array_values($array2);
$array3 = array_merge($array1,$array2);
echo '<pre>';
print_r($array3);
echo '</pre>';
?>