I have two arrays like this:
array1 = Array (
[0] => Array ( [value] => 1 [date] => 2014-03-15 ),
[1] => Array ( [value] => 1 [date] => 2014-03-15 )
);
array2 = Array (
[0] => Array ( [value] => 1 [date] => 2014-03-15 ),
[1] => Array ( [value] => 1 [date] => 2014-03-16 )
);
How to get output like this?
date 2014-03-15 = 3
date 2014-03-16 = 1
You can not merge those array directly with array_merge, because the counting based on 'value', so you must create some codes, try this:
$array1 = array(
array("value" => 1, "date" => "2014-03-15"),
array("value" => 1, "date" => "2014-03-15"),
);
$array2 = array(
array("value" => 1, "date" => "2014-03-15"),
array("value" => 1, "date" => "2014-03-16"),
);
foreach(array_merge($array1, $array2) as $arr){
!isset($array[$arr['date']]) ? $array[$arr['date']] = $arr['value'] : $array[$arr['date']] += $arr['value'];
}
print_r($array);
it will returns :
Array ( [2014-03-15] => 3 [2014-03-16] => 1 )
Related
I am currently working on a project where the fields scale when clicking on the "Add" button.
I am grouping each field like this: name="packaging[]", name="packaging[1]", name="packaging[2]" and so on. When I submit the form, this is how the data looks like when posted:
Array
(
[packaging] => Array
(
[0] => 1
[1] => 2
)
[quantity] => Array
(
[0] => 1
[1] => 2
)
[total-weight] => Array
(
[0] => 1
[1] => 2
)
[length] => Array
(
[0] => 1
[1] => 2
)
)
Using PHP I would like to convert the above code to look like this:
Array
(
[0] => Array
(
[packaging] => 1,
[quantity] => 1,
[total-weight] => 1,
[length] => 1,
)
[1] => Array
(
[packaging] => 2,
[quantity] => 2,
[total-weight] => 2,
[length] => 2,
)
)
Any help would be greatly appreciated.
Try this....
$array=array();
foreach($data as $key=>$value){
foreach($value as $k=>$val){
$array[$k][$key]=$val;
}
}
DEMO
Try this code:
$rows = array ('packaging' => array ('0'=> 1,'1' => 2),'quantity' => array('0'=> 1,'1' => 2),'total-weight' => array ('0'=> 1,'1' => 2),
'length' =>array ('0'=> 1,'1' => 2)
);
$res_array = array();
$total_records = count($rows['packaging']);
for($i=0;$i<$total_records;$i++)
{
$res_array[] = array('packaging'=>$rows['packaging'] [$i],'quantity'=>$rows['quantity'][$i],
'total-weight'=>$rows['total-weight'][$i],'length'=>$rows['length'] [$i]);
}
print_r($res_array);
My array one is like this :
Array
(
[0] => Array
(
[total_transaction] => 2000000
[month] => May
)
[1] => Array
(
[total_transaction] => 1000000
[month] => June
)
)
My array two is like this :
Array
(
[0] => 4000000
[1] => 5000000
)
I want to group the array like this :
Array
(
[0] => Array
(
[target] => 4000000
[total_transaction] => 2000000
[month] => May
)
[1] => Array
(
[target] => 5000000
[total_transaction] => 1000000
[month] => June
)
)
I'm still confused
Any solution to solve my problem?
Thank you very much
The solution using array_map and array_replace_recursive functions:
// $arrOne is your first array
$arrTwo = [4000000, 5000000]; // it's your second array
$arrTwo = array_map(function($v){ return ['target' => $v]; }, $arrTwo);
$result = array_replace_recursive($arrOne, $arrTwo);
print_r($result);
The output:
Array
(
[0] => Array
(
[total_transaction] => 2000000
[month] => May
[target] => 4000000
)
[1] => Array
(
[total_transaction] => 1000000
[month] => June
[target] => 5000000
)
)
Try :
foreach($array1 as $key1=>$arr1) {
if(isset($array2[$key1])) $array1[$key1]['target'] = $array2[$key1];
}
$a = array
(
0 => array
(
"total_transaction" => 2000000,
"month" => "May"
),
1 => array
(
"total_transaction" => 1000000,
"month" => "June"
)
);
$b = array
(
0 => 4000000,
1 => 5000000
);
foreach($a as $key=>$val) {
$a[$key]['target'] = $b[$key];
}
Try this one..
try this,
$out = array();
foreach ($aa1 as $key => $value){
$out[] = (object)array_merge((array)$aa2[$key], (array)$value);
}
print_r($out);
https://3v4l.org/kBJVB
i hope it will be helpful.
$array1 = array(
array(
'total_transaction' => 2000000,
'month' => 'May'
),
array(
'total_transaction' => 1000000,
'month' => 'June'
));
$array2 = array(
'0' => 4000000,
'1' => 5000000
);
foreach($array2 as $k=>$v){
$array1["$k"]['target']=$v;
}
I have an array that looks like this:
getting array need to convert array as same key value as 0
foreach($array as $key=>$id){
$consumer_data[]=$this->App_model->get_session($id);
}
print_r($consumer_data);
Array
(
[0] => Array
(
[0] => Array
(
[ConsumerID] => 1
[name] => asdfd
)
[1] => Array
(
[ConsumerID] => 5
[name] => test
)
[2] => Array
(
[ConsumerID] => 3
[name] => test1
)
)
[1] => Array
(
[0] => Array
(
[ConsumerID] => 4
[name] => test4
)
)
i want to implement array like this in same key value as 0
Array
(
[0] => Array
(
[0] => Array
(
[ConsumerID] => 1
[name] => asdfd
)
[1] => Array
(
[ConsumerID] => 5
[name] => test
)
[2] => Array
(
[ConsumerID] => 3
[name] => test1
)
[3] => Array
(
[ConsumerID] => 4
[name] => test4
)
)
I am using PHP. Can anyone point me to a good starting point as to how I should go about doing this?
You can use array_merge():
$new_array[0] = array_merge($array[0], $array[1]);
Where $array is the first array.
SEE DEMO
OR for a more dynamic approach:
$new_array = array(0 => array());
foreach($array as $a) {
$new_array[0] = array_merge($new_array[0], $a);
}
SEE DEMO 2
The simpliest solution is to do it with:
$input = array(
array(
array('ConsumerID' => 1, 'name' => 'asdfd'),
array('ConsumerID' => 5, 'name' => 'test'),
array('ConsumerID' => 4, 'name' => 'test1'),
),
array(
array('ConsumerID' => 4, 'name' => 'test4'),
),
);
$output = array(
array()
);
foreach ($input as $data) {
$output[0] = array_merge($output[0], $data);
}
Try this->
$newArray = array();
foreach($values as $key=>$val){
$newArray [0][$key]=$val;
}
print_r($newArray);
Check this:
<?php
$arr[0] = array(0 => array("ConsumerID" => 1, "name" => "Ni"), 1 => array("ConsumerID" => 2, "name" => "Ab"));
$arr[1] = array(1 => array("ConsumerID" =>5, "name" => "GE"), 1 => array("ConsumerID" => 6, "name" => "DB"));
$new = array();
foreach($arr as $key => $value) {
foreach($value as $innerkey => $innervalue) {
$new[0][] = $innervalue;
}
}
print_r($new);
?>
How can i remove duplicates in an array like this?
My array $test1 gives me this out:
Array (
[0] => Array ( [id] => 47523 [date] => 12-02-13 14:36:32 )
[1] => Array ( [id] => 47523 [date] => 12-02-13 13:56:48 )
[2] => Array ( [id] => 38639 [date] => 12-02-13 13:38:51 )
[3] => Array ( [id] => 38639 [date] => 12-02-13 13:07:43 )
)
My array $test2 gives me this out:
Array (
[0] => Array ( [id] => 47523 [date] => 12-02-13 14:36:32 )
[1] => Array ( [id] => 47523 [date] => 12-02-13 13:56:48 )
[2] => Array ( [id] => 38639 [date] => 12-02-13 13:38:51 )
[3] => Array ( [id] => 38639 [date] => 12-02-13 13:07:43 )
[4] => Array ( [id] => 53241 [date] => 12-02-13 11:02:48 )
)
But I want the output to be that way with the latest date
Array (
[0] => Array ( [id] => 53241 [date] => 03-02-13 11:02:48 )
)
What can i do?
$test1 = array (
array ( "id" => 47523, "date" => "12-02-13 14:36:32" ),
array ( "id" => 47523, "date" => "12-02-13 13:56:48" ),
array ( "id" => 38639, "date" => "12-02-13 13:38:51" ),
array ( "id" => 38639, "date" => "12-02-13 13:07:43" )
);
$test2 = array (
array ( "id" => 47523, "date" => "12-02-13 14:36:32" ),
array ( "id" => 47523, "date" => "12-02-13 13:56:48" ),
array ( "id" => 38639, "date" => "12-02-13 13:38:51" ),
array ( "id" => 38639, "date" => "12-02-13 13:07:43" ),
array ( "id" => 53241, "date" => "12-02-13 11:02:48" )
);
foreach($test2 as $array) {
if (!in_array($array, $test1)) {
$new[] = $array;
}
}
print_r($new);
First merge the 2 arrays (from $test2 into $tes1 in this case):
foreach($test2 as $id=>$arr){
$test1[] = $arr;
}
Then sort the $test1 by date (putting the oldest dates last, and newest dates first):
foreach ($test1 as $key => $row) {
$orderByDate[$key] = strtotime($row['date']);
}
array_multisort($orderByDate, SORT_DESC, $dataArray);
Then remove the duplicates (This will keep the newest datetime and remove earlier ones)
$unique = array()
foreach($test1 as $id => $arr){
if( in_array($arr->id, $unique ) {
unset($test1[$id]);
}
else {
array_push($unique, $arr->id);
}
}
Try using array_unique($test1)
You can use array_intersect_key with array_unique and array_map for single line job:
In PHP >5.3.0:
$array = array (
0 => array ( "id" => 47523, "date" => "12-02-13 14:36:32" ),
1 => array ( "id" => 47523, "date" => "12-02-13 13:56:48" ),
2 => array ( "id" => 38639, "date" => "12-02-13 13:38:51" ),
3 => array ( "id" => 38639, "date" => "12-02-13 13:07:43" )
);
$array = array_intersect_key($array, array_unique(array_map(function($n){ return $n['id']; }, $array)));
print_r($array);
PHP <5.3.0
$array = array (
0 => array ( "id" => 47523, "date" => "12-02-13 14:36:32" ),
1 => array ( "id" => 47523, "date" => "12-02-13 13:56:48" ),
2 => array ( "id" => 38639, "date" => "12-02-13 13:38:51" ),
3 => array ( "id" => 38639, "date" => "12-02-13 13:07:43" )
);
function mapArray($n){
return $n['id'];
}
$array = array_intersect_key($array, array_unique(array_map("mapArray", $array)));
print_r($array);
You should write your own function beacuse array_unique is not for multidimensional arrays.
function array_unique_md($array)
{
$temp = array();
foreach($array as $key => $value)
{
if(!isset($temp[$value['id']])) $temp[$value['id']] = $value['date'];
}
$your_structure = array();
foreach($temp as $key=> $value)
{
$your_structure[] = array('id'=>$key,'date'=>$value);
}
return $your_structure;
}
You can use array_reduce
$data = array_reduce($data, function($a,$b){
isset($a[$b['id']]) or $a[$b['id']] = $b;
return $a;
});
var_dump(array_values($data));
See Live Demo
here's the result of my first function:
Array
(
[0] => Array
(
[MaidID] => 13
[Stores] => Array
(
[0] => 14
[1] => 5
)
)
[1] => Array
(
[MaidID] => 3
[Stores] => Array
(
[0] => 4
)
)
[2] => Array
(
[MaidID] => 41
[Stores] => Array
(
[0] => 14
)
)
)
Then, here's the result of my second function:
Array
(
[1] => Array
(
[MaidID] => 14
[Cash] => 10000
[Debit] => 0
[Credit] => 0
)
)
and here's should be the result:
Array ([0] => Array (
[MaidID] => 14
[Cash] => 10000.00
[Debit] => 0
[Credit] => 0
[MaidsID] => Array(
[0] => 13
[1] => 41
)
)
)
Is it possible to make it?I need to a new key name MaidsID pointing to the list of MaidID owned by more than one Stores.Please help me, Please be patience in my question, im just a beginner.Thank you so much.
this code work ok. $a is your first array $b is the second, $c is result
$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);`
I tested it here and it was ok. (Just replace $a with your first array and $b with seccond ).
Are you sure that the structure of your arrays is exactly as you have written above?? Maybe there is any problem with that.
You have puted array inside another array? (its not needed i think)
Howerver: For this code:
`$a = array (array('Maid' => 1, 'Stores' => array (1,5) ), array('Maid' => 3, 'Stores' => array (4) ), array('Maid' => 4, 'Stores' => array (1) ));
$b = array (array('Maid' => 1, 'Cash' => 10000, 'Debit' => 0, 'Credit' => 0));
print_r($a);
echo "<br><br>================================================<br><br>";
print_r($b);
echo "<br><br>================================================<br><br>";
$MaidsID=array();
foreach ($a as $aa ){
if (count($aa['Stores']>1)){
array_push($MaidsID, $aa['Maid']);
}
}
$MaidsID=array('MaidsID' => $MaidsID);
$c = array_merge($b, $MaidsID);
print_r($c);
echo "<br><br>================================================<br><br>";`
The output is:
Array ( [0] => Array ( [Maid] => 1 [Stores] => Array ( [0] => 1 [1] => 5 ) ) [1] => Array ( [Maid] => 3 [Stores] => Array ( [0] => 4 ) ) [2] => Array ( [Maid] => 4 [Stores] => Array ( [0] => 1 ) ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) )
================================================
Array ( [0] => Array ( [Maid] => 1 [Cash] => 10000 [Debit] => 0 [Credit] => 0 ) [MaidsID] => Array ( [0] => 1 [1] => 3 [2] => 4 ) )
================================================
Isn't this how you want the result?
Have a look at this. Maybe this can help you.
$result = array_merge($array1, $array2);