i have 2 arrays i want to display the final array as what are the array element in $displayArray only be displayed from the $firstArray
$firstArray = Array
(
[0] => Array
(
[Dis_id] => Dl-Dis1
[Dis_Desc] => Discount
[Dis_Per] => 7.500
[Dis_val] => 26.25
)
[1] => Array
(
[Dis_id] => Dl-Dis2
[Dis_Desc] => Discount
[Dis_Per] => 2.500
[Dis_val] => 8.13
)
)
$displayArray = Array
(
[0] => Array
(
[0] => Dis_id
[1] => Dis_val
)
)
i want the final output will be
$resultArray = Array
(
[0] => Array
(
[Dis_id] => Dl-Dis1
[Dis_val] => 26.25
)
[1] => Array
(
[Dis_id] => Dl-Dis2
[Dis_val] => 8.13
)
)
Both the $firstArray and the $DisplayArray are dynamic but the $displayArray should be one.
i dont know how to do give me any suggestion
First up, if $displayArray will never have more than one array, the answer is pretty simple. Start by popping the inner array, to get to the actual keys you will need:
$displayArray = array_pop($displayArray);//get keys
$resultArray = array();//this is the output array
foreach ($firstArray as $data)
{
$item = array();
foreach ($displayArray as $key)
$item[$key] = isset($data[$key]) ? $data[$key] : null;//make sure the key exists!
$resultArray[] = $item;
}
var_dump($resultArray);
This gives you what you need.
However, if $displayArray contains more than 1 sub-array, you'll need an additional loop
$resultArray = array();
foreach ($displayArray as $k => $keys)
{
$resultArray[$k] = array();//array for this particular sub-array
foreach ($firstArray as $data)
{
$item = array();
foreach ($keys as $key)
$item[$key] = isset($data[$key]) ? $data[$key] : null;
$resultArray[$k][] = $item;//add data-item
}
}
var_dump($resultArray);
the latter version can handle a display array like:
$displayArray = array(
array(
'Dis_id',
'Dis_val'
),
array(
'Dis_id',
'Dis_desc'
)
);
And it'll churn out a $resultArray that looks like this:
array(
array(
array(
'Dis_id' => 'foo',
'Dis_val' => 123
)
),
array(
array(
'Dis_id' => 'foo',
'Dis_desc' => 'foobar'
)
)
)
Job done
Related
I have 2 arrays, I'm trying to find any matches and return 'url from $array_full.
I tried array_intersect($array_full, $array_ids), but it doesn't work.
$array_full = array
(
Array
(
'#attributes' => Array
(
'topicid' => 102000,
'url' => 'Velkommen.htm',
'alias' => 'Velkommen'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130313,
'url' => 'WStation/WAS_Indstillinger.htm',
'alias' => 'WAS_Indstillinger'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130315,
'url' => 'SPedestal/Applikationer/LoadSharing/Indstillinger.htm',
'alias' => 'LOS_Indstillinger'
)
),
Array
(
'#attributes' => Array
(
'topicid' => 130312,
'url' => 'WStation/WAS_Indstillinger.htm',
'alias' => 'WAS_Indstillinger'
)
)
);
$array_ids = array('130312', '130315');
I expect to get an array of matched url's, like:
array('WStation/WAS_Indstillinger.htm','SPedestal/Applikationer/LoadSharing/Indstillinger.htm')
A simple couple of foreach loops seems the easiest approach
$results = [];
foreach ( $array_full as $a ) {
foreach ( $a as $item ) {
if ( in_array($item['topicid'], $array_ids) ) {
$results[] = $item['url'];
}
}
}
print_r($results);
RESULT
Array
(
[0] => SPedestal/Applikationer/LoadSharing/Indstillinger.htm
[1] => WStation/WAS_Indstillinger.htm
)
You will have to make foreach inside foreach to find item that is matching to ID.
Something like this (not tested, may contain some typos).
foreach($array_ids as $id) {
foreach($array_full as $key => $fullItem) {
if($fillItem['#attributes']['topicid'] != $id) {
continue;
}
//do what you need with $fullItem array
$key; // this is the key you want
}
}
you can use array_map, in_array to get the URL's
$result = [];
array_map(function($v) use ($array_ids,&$result){
$result[] = in_array($v['#attributes']['topicid'], $array_ids) ? $v['#attributes']['url'] : '';
}, $array_full);
Result:-
echo '<pre>';
print_r(array_filter($result));
Array
(
[2] => SPedestal/Applikationer/LoadSharing/Indstillinger.htm
[3] => WStation/WAS_Indstillinger.htm
)
im having trouble figuring this one out, the scenario is, that there is an multidimensional array, in which there is name, id, and number in an array, what i want is to get the unique array in which array having same name should not appear, this can be done and i had done it, but i also want that the array that is returned should contain the lowest num, i hope this would help make you understand
i have
Array(
[0]=>array(
[id]=>1
[name]=>abc
[num]=>4)
[1]=>
array(
[id]=>2
[name]=>efg
[num]=>4)
[2]=>array(
[id]=>3
[name]=>abc
[num]=>2)
)
Now its a rough array representation, what i want from this is
Array(
[0]=>array(
[id]=>3
[name]=>abc
[num]=>2)
[1]=>
array(
[id]=>2
[name]=>efg
[num]=>4)
What im using:
code.php
<?php
$details = array(
0 => array("id"=>"1", "name"=>"Mike", "num"=>"9876543210"),
1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"),
2 => array("id"=>"1", "name"=>"Mathew", "num"=>"784581254"),
);
function unique_multidim_array($array, $key) {
$temp_array = array();
$i = 0;
$key_array = array();
foreach($array as $val) {
if (!in_array($val[$key], $key_array)) {
$key_array[$i] = $val[$key];
$temp_array[$i] = $val;
}
$i++;
}
return $temp_array;
}
$details = unique_multidim_array($details,'name');
?>
This function returns me
Array(
[0]=>array(
[id]=>1
[name]=>abc
[num]=4)
[1]=>
array(
[id]=>2
[name]=>efg
[num]=>4)
You can use array_reduce:
$result = array_reduce
(
$array,
function( $carry, $item )
{
if( !isset( $carry[$item['name']] ) || $item['num'] < $carry[$item['name']]['num'] )
{
$carry[$item['name']] = $item;
}
return $carry;
},
[] // This parameter (empty array) is optional in this case
);
$result = array_values( $result );
We compare each array element with constructing returned array (initially empty): if in returned array doesn't exists an item with key = $item['name'], we add it; otherwise, if current item has num value lower than corresponding returned array item, we replace this item with current item. At the end, we use array_values to remove associative keys.
Result:
Array
(
[0] => Array
(
[id] => 3
[name] => abc
[num] => 2
)
[1] => Array
(
[id] => 2
[name] => efg
[num] => 4
)
)
Read more about array_reduce
Read more about array_values
I would do this with two loops
First, group the values by name, then number
foreach ($your_array as $value) {
$names[$value['name']][$value['num']] = $value;
}
Then iterate the result of that, sort name by key (num) and append the first element to the final result.
foreach ($names as $set_of_nums) {
ksort($set_of_nums);
$result[] = reset($set_of_nums);
}
I have considered the below example.
$details = array(
0 => array("id"=>"1", "name"=>"Mike", "num"=>"123"),
1 => array("id"=>"2", "name"=>"Carissa", "num"=>"235"),
2 => array("id"=>"3", "name"=>"Mike", "num"=>"5"),
3 => array("id"=>"4", "name"=>"Tom", "num"=>"256"),
4 => array("id"=>"5", "name"=>"Tom", "num"=>"500"),
5 => array("id"=>"6", "name"=>"Mike", "num"=>"7"),
);
function unique_multidim_array($array, $key) {
$temp_array = array();
$key_array = array();
foreach($array as $k=>$val) {
if (!in_array($val[$key], $key_array)) {
// storing the array with the key
$key_array[$k] = $val[$key];
$temp_array[$k] = $val;
} else{
foreach($key_array as $r=>$p){
//check for the array with the name
if($temp_array[$r]['name'] == $val['name']){
// compare the value
if($temp_array[$r]['num']>$val['num']){
// store the new array to the temp_array with same key of key_array
$temp_array[$r] = $val;
}
}
}
}
}
return $temp_array;
}
$details = unique_multidim_array($details,'name');
Output:
Array
(
[0] => Array
(
[id] => 3
[name] => Mike
[num] => 5
)
[1] => Array
(
[id] => 2
[name] => Carissa
[num] => 235
)
[3] => Array
(
[id] => 4
[name] => Tom
[num] => 256
)
)
You can sort the output array using any of the sort functions as you desire.
I've received and XML, converted it into an array for usage.
The XML comes in unpredictable multi dimension when I convert it into array.
Had been looking around but could not find a suitable solution.
An alternative is to simplify the converted array.
I've converted an XML to array in PHP, and the result looked like this:
Array
(
[GetMLCBRes] => Array
(
[0] => Array
(
[Ord] => Array
(
[0] => Array
(
[OrdId] => Array
(
[0] => DP Order ID
)
)
)
[ReqInf] => Array
(
[0] => Array
(
[ReqDat] => Array
(
[0] => Date of Request
)
)
)
[Res] => Array
(
[0] => PDF Report
)
)
)
)
May I know how to drop the index like [0] but remain the assoc keys like [Ord], [OrdId], [ReqInf] and [Res], etc.
How to convert it to become like this?
Array
(
[GetMLCBRes] => Array
(
[Ord] => Array
(
[OrdId] => DP Order ID
)
[ReqInf] => Array
(
[ReqDat] => Date of Request
)
[Res] => PDF Report
)
)
it works but if you change your structure maybe it won't. It's not optimized too :)
$input = Array(
'GetMLCBRes' => Array(Array(
'Ord' => Array(Array(
'OrdId' => Array('DP Order ID')
)),
'ReqInf' => Array(Array(
'ReqDat' => Array('Date of Request')
)),
'Res' => Array('PDF Report')
))
);
foreach($input as &$in){
$sub = $in[0];
foreach($sub as $key => &$value){
$sub2 = $value[0];
if(!is_array($sub2)){
$sub[$key] = $sub2;
continue;
}
$final2 = array();
foreach($sub2 as $key2 => $final)
$final2[$key2] = $final[0];
$sub[$key] = $final2;
}
$in = $sub;
}
var_dump($input);
You can test it here : http://sandbox.onlinephpfunctions.com/code/a6770c7649d7d277aa1dc3544093cc87bed0951d
This should work as expected :
function recursive_skip(array $ary) {
foreach($ary as $k => &$v) {
if (is_array($v)) {
// Skip it
$v = $v[0];
}
if (is_array($v)) {
// If current array item is an array itself, recursively call the function on it
$v = recursive_skip($v);
}
}
// Return updated array to calling context
return $ary;
}
$source = Array(
'GetMLCBRes' => Array(Array(
'Ord' => Array(Array(
'OrdId' => Array('DP Order ID')
)),
'ReqInf' => Array(Array(
'ReqDat' => Array('Date of Request')
)),
'Res' => Array('PDF Report')
))
);
$dest = recursive_skip($source);
var_dump($dest);
A few caveats : the function will only skip one array level each time (but could be adapted to handle more if needed) and might come with a significant performance cost if your source array is huge since it's recursive (O(n)), it just walks through the whole array tree.
I have the following array being returned
Array
(
[0] => Array
(
[uid] => 616941445
)
[1] => Array
(
[uid] => 1354124203
)
)
However I want just a single layered array, so i would like something like this.
Array
(
[0] => 616941445
[1] => 1354124203
)
foreach ($arr as $key => $val) {
$arr[$key] = $val['uid'];
}
<?php
$multi_arr = array(
array(
'uid' => 616941445
),
array(
'uid' => 1354124203
),
);
$single_arr = array();
foreach($multi_arr as $arr){
foreach($arr as $val) $single_arr[] = $val;
}
?>
As always, when you need to change two level array into one level without preserve keys:
$your2DArray = array(/* .. */);
$flatArray = array_map('array_pop', $your2DArray);
And like you want to, no loops.
foreach($arr as $key=>$val) {
$single_arr[] = $arr[$key]['uid'];
}
I have a multidimensional array and am trying to group them according to the value in a specific column.
I'm trying to group them by level, but I won't actually know the level beforehand. So, it's not like I can put it in a for loop and say while $i < 7, because I won't know that 7 is the maximum value for the level key, and frankly, I'm not sure that's how I would need to do it even if I did.
[
['cust' => 'XT8900', 'type' => 'standard', 'level' => 1],
['cust' => 'XT8944', 'type' => 'standard', 'level' => 1],
['cust' => 'XT8922', 'type' => 'premier', 'level' => 3],
['cust' => 'XT8816', 'type' => 'permier', 'level' => 3],
['cust' => 'XT7434', 'type' => 'standard', 'level' => 7],
]
Desired result:
Array (
[1] => Array (
[0] => Array (
[cust] => XT8900
[type] => standard
)
[1] => Array (
[cust] => XT8944
[type] => standard
)
)
[3] => Array (
[2] => Array (
[cust] => XT8922
[type] => premier
)
[3] => Array (
[cust] => XT8816
[type] => permier
)
)
[7] => Array (
[4] => Array (
[cust] => XT7434
[type] => standard
)
)
)
Best way, if you have control over building the initial array, is just set things up like that at the start as you add entries.
If not then build a temporary array to sort:
foreach ($input_arr as $key => &$entry) {
$level_arr[$entry['level']][$key] = $entry;
}
Leaves you with the form you wanted and everything referenced together.
Build the array like that in the first place though if at all possible.
You need to group them by level first
Use foreach to loop into array check if the level is the same with the previous item then group it with that array
$templevel=0;
$newkey=0;
$grouparr[$templevel]="";
foreach ($items as $key => $val) {
if ($templevel==$val['level']){
$grouparr[$templevel][$newkey]=$val;
} else {
$grouparr[$val['level']][$newkey]=$val;
}
$newkey++;
}
print($grouparr);
The output of print($grouparr); will display like the format you hoped for
You can also try to
print($grouparr[7]);
Will display
[7] => Array (
[4] => Array (
[cust] => XT7434
[type] => standard
)
)
Or
print($grouparr[3]);
Will display
[3] => Array (
[2] => Array (
[cust] => XT8922
[type] => premier
)
[3] => Array (
[cust] => XT8816
[type] => permier
)
)
Here is the solution I landed on for an identical problem, wrapped as a function:
function arraySort($input,$sortkey){
foreach ($input as $key=>$val) $output[$val[$sortkey]][]=$val;
return $output;
}
To sort $myarray by the key named "level" just do this:
$myArray = arraySort($myArray,'level');
Or if you didn't want it as a function, just for a one time use, this would create $myNewArray from $myArray grouped by the key 'level'
foreach ($myArray as $key=>$val) $myNewArray[$val['level']][]=$val;
function group_assoc($array, $key) {
$return = array();
foreach($array as $v) {
$return[$v[$key]][] = $v;
}
return $return;
}
//Group the requests by their account_id
$account_requests = group_assoc($requests, 'account_id');
$result = array();
foreach ($yourArrayList as $data) {
$id = $data['level'];
if (isset($result[$id])) {
$result[$id][] = $data;
} else {
$result[$id] = array($data);
}
}
Best ans.
$levels = array_unique(array_column($records, 'level'));
$data = array();
foreach($records as $key => $value){
$data[$levels[array_search($value['level'],$levels )]][] = $value ;
}
print_r($data);
To generate the question's exact desured output from the sample input, pull/pop the last value from each row, use that value as the first level grouping key. Then use the original first level index as the second level key. Then push the two remaining elements into the group's subset.
Code: (Demo)
$result = [];
foreach ($array as $key => $row) {
$result[array_pop($row)][$key] = $row;
}
var_export($result);
For functional style syntax, use array_reduce(). (Demo)
var_export(
array_reduce(
array_keys($array),
function($result, $key) use ($array) {
$result[array_pop($array[$key])][$key] = $array[$key];
return $result;
}
)
);
function _group_by($array,$key,$keyName)
{
$return = array();
foreach($array as $val) {
$return[$keyName.$val[$key]][] = $val;
}
return $return;
} //end of function