I have a PHP array that I am trying to split into 2 different arrays into that particular array. I am trying to pull out any values for each "destination" and "balance".
Here is the array i get,
Array
(
[destination_1] => 1
[balance_1] => 1
[destination_2] => 2
[balance_2] => 2
[destination_3] => 3
[balance_3] => 3
)
I need output like,
Array (
[0] => Array (
[destination_1] => 1
[balance_1] => 1
)
[1] => Array (
[destination_2] => 2
[balance_1] => 2
)
[2] => Array (
[destination_3] => 3
[balance_3] => 3
)
)
What you need is array_chunk()
$arr = [
"destination_1" => 1,
"balance_1" => 1,
"destination_2" => 2,
"balance_2" => 2,
"destination_3" => 3,
"balance_3" => 3
];
$result = array_chunk($arr, 2, true);
Output:
Array
(
[0] => Array
(
[destination_1] => 1
[balance_1] => 1
)
[1] => Array
(
[destination_2] => 2
[balance_2] => 2
)
[2] => Array
(
[destination_3] => 3
[balance_3] => 3
)
)
Related
I have this array
Array
(
[0] => Array
(
[id] => 1
[job_id] => 100
)
[1] => Array
(
[id] => 2
[job_id] => 100
)
[2] => Array
(
[id] => 3
[job_id] => 101
)
)
Now here how to check if "job_id" is different. eg all have same job_id or not and how many different job_id array have.
Thanks
Try this using array function:
$data = Array
(
[0] => Array
(
[id] => 1
[job_id] => 100
)
[1] => Array
(
[id] => 2
[job_id] => 100
)
[2] => Array
(
[id] => 3
[job_id] => 101
)
)
$totalDifferentJobId = count(array_unique(array_column($data, 'job_id')));
It will give you total unique job
Well, if it is not a too large array, you can do the following:
<?php
$arr = [
['id' => 1, 'job_id' => 100],
['id' => 2, 'job_id' => 100],
['id' => 3, 'job_id' => 101]
];
$jobids = array_unique(array_column($arr, 'job_id'));
var_dump($jobids);
This looks like a db-result, in wich case it would be better to use a group-by statement?
I'd like to merge two arrays on same key. I've tried my best to do it but unable to get success. I've added both the Arrays below. Kindly check both the Arrays in detail and help me how to merge it using same key.
Here's the 1st array :
Array
(
[1] => Array
(
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[costprice3] => 700
[margin3] => 25
)
)
Here's the 2 array :
Array
(
[1] => Array
(
[entityType1] => Products1
)
[2] => Array
(
[entityType2] => Products2
)
[3] => Array
(
[entityType3] => Products3
)
)
i want to need like that array please suggestion me
Array
(
[1] => Array
(
[entityType1] => Products1
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[entityType2] => Products2
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[entityType3] => Products3
[costprice3] => 700
[margin3] => 25
)
)
please help me how to merge two array
Try this :
foreach($array1 as $key => $value) {
$array1[$key]['entityType'.$key] = $array2[$key]['entityType'.$key];
}
print_r($array1);
<?php
$array1 = [
1 => [
'costprice1' => 500,
'margin1' => 20
],
2 => [
'costprice2' => 600,
'margin2' => 15
],
3 => [
'costprice2' => 700,
'margin2' => 25
],
];
$array2 = [
1 => ['entityType1' => 'Products1'],
2 => ['entityType2' => 'Products2'],
3 => ['entityType3' => 'Products3'],
];
array_walk($array2, function(&$v, $k)use($array1){
$v = array_merge($v, $array1[$k]);
});
print_r($array2);
Output:
Array
(
[1] => Array
(
[entityType1] => Products1
[costprice1] => 500
[margin1] => 20
)
[2] => Array
(
[entityType2] => Products2
[costprice2] => 600
[margin2] => 15
)
[3] => Array
(
[entityType3] => Products3
[costprice2] => 700
[margin2] => 25
)
)
https://eval.in/618561
I have two arrays like the following:
Array1
Array ( [price] => 117.00 [recurring_profile] => [use_config_gift_message_available] => 1 [stock_data] => Array ( [use_config_manage_stock] => 1 [original_inventory_qty] => 100 [qty] => 100 [use_config_min_qty] => 1 [use_config_min_sale_qty] => 1 [use_config_max_sale_qty] => 1 [is_qty_decimal] => 0 [is_decimal_divided] => 0 [use_config_backorders] => 1 [use_config_notify_stock_qty] => 1 [use_config_enable_qty_increments] => 1 [use_config_qty_increments] => 1 [is_in_stock] => 1 ) [website_ids] => Array ( [0] => 1 ) [can_save_configurable_attributes] => [can_save_custom_options] => [can_save_bundle_selections] => [type_has_options] => [type_has_required_options] => )
Array2
Array ( [price] => 118.0000 )
I use
$newarr =array_intersect_assoc($oldValues, $newValues);
but $newarr will be blank, any ideas?
My expected results:
$newArray1 = Array ( [price] => 117.00 );
It's blank, because price has different values 117.00 and 118.0000
You can try array_intersect_key
array_intersect_assoc() will only return items where BOTH the key and value match.
I'm a beginner at php and was searching for a solution all day long without success.
I have the following array:
$data = Array
(
[0] => Array
(
[my_id] => 1
[my_post_id] => 123
[my_status] => 1
[my_rating] => 5
)
[1] => Array
(
[my_id] => 2
[my_post_id] => 123
[my_status] => 1
[my_rating] => 4
)
[2] => Array
(
[my_id] => 3
[my_post_id] => 123
[my_status] => 1
[my_rating] => 5
)
[3] => Array
(
[my_id] => 4
[my_post_id] => 456
[my_status] => 1
[my_rating] => 5
)
[4] => Array
(
[my_id] => 5
[my_post_id] => 456
[my_status] => 1
[my_rating] => 3
)
)
and would like to merge the arrays with the same 'my_post_id' and count the values for 'my_status' and 'my_rating' which have the same 'my_post_id'.
At the end, I would like to have the following array:
$data = Array
(
[0] => Array
(
[my_post_id] => 123
[my_status] => 3
[my_rating] => 14
)
[1] => Array
(
[my_post_id] => 456
[my_status] => 2
[my_rating] => 8
)
)
I could get arrays with unique 'my_post_id' with the following code but I couldn't find out how to count the other values.
$out = array();
foreach( $data as $row ) {
$out[$row['my_post_id']] = $row;
}
$array = array_values( $out );
Any help would be much appreciated.
Daniel
This will produce the array you are looking for:
$out = array();
foreach( $data as $row ) {
if (!isset($out[$row['my_post_id']])) {
$out[$row['my_post_id']] = Array( "my_id"=>$row['my_id'],
"my_status" => $row["my_status"],
"my_rating" => $row["my_rating"]);
}
else {
$out[$row['my_post_id']]["my_status"] += $row["my_status"];
$out[$row['my_post_id']]["my_rating"] += $row["my_rating"];
}
}
results in:
Array
(
[123] => Array
(
[my_id] => 1
[my_status] => 3
[my_rating] => 14
)
[456] => Array
(
[my_id] => 4
[my_status] => 2
[my_rating] => 8
)
)
try the following - untested code
foreach($data as $key => $val){
if(!array_search($val['my_post_id'],$newArray)){
$newArray[]=array('my_post_id' => $val['my_post_id'],
'my_status' => $val['my_status'],
'my_rating' => $val['my_rating']);
}else{
$myIndex=array_search($val['my_post_id'],$newArray);
$newArray[$myIndex]['my_status']+=$val['my_status'];
$newArray[$myIndex]['my_rating']+=$val['my_rating'];
}
}
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);