Merge 2 array same key - php

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

Related

How can i get seperate values of array into same array?

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
)
)

How to check is php array have different value of key

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?

Remove specific string from whole associative array items

I have an associative array. I want to remove specific string from whole array items. Here is the structure of my array:
Array
(
[50808] => Array
(
[quantity] => 2
[name] => asv (test)
)
[50809] => Array
(
[quantity] => 2
[name] => 37 (test)
)
[50810] => Array
(
[quantity] => 3
[name] => 38 (test)
)
)
Output i want:
Array
(
[50808] => Array
(
[quantity] => 2
[name] => asv
)
[50809] => Array
(
[quantity] => 2
[name] => 37
)
[50810] => Array
(
[quantity] => 3
[name] => 38
)
)
I know its very simple using loop but i want to do it without loop.
Use array_map function
<?php
$array = Array
(
'50808' => Array
(
'quantity' => 2,
'name' => 'asv (test)',
),
'50809' => Array
(
'quantity' => 2,
'name' => '37 (test)'
),
'50810' => Array
(
'quantity' => 3,
'name' => '38 (test)'
)
);
echo '<pre>';
$new_array = array_map(function($val){
$val['name'] = trim(str_replace('(test)', '', $val['name']));
return $val;
}, $array);
print_r($new_array);
Output:
Array
(
[50808] => Array
(
[quantity] => 2
[name] => asv
)
[50809] => Array
(
[quantity] => 2
[name] => 37
)
[50810] => Array
(
[quantity] => 3
[name] => 38
)
)
Another method can be array_walk function:
array_walk($array, function(&$val){
$val['name'] = trim(str_replace('(test)', '', $val['name']));
});
print_r($array); //out put: desired output

How to manipulate php arrays [duplicate]

This question already has answers here:
Group rows in an associative array of associative arrays by column value and preserve the original first level keys
(7 answers)
Closed 7 years ago.
How to manipulate php arrays. I have a $data variable below.
$data = Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => users
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => users
)
[2] => Array
(
[id] => 3
[capacity] => 900
[category] => students
)
[3] => Array
(
[id] => 4
[capacity] => 300
[category] => students
)
)
Now I want to group the data with same category like below. . I am not really familiar in php. Can somebody out there help me how to achieve this. What function should I used. Thanks
Array
(
[users] => Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => users
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => users
)
),
[students] => Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => students
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => students
)
)
)
Just iterate over the array and add it depending on the content of the category field to a new array:
$new = array();
foreach ($data as $val) {
$new[$val['category']][] = $val;
}
This does what you requested
<?php
$data = array(
0 => array (
"id" => 1,
"capacity" => 900,
"category" => "users"
),
1 => array (
"id" => 2,
"capacity" => 300,
"category" => "users"
),
2 => array (
"id" => 3,
"capacity" => 900,
"category" => "students"
),
3 => array (
"id" => 4,
"capacity" => 300,
"category" => "students"
)
);
$groups = array();
foreach ($data as $i) {
if ($i['category'] === "students") {
$groups['students'][] = $i;
}
else if ($i['category'] === "users") {
$groups['users'][] = $i;
}
}
echo "<pre>", print_r($groups), "</pre>";
Here is a working demo - http://codepad.viper-7.com/G4Br28

How to Add a new Key and Merge the array values in multidimemsional array PHP

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

Categories