Include an array in another array - php

Was wondering how to add the values of one array into another to save me typing the values of one array over and over:
$array_main = array(
'[1]' => '1',
'[2]' => '2',
'[3]' => '3',
'[4]' => '4'
);
$array_1 = array( $array_main, '[5]' => '5' );
This deduces:
$array_1 = array(
array(
'[1]' => '1',
'[2]' => '2',
'[3]' => '3',
'[4]' => '4'
),
'[5]' => '5'
);
But I wanted:
$array_1 = array(
'[1]' => '1',
'[2]' => '2',
'[3]' => '3',
'[4]' => '4',
'[5]' => '5'
);
So is there anything that can turn an array into a string? I've tried implode and array_shift but I need the whole array() not just the values..

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
The above example will output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
http://php.net/manual/en/function.array-merge.php

Fastest way is simply use single array like following,
$array_main = array(
'[1]' => '1',
'[2]' => '2',
'[3]' => '3',
'[4]' => '4'
);
$array1 = $array_main;
$array1['[5]'] = '5';
Though if specific requirement for new array use array_merge,
$array1 = array_merge($array_main,array('[5]' => '5'));

You can use merge array (don't just want to add an extra value) to merge two arrays:
<?php
$array1 = array("0" => "0", "1" => "1");
$array2 = array("a" => "a", "b" => "b");
print_r( array_merge($array1, $array2 );
?>
Prints:
Array
(
[0] => 0
[1] => 1
[a] => a
[b] => b
)

Use array_merge()
$array_1 = array_merge($array_main, array('[5]' => '5'));

Related

How to combine 4 multidimensional arrays into 1 multidimensional

I unload 4 different tables from MySQL with different keys. I need to combine them into one array. I'll sort them by date (but it's not important and I know how to do it).
As I see it:
foreach ($rows2 as $msgs2) {
$arraynew = array_merge($arraynew, array('cost' => $msgs2['vivod'], 'date' => $msgs2['date'], 'type' => '1'));
}
foreach ($rows3 as $msgs3) {
$arraynew = array_merge($arraynew, array('cost' => $msgs3['price'], 'date' => $msgs3['data'], 'type' => '2'));
}
foreach ($rows4 as $msgs4) {
$arraynew = array_merge($arraynew, array('cost' => $msgs4['price'], 'date' => $msgs4['data'], 'type' => '3'));
}
foreach ($rows5 as $msgs5) {
$arraynew = array_merge($arraynew, array('cost' => $msgs5['cost'], 'date' => $msgs5['data'], 'type' => '4'));
}
But it does not work.
<?php
$foos = [
['vivod' => '11', 'date' => '20140112'],
['vivod' => '23', 'date' => '20140113']
];
$bars = [
['price' => '29', 'date' => '20171201'],
['price' => '31', 'date' => '20170102']
];
$result = array();
foreach ($foos as $foo) {
$result[] = array('cost' => $foo['vivod'], 'date' => $foo['date'], 'type' => '1');
}
foreach ($bars as $bar) {
$result[] = array('cost' => $bar['price'], 'date' => $bar['date'], 'type' => '2');
}
var_export($result);
Output:
array (
0 =>
array (
'cost' => '11',
'date' => '20140112',
'type' => '1',
),
1 =>
array (
'cost' => '23',
'date' => '20140113',
'type' => '1',
),
2 =>
array (
'cost' => '29',
'date' => '20171201',
'type' => '2',
),
3 =>
array (
'cost' => '31',
'date' => '20170102',
'type' => '2',
),
)
What you are looking for is array_merge_recursive,
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>
Output
Array
(
[color] => Array
(
[favorite] => Array
(
[0] => red
[1] => green
)
[0] => blue
)
[0] => 5
[1] => 10
)
use array_reduce($array, 'array_merge', array()).
Example:
$a = array(array(1, 2, 3), array(4, 5, 6),array(7, 8, 9),array(10, 11, 12));
$result = array_reduce($a, 'array_merge', array());`

Cakephp combine two or more arrays

How can I combine for example this arrays?
(int) 0 => array(
'Test' => array(
'id' => '108',
'bericht' => '12',
'fzge' => '35'
'treiber' => 'Analyse'
)
),
(int) 1 => array(
'Test' => array(
'id' => '109',
'bericht_id' => '12',
'fzge' => '25',
'treiber' => 'Analyse'
)
)
The result that I want is somethings like: 'Analyse' => '60', so I would like to sum the "fzge" if "treiber" is the same.
Thank you in advance.
Use the below code. Considering keys like ('Test', 'treiber' and 'fzge') of array remain same for all arrays
// declare new array
$new_array = array();
foreach($array as $key=>$value){
$new_array[$value['Test']['treiber']] = (isset($new_array[$value['Test']['treiber']])) ? $new_array[$value['Test']['treiber']]+$value['Test']['fzge'] : $value['Test']['fzge'];
}
print_r($new_array);
Output:
Array
(
[Analyse] => 60
)

Merge two arrays, replacing values of same key

I have two arrays
First Array
(
[0] => Array
(
[352] => 1
[128] =>
[64] =>
[70] => 2
)
)
Second array is like this :
Array
(
[0] => Array
(
[128] => 1.5
)
)
I want to make final array like this.(i want to store the matching into the main array in this example it is 128 -> 1.5) how can i do it.?
Array
(
[0] => Array
(
[352] => 1
[128] => 1.5
[64] =>
[70] => 2
)
)
here is my array variables:
print_r($listskilssresult);
print_r($listskilssresultmatching);
You need to use array_replace_recursive
$listskilssresult = [
[
352 => 1,
128 => '',
64 => '',
70 => 2
]
];
$listskilssresultmatching = [
[
128 => 1.5
]
];
print_r(array_replace_recursive($listskilssresult, $listskilssresultmatching));
Prints :
Array
(
[0] => Array
(
[352] => 1
[128] => 1.5
[64] =>
[70] => 2
)
)
Know the difference between array_replace_recursive and array_merge_recursive here
This is specific to your question. If you want to make something more automated, you can create a function. But this will do what you want:
<?php
$array1 = [[352 => 1, 128 => null, 64 => null, 70 => 2]];
$array2 = [[128 => 1.5]];
$keys1 = array_keys($array1[0]);
$keys2 = array_keys($array2[0]);
foreach ($keys1 as $key => $value) {
if (in_array($value, $keys2)) {
$array1[0][$value] = $array2[0][$value];
unset($array2[0][$value]);
}
}
if (!empty($array2[0])) {
foreach ($array2[0] as $key => $value) {
$array1[0][$key] = $value;
unset($array2[0][$key]);
}
}
print_r($array1[0]);
?>
The last if statement will add key + value from the 2nd array to the first if no match was found for them (from the foreach statement). You can just delete that condition if you just want to add only matching keys.
For this solution, you have to use array_merge() built-in php function.
Syntax:
$finalArray = array_merge($array1, $array2);
print_r($finalArray)
Example:
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
Output:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
Reference : http://php.net/manual/en/function.array-merge.php
array_replace_recursive is the best solution!
$old_array = Array('my_index1' => Array('1' => 'A', '2' => 'B'), 'my_index2' => Array('1' => 'C', '2' => 'D'));
$new_array = Array('my_index2' => Array('2' => 'Z'));
$result = array_replace_recursive($old_array, $new_array);
//Result : Array('my_index1' => Array('1' => 'A', '2' => 'B'), 'my_index2' => Array('1' => 'C', '2' => 'Z'));
array_merge would do the job for you:
array_merge($listskilssresult,$listskilssresultmatching)

How to merge multiple two dimensional array based on first dimension key?

My problem statement is like follows:
Suppose I have 2 two dimensional array. The arrays are:
$array1 = Array
(
[8] => Array
(
[branch_code] => DG-52484
[total_desg] => 11
)
);
$array2 = Array
(
[8] => Array
(
[total_dak] => 0
[total_dak_decision] => 0
)
);
After combining the two array my required out put will be:
Array
(
[8] => Array
(
[branch_code] => DG-52484
[total_desg] => 11
[total_dak] => 0
[total_dak_decision] => 0
)
);
Is there any php function for this type of task. Please note that i am not interested to use foreach or while in my situation.
Thanks in advance.
It will work with array_replace_recursive:
$array1 = Array(
8 => Array(
'branch_code' => 'DG-52484',
'total_desg' => '11',
)
);
$array2 = Array
(
8 => Array(
'total_dak' => 0,
'total_dak_decision' => 0,
)
);
var_dump(array_replace_recursive($array1, $array2));
Output
array (size=1)
8 =>
array (size=4)
'branch_code' => string 'DG-52484' (length=8)
'total_desg' => string '11' (length=2)
'total_dak' => int 0
'total_dak_decision' => int 0
try to use
$array = array(
8 => array_merge($array1[8],$array2[8]);
);
You can use array_merge
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
Output
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
For more info http://php.net/manual/tr/function.array-merge.php

How do I remove an array elements from another array?

I have this array. How do I remove all those elements which are present in another array i.e. $remove and re-index the final array starting from 1 not 0?
$info = array(
'1' => array('name' => 'abc', 'marks' => '56'),
'2' => array('name' => 'def', 'marks' => '85'),
'3' => array('name' => 'ghi', 'marks' => '99'),
'4' => array('name' => 'jkl', 'marks' => '73'),
'5' => array('name' => 'mno', 'marks' => '59')
);
$remove = array(1,3);
Desired Output:
$info = array(
'1' => array('name' => 'def', 'marks' => '85'),
'2' => array('name' => 'jkl', 'marks' => '73'),
'3' => array('name' => 'mno', 'marks' => '59')
);
So far I've tried these two methods. Nothing worked for me.
if (($key = array_search(remove[0], $info))) {
unset($info[$key]);
$info = array_values($info);
}
And
$result = array_diff($info, $remove);
Something like this will work:
$result = array_diff_key( $info, array_flip( $remove));
This array_flip()s your $remove array so the keys become the values and the values becomes the keys. Then, we do a difference against the keys with array_diff_key() of both arrays, to get this result:
Array
(
[2] => Array
(
[name] => def
[marks] => 85
)
[4] => Array
(
[name] => jkl
[marks] => 73
)
[5] => Array
(
[name] => mno
[marks] => 59
)
)
Finally, to yield your exact output, you can reindex your array by passing it through array_values(), but this will yield sequential indexes starting at zero, not one:
$result = array_values( array_diff_key( $info, array_flip( $remove)));
If you really need indexes to start at one, you will need a combination of array_combine() and range():
$result = array_diff_key( $info, array_flip( $remove));
$result = array_combine( range( 1, count( $result)), $result);
What about using array_diff function?
Example
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
this will output
Array
(
[1] => blue
)

Categories