I have this array of userids. I want to combine the child userid arrays into one array within the parent arrays.
[0] => Array
(
[0] => Array
(
[userid] => 1610
)
[1] => Array
(
[userid] => 1614
)
[2] => Array
(
[userid] => 1616
)
)
[1] => Array
(
[0] => Array
(
[userid] => 1610
)
[1] => Array
(
[userid] => 1614
)
[2] => Array
(
[userid] => 1616
)
[3] => Array
(
[userid] => 1618
)
)
My desired result would look like...
[0] => Array
(
[userids] => Array
(
[0] => 1610
[1] => 1614
[2] => 1616
)
)
[1] => Array
(
[userids] => Array
(
[0] => 1610
[1] => 1614
[2] => 1616
[3] => 1618
)
)
Loop the array and use array_column to flatten the subarrays.
foreach($arr as $sub){
$res[][key($sub[0]) . "s"] = array_column($sub, 'userid');
}
var_dump($res);
https://3v4l.org/A68VA
Related
My problem is that I want to sort array keys by ascending and I am googling last 2 days what I am getting the solution is uksort, ksort but it's not give the exact solution what am I want.I feel great full for helping to solve this issue. My output array is:-
Array
(
[2020-06-22,13:00] => Array
(
[0] => 1663
[1] => 1664
)
[2020-06-22,14:00] => Array
(
[0] => 1665
[1] => 1666
)
[2020-06-24,13:00] => Array
(
[0] => 1715
)
[2020-06-24,8:30] => Array
(
[0] => 1714
)
[2020-06-23,13:00] => Array
(
[0] => 1724
)
[2020-06-23,8:30] => Array
(
[0] => 1727
)
)
and we need array is in:-
Array
(
[2020-06-22,13:00] => Array
(
[0] => 1663
[1] => 1664
)
[2020-06-22,14:00] => Array
(
[0] => 1665
[1] => 1666
)
[2020-06-23,8:30] => Array
(
[0] => 1727
)
[2020-06-23,13:00] => Array
(
[0] => 1724
)
[2020-06-24,8:30] => Array
(
[0] => 1714
)
[2020-06-24,13:00] => Array
(
[0] => 1715
)
)
I want to insert extra key before of array if key exists and array is not multi dimensional for example:
Array
(
[0] => Array
(
[_key_] => Array
(
[0] => Array
(
[pub-id-type] => pmid
[value] => 25588809
)
[1] => Array
(
[pub-id-type] => pmc
[value] => 4302133
)
[2] => Array
(
[pub-id-type] => publisher-id
[value] => 1008
)
[3] => Array
(
[pub-id-type] => doi
[value] => 10.1186/s12885-015-1008-4
)
[type_s] => article-id
[id] => 58a6eeedeab2f
)
)
I want:
Array
(
[0] => Array
(
[_key_] => Array
(
[0]=>array(
[0] => Array
(
[pub-id-type] => pmid
[value] => 25588809
)
[1] => Array
(
[pub-id-type] => pmc
[value] => 4302133
)
[2] => Array
(
[pub-id-type] => publisher-id
[value] => 1008
)
[3] => Array
(
[pub-id-type] => doi
[value] => 10.1186/s12885-015-1008-4
)
[type_s] => article-id
[id] => 58a6eeedeab2f
)
)
)
recursively util n depath of _Key_ is found.
The layout is not the best, but howerver ...
try this one:
if(is_array($arr[0][_key_]))
{
$tmp = $arr[0][_key_];
unset($arr[0][_key_]);
$arr[0][_key_][] = $tmp;
}
How can i parse the below multi dimensional array ($array) and pull values of [productType] , [totalPrice]and [productCategory] if [packageCode] is matching with the value of $pkgcodes[1]...[z]
$pkgcodes is an array of codes
print_r of $pkgcodes
Array ( [0] => TRA1I2 [1] => TREZEC [n] ...)
The array $array is a response from SOAP client
print_r of $array
Array (
[0] => Array (
[packageCode] => TRA1I2
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Simple
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) )
[1] => Array (
[packageCode] => TREZEC
[totalPrice] => 17
[productType] => product Only
[products] => Array (
[0] => Array (
[productCategory] => Complicated
[paxes] => Array (
[0] => Array (
[paxType] => Adult
[age] => 30 )
[1] => Array (
[paxType] => Adult
[age] => 30 ) )
[totalproductRate] => 17
[ratesPerNight] => Array (
[0] => Array (
[date] => 2015-01-28
[amount] => 17 ) ) ) ) ) ).
You help is more appreciated
Try with -
$newArr = array();
foreach($array as $value) {
if (in_array($value['packageCode'], $pkgcodes)) {
$temp['productType'] = $value['productType'];
$temp['totalPrice'] = $value['totalPrice'];
$temp['packageCode'] = $value['packageCode'];
$temp['productCategory'] = $value['products']['productCategory'];
$newArr[] = $temp;
}
}
var_dump($newArr);
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.
I need to merge a PHP array, this array has 2 arrays into it named "targetXX", I can have 2 or more. Each target have the same keys, for each key I have an array with 2 values a and b, a is always the same in both targets, but I need to merge both B values like this:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
)
[2] => Array
(
[0] => 600
[1] => 1392282320
)
[3] => Array
(
[0] => 200
[1] => 1392282380
)
[4] => Array
(
[0] => 400
[1] => 1392282440
)
[5] => Array
(
[0] => 600
[1] => 1392282500
)
)
)
[1] => Array
(
[target] => hitcount(stats.asdf.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 4321
[1] => 1392282200
)
[1] => Array
(
[0] => 76567
[1] => 1392282260
)
[2] => Array
(
[0] => 5556
[1] => 1392282320
)
[3] => Array
(
[0] => 7675
[1] => 1392282380
)
[4] => Array
(
[0] => 2344
[1] => 1392282440
)
[5] => Array
(
[0] => 0999
[1] => 1392282500
)
)
)
Result:
Array
(
[0] => Array
(
[target] => hitcount(stats.asdf1.requests, "1min")
[datapoints] => Array
(
[0] => Array
(
[0] => 1200
[1] => 1392282200
[2] => 4321
)
[1] => Array
(
[0] => 1400
[1] => 1392282260
[2] => 76567
)
[2] => Array
(
[0] => 600
[1] => 1392282320
[2] => 5556
)
[3] => Array
(
[0] => 200
[1] => 1392282380
[2] => 7675
)
[4] => Array
(
[0] => 400
[1] => 1392282440
[2] => 2344
)
[5] => Array
(
[0] => 600
[1] => 1392282500
[2] => 0999
)
)
)
Use array_merge() to achieve this:
$newArray = array();
foreach ($myArray['target2'] as $key => $innerArr1) {
$newArray['target'][$key] = array_merge(
$myArray['target1'][$key], /* 0th and 1st index */
array($innerArr1[1]) /* 2nd index */
);
}
print_r($newArray);
Output:
Array
(
[target] => Array
(
[0] => Array
(
[0] => 333333
[1] => 13
[2] => 99
)
[1] => Array
(
[0] => 444444
[1] => 15
[2] => 98
)
[2] => Array
(
[0] => 555555
[1] => 17
[2] => 97
)
)
)
Demo
The built-in function array_merge may do the work for you. You need to merge each subarrays in fact, as the array_merge_recursive function doesn't handle indexes.
$newArray = array();
foreach ($myArray['target2'] as $key => $arr) {
$newArray['target'][$key] = array_merge($myArray['target1'][$key], $arr[1]);
}
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If you have more than 2 keys to merge, you can loop on the algorithm multiple times.