php - array_combine or array_merge? - php

I'm sure this is easy for someone well-versed in php, but I've made the mistake of overloading my brain, so now I'm really confused as to whether I should use array_combine, array_merge, or something else... I've been googling and reading php.net for 4 hours and I think I'm just confusing myself even more...
Essentially, I just want to combine an array while keeping the keys?
//Here are the original arrays
[field_sreference] => Array
(
[0] => Array
(
[nid] => 28
)
[1] => Array
(
[nid] => 28
)
[2] => Array
(
[nid] => 29
)
)
[field_idelta] => Array
(
[0] => Array
(
[value] => 0
)
[1] => Array
(
[value] => 1
)
[2] => Array
(
[value] => 0
)
)
[field_iswitch] => Array
(
[0] => Array
(
[value] => 0
)
[1] => Array
(
[value] => 0
)
[2] => Array
(
[value] => 0
)
)
//Here is what I'm trying to achieve:
[combinedarray] => Array
(
[0] => Array
(
[nid] => 28
[idelta] => 0
[iswitch] => 0
)
[1] => Array
(
[nid] => 28
[idelta] => 1
[iswitch] => 0
)
[2] => Array
(
[nid] => 29
[idelta] => 0
[iswitch] => 0
)
)

You can solve this is O(n) by simply iterating the arrays...
$combinedarray = array();
$len = count($field_sreference);
for ($i = 0; $i < $len; $i++) {
$combinedarray[] = array("nid" => $field_sreference[$i]['nid'],
"idelta" => $filed_idelta[$i]['value'],
"iswitch" => $field_iswitch[$i]['value']);
}
This assumes, the 3 arrays are all of equal length.

A bit quickly typed, but this should work:
$result = array();
foreach ($arrays as $array)
{
foreach ($array as $index => $data)
{
$result[$index] += $data;
}
}
As you have not provided some input array in some easy form, you need to test it on your own. Let's say it's pseudo-code and I leave it here as an exercise. The + operator is the array union operator.

Related

Find if two different keys have the same value in 2 arrays PHP

My arrays are:
Array1
(
[0] => Array
(
[id] => 2
[name] => Melamine
[deleted] => 0
)
[1] => Array
(
[id] => 4
[name] => Vinyl
[deleted] => 0
)
[2] => Array
(
[id] => 5
[name] => Polyu
[deleted] => 0
)
)
Array2
(
[0] => Array
(
[productFinish] => 29
[type] => 2
)
[1] => Array
(
[productFinish] => 29
[type] => 4
)
)
So, i would like to return first array if id of 1st array matches with type of another array. In this case, first 2 indexes of first array must come out in return.
Thanks
You can use array_uintersect to get the results you want, supplying a callback function that compares the id value in array1 with the type value in array2:
$result = array_uintersect($array1, $array2, function ($a1, $a2) {
return ($a1['id'] ?? $a1['type']) - ($a2['type'] ?? $a2['id']);
});
print_r($result);
Note that because the callback is also called with values exclusively from $array1 or $array2 (for sorting), we have to allow for that in the comparison expression.
Output:
Array
(
[0] => Array
(
[id] => 2
[name] => Melamine
[deleted] => 0
)
[1] => Array
(
[id] => 4
[name] => Vinyl
[deleted] => 0
)
)
Demo on 3v4l.org
Ok, i got it with for loop.
$newTypeFilter = [];
for($i=0; $i < count($arra1); $i++){
for($j=0;$j<count($arra2); $j++){
if($arra1[$i]['id'] == $arra2[$j]['type']){
$newTypeFilter[] = $arra1[$i];
}
}
}
Any other answers will be appreciated. Thanks

Collecting values from a multidimensional array using foreach loops

There is an array which is built looks like the following (with some more values which i left away for this example):
Array
(
[0] => Array
(
[id] => 44
[cars] => Array
(
[0] => Array
(
[id] => 38
)
[1] Array
(
[id] => 39
)
)
)
[1] => Array
(
[id] => 45
[cars] => Array
(
[0] => Array
(
[id] =>136
)
[1] =>Array
(
[id] =>137
)
[2] =>Array
(
[id] =>138
)
)
)
)
I want to build another array from the above in the following form:
Array
(
[0] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 38
)
[1] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 39
)
[2] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 136
)
[3] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 137
)
[4] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 138
)
)
I tried to achieve this with following function:
foreach($filterSortSaveArray as $filterSortSaveArray['cars'] => $value){
$id = $filterSortSaveArray['id'];
foreach($value['cars'] as $value => $car){
$field_values['car_filter_sort_id'] = $id;
$field_values['car_id'] = $car['id'];
}
}
But the the result differs from what I have expected. Any suggestions?
There are two big issues in your code. First, are you referencing undefined value with $filterSortSaveArray['cars'], since there is no 'cars' key in the first level of the original array. Second, by assigning values to $field_values['car_filter_sort_id'] and $field_values['car_id'] in the loop you are just overriding them in each iteration. You need to push the values into an array using []= operator (which is equivalent to applying array_push()).
Try this:
$result = [];
foreach($filterSortSaveArray as $k => $v) {
if (!is_array($v['cars']))
continue;
$id = $v['id'];
foreach ($v['cars'] as $i => $car){
$result[] = [
'car_filter_sort_id' => $id,
'car_id' => $car['id']
];
}
}

how to get the nested array count with key values seperated in php

This is my array
Array
(
[2] => Array
(
[0] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
[1] => Array
(
[id] => 2
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:38 AM
[status] => 0
)
)
[1] => Array
(
[0] => Array
(
[id] => 1
[res_id] => 1
[grand_total] => 303.42
[time] => 2016-07-28 11:04:17 AM
[status] => 0
)
)
)
From this I need sub array count i.e., the array having two indexes such as 2 & 1 from this 2 & 1 there are some nested arrays found such as 0 & 1 for each
Here,I need array count as follows
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
How should I get this..
Someone help me out of this...
Thank you..
It is very easy foreach your array and use count or sizeof function.
$desiredArray = array();
foreach ($myarray as $key => $value) {
$desiredArray [$key] ['count'] = sizeof ($value);
}
print_r ($desiredArray);
The output will be as your desired output
Array
(
[2] => Array
(
[count] = 2
)
[1] => Array
(
[count] = 1
)
)
It's simple, and is better to create new array where you can save count of elements of main array items:
$counts = array();
foreach ($array as $k => $values) {
$counts[$k] = count($values);
}
print($counts); // gives desired result
Also you don't need to have extra array for the $counts array, what you get is:
array (
2 => 2,
1 => 1
)

count same categories from array and Store in new array with its count and category name

I have one array which I am getting from database query response.
Now I want to count same categories and print in option_name array.
I have tried it with different array functions but want get desire output.
I have one idea to take new array and push it with foreach loop but not much idea of how can i achieve using code. Please help me to solve it.
Array
(
[0] => Array
(
[CNC] => Array
(
[id] => 5
[category_id] => 68
)
[GVO] => Array
(
[option_name] => Actors
)
)
[1] => Array
(
[CNC] => Array
(
[id] => 11
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[2] => Array
(
[CNC] => Array
(
[id] => 3
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[3] => Array
(
[CNC] => Array
(
[id] => 4
[category_id] => 74
)
[GVO] => Array
(
[option_name] => Musician
)
)
[4] => Array
(
[CNC] => Array
(
[id] => 7
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
[5] => Array
(
[CNC] => Array
(
[id] => 6
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
)
Desire Output:
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)
Thanks in advance!
You simply need to loop through the array using foreach like as
$result = [];
foreach($arr as $k => $v){
if(isset($result[$v['GVO']['option_name']])){
$result[$v['GVO']['option_name']] += 1;
}else{
$result[$v['GVO']['option_name']] = 1;
}
}
print_R($result);
You can count the option_name values by incrementing a counter in an associative array where the key is the option_name:
$counts = [];
foreach($array as $v) {
if(!isset($counts[$v['GVO']['option_name']])) {
$counts[$v['GVO']['option_name']] = 0;
}
$counts[$v['GVO']['option_name']]++;
}
print_r($counts);
Try this:
$new_arr = array();
foreach(array_column($your_arr, 'option_name') as $value){
if(in_array($value, $new_array)){
$new_array[$value] = $new_array[$value]+1;
}else{
$new_array[$value] = 1;
}
}
Output
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)

how to merge key array on array

and i got a problem (its big for me) :(
ok, i have some array like ...
Array(
[0] => Array
(
[id] => 1
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
[3] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[4] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
how to merge them if the array have same key value ... ?
the result that i need got is like this ...
Array(
[0] => Array
(
[id] => 1
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
)
)
[2] => Array
(
[id] => 2
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
)
)
im very need some help here, and im working on PHP ... what method should i do for this case?
i try too searching on google and in here ... but i dont know the keyword >.<
Many thanks before :)
regard, Stecy
Try like this:
<?php
$result = array();
foreach ($my_array as $v) {
$id = $v['id'];
$result[$id]['id'] = $id;
$result[$id]['detail'][] = array(
'order_sn' => $v['order_sn'],
'total' => $v['total'],
);
}
You can just loop over the array and build a resulting one:
// $a is your array
$r=array();
foreach($a as $v)
$r[$v['id']][]=array('order_sn'=>$v['order_sn'], 'total'=>$v['total']);
echo'<pre>';
var_dump($r);
Since you do the paring by ID, it is wise to have it as the key and all the data associated with it as the value. There's no need to also have id and detail.
foreach($origianlArray as $key => $value){
$newArray[$value['id']]['id'] = $value['id'];
$newArray[$value['id']]['detail'][] = array('order_sn' => $value['order_sn'], 'total' => $value['total']);
}
Check out the PHP array_merge function.

Categories