How to combine 4 multidimensional arrays into 1 multidimensional - php

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());`

Related

how to display selected from multidimensional array

I have multidimensional array in which two arrays have multiple array and one has single value which are as below:
$array([0] => array(
[0] => array(
['name'] => 'one',
['number'] => 051
),
[1] => array(
['name'] => 'two',
['number'] => 052
)
),
[1] => array(
['name'] => 'three',
['number'] => 053
),
[2] => array(
[0] => array(
['name'] => 'four',
['number'] => 054
),
[1] => array(
['name'] => 'six',
['number'] => 055
)
)
From above array, how can I get result :
[0] = 051, 052.
[1] = 053.
[2] = 054, 055.
Please help, thank you.
Please try below code:
$var = array(
array(
array(
'name' => 'one',
'number' => '051'
),
array(
'name' => 'two',
'number' => '052'
)
),
array(
'name' => 'three',
'number' => '053'
),
array(
array(
'name' => 'four',
'number' => '054'
), array(
'name' => 'six',
'number' => '055'
)
));
$numbers = [];
foreach ($var as $k => $v) {
$num = [];
if(isset($v['number'])){
$num[] = $v['number'];
}
foreach ($v as $k2 => $v2) {
if(isset($v2['number'])){
$num[] = $v2['number'];
}
}
$numbers[$k] = implode(',', $num) . '.';
}

calculate the difference between two array

In PHP, how can I compare two array and get result in a third array ?
This result must contain old and new values. The goal is to display an HTML table with a header like "field | old value | new value" as this, the user can compare all values one by one.
First array :
$array1 = array(
'key1' => array(
'key1.1' => 'value',
'key1.2' => 'value',
),
'key2' => array(
'key2.1' => 'value',
'key2.2' => 'value',
),
'key3' => array(
array('key3.1' => 'value'),
array('key3.2' => 'value'),
),
);
Second array :
$array2 = array(
'key1' => array(
'key1.1' => 'value',
'key1.2' => 'value',
),
'key2' => array(
'key2.1' => 'value',
'key2.2' => 'value',
),
'key3' => array(
array('key3.1' => 'value'),
array('key3.2' => 'value'),
),
);
What I expect :
$results = array(
'key1' => array(
'key1.1' => array(
'old' => 'old_value',
'new' => 'new_value',
),
'key1.2' => array(
'old' => 'old_value',
'new' => 'new_value',
),
),
'key2' => array(
'key2.1' => array(
'old' => 'old_value',
'new' => 'new_value',
),
'key2.2' => array(
'old' => 'old_value',
'new' => 'new_value',
),
),
'key3' => array(
array(
'key3.1' => array(
'old' => 'old_value',
'new' => 'new_value')
),
array(
'key3.1' => array(
'old' => 'old_value',
'new' => 'new_value'),
)
),
);
What I have already tried without success :
function array_diff_assoc_recursive($array1, $array2) {
$exclude = array(
'custom_key'
);
$difference = array();
foreach($array1 as $key => $value) {
if(is_array($value)){
if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
if(!in_array($key,$exclude)){
$difference[$key]['old'] = $value;
$difference[$key]['new'] = $array2[$key];
}
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if(!empty($new_diff))
$difference[$key] = $new_diff;
}
} else if(!array_key_exists($key,$array2) || $array2[$key] !== $value) {
if(!in_array($key,$exclude)){
$difference[$key]['old'] = $value;
$difference[$key]['new'] = $array2[$key];
}
}
}
return $difference;
}
try this code, i'm sure this will work for you
<?php
echo "<pre>";
$array1 = array(
'key1' => array(
'key1.1' => 'aaa',
'key1.2' => 'xxx',
'key1.3' => 'vvv',
),
'key2' => array(
'key2.1' => 'eee',
'key2.2' => 'fff',
'key2.3' => 'ggg',
),
) ;
echo "Array 1: </br>";
print_r($array1);
$array2 = array(
'key1' => array(
'key1.1' => 'aaa',
'key1.2' => 'ddd',
'key1.3' => 'ccc',
),
'key2' => array(
'key2.1' => 'hhh',
'key2.2' => 'fff',
'key2.3' => 'ttt',
),
);
echo "Array 2:</br>";
print_r($array2);
$result='';
foreach($array1 as $key=> $val)
{
foreach($val as $k=> $v)
{
if($v != $array2[$key][$k])
{
$result[$key][$k]['old']= $array2[$key][$k] ;
$result[$key][$k]['new']= $v;
}
}
}
echo "Compared Result: </br>";
echo "<pre>"; print_r($result);
?>
This will Output
Array 1:
Array
(
[key1] => Array
(
[key1.1] => aaa
[key1.2] => xxx
[key1.3] => vvv
)
[key2] => Array
(
[key2.1] => eee
[key2.2] => fff
[key2.3] => ggg
)
)
Array 2:
Array
(
[key1] => Array
(
[key1.1] => aaa
[key1.2] => ddd
[key1.3] => ccc
)
[key2] => Array
(
[key2.1] => hhh
[key2.2] => fff
[key2.3] => ttt
)
)
Compared Result:
Array
(
[key1] => Array
(
[key1.2] => Array
(
[old] => ddd
[new] => xxx
)
[key1.3] => Array
(
[old] => ccc
[new] => vvv
)
)
[key2] => Array
(
[key2.1] => Array
(
[old] => hhh
[new] => eee
)
[key2.3] => Array
(
[old] => ttt
[new] => ggg
)
)
)
Prepare arrays using array_walk_recursive, and combine them. Please note that the original arrays will be changed
array_walk_recursive($array1, function(&$i) { if(!is_array($i)) $i = array('old'=> $i); });
array_walk_recursive($array2, function(&$i) { if(!is_array($i)) $i = array('new'=> $i); });
print_r(array_merge_recursive($array1, $array2));
demo

PHP - generate multi-dimentional array

This is my array:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
I've to convert this array into multi-dimentional array as below:
$arr = array(
'jan-2015' => array(
0 => array(
'title' => 'test1',
'count' => 4,
),
1 => array(
'title' => 'test2',
'count' => 10,
),
),
'jun-2015' => array(
0 => array(
'title' => 'test3',
'count' => 14,
),
),
'july-2015' => array(
0 => array(
'title' => 'test4',
'count' => 45,
),
),
);
I've tried to make it as expected but unfortunately i can't make this.
Is any other solutions for this?
According to your data structure :
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
try this:
$newArray = array();
foreach($arr as $key => $val) {
$newArray[$val['month']][] = $val;
}
echo '<pre>'.print_r($newArray,1).'</pre>';
Output:
Array
(
[jan-2015] => Array
(
[0] => Array
(
[title] => test1
[count] => 4
[month] => jan-2015
)
[1] => Array
(
[title] => test2
[count] => 10
[month] => jan-2015
)
)
[jun-2015] => Array
(
[0] => Array
(
[title] => test3
[count] => 14
[month] => jun-2015
)
)
[july-2015] => Array
(
[0] => Array
(
[title] => test4
[count] => 45
[month] => july-2015
)
)
)
You could use this function:
function transform($input) {
// Extract months, and use them as keys, with value set to empty array
// The array_fill_keys also removes duilicates
$output = array_fill_keys(array_column($input, 'month'), array());
foreach ($input as $element) {
$copy = $element;
// remove the month key
unset($copy["month"]);
// assign this to the month key in the output
$output[$element["month"]][] = $copy;
}
return $output;
}
Call it like this:
$arr = array(
0 => array(
'title' => 'test1',
'count' => 4,
'month' => 'jan-2015'
),
1 => array(
'title' => 'test2',
'count' => 10,
'month' => 'jan-2015'
),
2 => array(
'title' => 'test3',
'count' => 14,
'month' => 'jun-2015'
),
3 => array(
'title' => 'test4',
'count' => 45,
'month' => 'july-2015'
),
);
print_r (transform($arr));
Output:
Array
(
[jan-2015] => Array
(
[0] => Array
(
[title] => test1
[count] => 4
)
[1] => Array
(
[title] => test2
[count] => 10
)
)
[jun-2015] => Array
(
[0] => Array
(
[title] => test3
[count] => 14
)
)
[july-2015] => Array
(
[0] => Array
(
[title] => test4
[count] => 45
)
)
)
By using answer of #Girish Patidar, You can achieve this by:
$outputArr = array();
$to_skip = array();
foreach($arr as $row){
$to_skip = $row;
unset($to_skip['month']);
$outputArr[$row['month']][] = $to_skip;
}
echo "<pre>";
print_r($outputArr);
die;
There could many way to do this. Please try this one if it works for you
<?php
$newArr=NULL;
foreach($arr as $array)
{
$temp=NULL;
$temp['title']=$array['title'];
$temp['count']=$array['count'];
$newArr[$array['month']][]=$temp;
}
var_dump($newArr);
?>

Find similar subarrays in some arrays

Have some arrays:
$a = array(
0 => array('value' => 1000, 'name' => 'AA1'),
1 => array('value' => 2000, 'name' => 'AA2'),
2 => array('value' => 3000, 'name' => 'AA3'),
3 => array('value' => 4000, 'name' => 'AA4'),
);
$b = array(
0 => array('value' => 1000, 'name' => 'BB1'),
1 => array('value' => 3000, 'name' => 'BB2'),
2 => array('value' => 1700, 'name' => 'BB3'),
3 => array('value' => 1200, 'name' => 'BB4'),
);
$c = array(
0 => array('value' => 3000, 'name' => 'CC1'),
1 => array('value' => 4000, 'name' => 'CC2'),
2 => array('value' => 4300, 'name' => 'CC3'),
3 => array('value' => 5000, 'name' => 'CC4'),
);
How can create a new array with the same variants from arrays $a, $b and $c? And order it by 'value' field..
$d = array(
0 => array('value' => 3000, 'name' => 'AA3');
);
Real example: http://3v4l.org/MAWjd
// Test Data
$timeVariantSerialized = 'a:3:{s:7:"group_a";a:3:{i:0;a:3:{s:10:"id_variant";i:122;s:4:"name";s:13:"3 hour 30 min";s:4:"time";i:12600;}i:1;a:3:{s:10:"id_variant";i:173;s:4:"name";s:6:"3 hour";s:4:"time";i:10800;}i:2;a:3:{s:10:"id_variant";i:271;s:4:"name";s:6:"1 hour";s:4:"time";i:3600;}}s:7:"group_b";a:2:{i:0;a:3:{s:10:"id_variant";i:107;s:4:"name";s:13:"1 hour 30 min";s:4:"time";i:5400;}i:1;a:3:{s:10:"id_variant";i:321;s:4:"name";s:6:"3 hour";s:4:"time";i:10800;}}s:7:"group_c";a:4:{i:0;a:3:{s:10:"id_variant";i:28;s:4:"name";s:6:"1 hour";s:4:"time";i:3600;}i:1;a:3:{s:10:"id_variant";i:98;s:4:"name";s:6:"3 hour";s:4:"time";i:10800;}i:2;a:3:{s:10:"id_variant";i:157;s:4:"name";s:6:"2 hour";s:4:"time";i:7200;}i:3;a:3:{s:10:"id_variant";i:158;s:4:"name";s:13:"1 hour 30 min";s:4:"time";i:5400;}}}';
// Test Data Array
$time_variant = unserialize( $timeVariantSerialized );
$groupsCount = count( $time_variant );
foreach( $time_variant as $groupId => $groupArray )
{
foreach( $groupArray as $groupChildId => $groupChildArray )
{
$timeCountsArray[$groupChildArray['time']][$groupId] = $groupChildId;
}
}
$result = array();
foreach( $timeCountsArray as $time => $groupIdsArray )
{
if( $groupsCount > count( $groupIdsArray ) ) continue;
foreach( $groupIdsArray as $groupId => $groupChildId )
{
$result[$time_variant[$groupId][$groupChildId]['id_variant']]
= $time_variant[$groupId][$groupChildId];
// If you need only one result , uncomment the below break
// break;
}
// If more than 1 time could have variants , comment out below break to get them all
break;
}
ksort( $result );
print_r( $result );
Result Array :
array
(
'98' => array
(
'id_variant' => 98
'name' => 3 hour
'time' => 10800
)
'173' => array
(
'id_variant' => 173
'name' => 3 hour
'time' => 10800
)
'321' => array
(
'id_variant' => 321
'name' => 3 hour
'time' => 10800
)
)
I'm not sure if this is what you want.
The result is a single array with all the repeated values in the given arrays:
//Lets merge them into a big array to work in a easier way;
//It will be a single level array as follow: [a0] => 1000, [a1] => 2000 etc.
$array = array('a' => $a, 'b' => $b, 'c' =>$c);
foreach($array as $letter => $subArray){
foreach($subArray as $key => $values){
$newArray[$letter.$key] = $values['value'];
}
}
$uniques = array_unique($newArray); //getting the non-repeated values
$repeated = array_diff_assoc($newArray, $uniques); // stripping them out
$final = array_unique($repeated); //cleaning the repeated values to just 1 of each
print_r($final);
Result
Array
(
[b0] => 1000
[b1] => 3000
[c1] => 4000
)

Searching in a multi-dimensional array (PHP)

I'm having a problem lately that's driving me crazy. I have a multi-dimensional array like this:
$a = array(
'db' => array(
'0' => array(
'id' => '1',
'name' => 'test',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
'1' => array(
'id' => '2',
'name' => 'test2',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
)
'2' => array(
'id' => '3',
'name' => 'test',
'cat' => array(
'a' => '50',
'b' => '40',
'c' => '90'
),
'canvas' => '1'
)
)
);
And i want to search on it using a function like this: search('canvas = 1');
That would return all the arrays, child of db, that have a key canvas with the value of 1. Or, for example:
search('a = 15');
Would return all arrays that have a key, child of cat, named a and with a value of 15.
$a = array(
'db' => array(
'0' => array(
'id' => '1',
'name' => 'test',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
'1' => array(
'id' => '2',
'name' => 'test2',
'cat' => array(
'a' => '15',
'b' => '20',
'c' => '30'
),
'canvas' => '2'
),
)
);
//checks if array $array contains element with $searchKey key, and $searchVal value
function arrayContains($array, $searchVal, $searchKey) {
if (!is_array($array))
return false;
foreach ($array as $key => $value) {
if ($key === $searchKey && $searchVal === $value)
return true;
if (is_array($value) && arrayContains($value, $searchVal, $searchKey))
return true;
}
return false;
}
function search($a, $search) {
list($searchKey, $searchVal) = explode('=', $search);
$result = array();
foreach($a as $val) {
if (arrayContains($val, $searchVal, $searchKey))
$result[] = $val;
}
return $result;
}
print_r(search($a['db'], "a=15"));
print_r(search($a['db'], "canvas=1"));
Which produces this output(outputs sub-arrays of $a['db'] which contain searched key=>value pair):
Array
(
[0] => Array
(
[id] => 1
[name] => test
[cat] => Array
(
[a] => 15
[b] => 20
[c] => 30
)
[canvas] => 2
)
[1] => Array
(
[id] => 2
[name] => test2
[cat] => Array
(
[a] => 15
[b] => 20
[c] => 30
)
[canvas] => 2
)
)
Array
(
[0] => Array
(
[id] => 3
[name] => test
[cat] => Array
(
[a] => 50
[b] => 40
[c] => 90
)
[canvas] => 1
)
)
Just check the below link if this can help you -
http://php.net/manual/en/function.array-search.php
It contains detailed documentation of php function array_search() and various user codes for searching in multi-dimensional array along with user reviews.
function search($array, $canvas)
{
$result = array();
foreach ($array as $k1 => $v1) {
foreach ($v1 as $k2 => $v2) {
if ($v2['canvas'] == $canvas) {
$result[] = $array[$k1][$k2];
}
}
}
return $result;
}
// $a = your array
print_r(search($a, 1));

Categories