PHP - Merge more than 2 arrays - php

I'm trying to make the values in the EVENTNAME array the key of the arrays below it.
I want to be able to do a foreach on on a php variable and get the EVENTNAME and the count for its IMMIGRATIONS, EFFECTS and SUPPLYCHAINS.
$keyevents = oci_parse($conn, "SELECT EVENTNAME FROM KEYEVENTS GROUP BY EVENTNAME");
oci_execute($keyevents);
oci_fetch_all($keyevents, $keyevent);
$immigrations = oci_parse($conn, "SELECT COUNT(*) IMMIGRATIONS FROM KEYEVENTS LEFT JOIN IMMIGRATION ON KEYEVENTS.EVENTID = IMMIGRATION.EVENTID GROUP BY EVENTNAME");
oci_execute($immigrations);
oci_fetch_all($immigrations, $imm);
$effects = oci_parse($conn, "SELECT COUNT(*) EFFECTS FROM KEYEVENTS LEFT JOIN EFFECT ON KEYEVENTS.EVENTID = EFFECT.EVENTID GROUP BY EVENTNAME");
oci_execute($effects);
oci_fetch_all($effects, $eff);
$supplychains = oci_parse($conn, "SELECT COUNT(*) SUPPLYCHAINS FROM KEYEVENTS LEFT JOIN SUPPLYCHAINS ON KEYEVENTS.EVENTID = SUPPLYCHAINS.EVENTID GROUP BY EVENTNAME");
oci_execute($supplychains);
oci_fetch_all($supplychains, $supp);
$stats = array_merge($keyevent, $imm, $eff, $supp);
highlight_string("<?php\n\$stats =\n" . var_export($stats, true) . ";\n?>");
var_export
array (
'EVENTNAME' =>
array (
0 => 'Brexit',
1 => 'leave date set',
2 => 'leave date ',
3 => 'deal or no deal',
),
'IMMIGRATIONS' =>
array (
0 => '1',
1 => '1',
2 => '1',
3 => '1',
),
'EFFECTS' =>
array (
0 => '2',
1 => '1',
2 => '1',
3 => '2',
),
'SUPPLYCHAINS' =>
array (
0 => '1',
1 => '1',
2 => '1',
3 => '1',
),
)

I assume you don't know the key names and use array_keys to figure it out.
Then I loop the first subarray and use the key in array_column to get the other values.
Your expected output does not include associative keys in the subarray, but if you need it use the second code.
$arr = array ( 'EVENTNAME' => array ( 0 => 'Brexit', 1 => 'leave date set', 2 => 'leave date ', 3 => 'deal or no deal', ), 'IMMIGRATIONS' => array ( 0 => '1', 1 => '1', 2 => '1', 3 => '1', ), 'EFFECTS' => array ( 0 => '2', 1 => '1', 2 => '1', 3 => '2', ), 'SUPPLYCHAINS' => array ( 0 => '1', 1 => '1', 2 => '1', 3 => '1', ), );
$keys = array_keys($arr);
foreach($arr[$keys[0]] as $key => $val){
$result[$val] = array_column(array_slice($arr,1), $key);
}
var_export($result)
Output:
array (
'Brexit' =>
array (
0 => '1',
1 => '2',
2 => '1',
),
'leave date set' =>
array (
0 => '1',
1 => '1',
2 => '1',
),
'leave date ' =>
array (
0 => '1',
1 => '1',
2 => '1',
),
'deal or no deal' =>
array (
0 => '1',
1 => '2',
2 => '1',
),
)
https://3v4l.org/Seteg
$keys = array_keys($arr);
foreach($arr[$keys[0]] as $key => $val){
$result[$val] = array_combine(array_slice($keys, 1), array_column(array_slice($arr,1), $key));
}
var_export($result);
Output:
array (
'Brexit' =>
array (
'IMMIGRATIONS' => '1',
'EFFECTS' => '2',
'SUPPLYCHAINS' => '1',
),
'leave date set' =>
array (
'IMMIGRATIONS' => '1',
'EFFECTS' => '1',
'SUPPLYCHAINS' => '1',
),
'leave date ' =>
array (
'IMMIGRATIONS' => '1',
'EFFECTS' => '1',
'SUPPLYCHAINS' => '1',
),
'deal or no deal' =>
array (
'IMMIGRATIONS' => '1',
'EFFECTS' => '2',
'SUPPLYCHAINS' => '1',
),
)
https://3v4l.org/aPCub

Related

Overwrite rows in one array using another array when two columns are shared between the arrays

I got 2 arrays. In first one field number is empty:
array (
3 => array ( 0 => array ( 'id' => 1, 'number' => 0, 'time' => 40,), ),
4 => array ( 0 => array ( 'id' => 2, 'number' => 0, 'time' => 40, ), ),
5 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 40, ), ),
6 => array ( 0 => array ( 'id' => 1, 'number' => 0, 'time' => 41, ), ),
7 => array ( 0 => array ( 'id' => 2, 'number' => 0, 'time' => 41, ), ),
8 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 41, ), ),
)
In the second one fields number are not empty, however the array is bit different, because it doesn't have a 2 arrays in the middle (id = 3, time = 40 and id = 3, time = 41).
array ( 3 => array ( 'id' => '1', 'number' => '3785', 'time' => '40', ),
4 => array ( 'id' => '2', 'number' => '1574', 'time' => '40', ),
5 => array ( 'id' => '1', 'number' => '2954', 'time' => '41', ),
6 => array ( 'id' => '2', 'number' => '2463', 'time' => '41', ),
)
What I want to do is to some sort of merge of this arrays into one looking like this:
array (
3 => array ( 'id' => '1', 'number' => '3785', 'time' => '40', ),
4 => array ( 'id' => '2', 'number' => '1574', 'time' => '40', ),
5 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 40, ), ),
6 => array ( 'id' => '1', 'number' => '2954', 'time' => '41', ),
7 => array ( 'id' => '2', 'number' => '2463', 'time' => '41', ),
8 => array ( 0 => array ( 'id' => 3, 'number' => 0, 'time' => 41,), ),
)
I tried some variations of array_merge() and array_combine(), but neither of them seems to work, or perhaps I've been using them badly.
Again, notice how row 5 and 8 are not affected because (id = 3 and time = 40) and (id = 3 and time = 41), respectively, are not represented in the second array.
What I tried and it works, but it doesn't look well in my opinion:
foreach($arr2 as $wpis2){
foreach($arr as $key=>$wpis){
if($wpis2['id'] == $wpis[0]['id'] && $wpis2['time'] == $wpis[0]['time']){
$arr[$key] = $wpis2;
}
}
}
You will need to iterate over the first array in order to populate a key-map based on the combination of id and time values from all rows.
Use a second loop to iterate over the second array and access the correct key to overwrite by leveraging the mapping array.
Code: (Demo)
$map = [];
foreach ($first as $k => [['id' => $id, 'time' => $time]]) {
$map["{$id}_{$time}"] = $k;
}
foreach ($second as $row) {
$key = $map["{$row['id']}_{$row['time']}"];
$first[$key] = $row;
}
var_export($first);

how to access value form array

please find below contains array in php
$data = array ( 'questions' => array ( 1 =>
array (
'section_id' => '61',
'questionid' => '2035',
'time_spent' => '0',
),
2 =>
array (
'section_id' => '61',
'questionid' => '2036',
'time_spent' => '0',
),
3 =>
array (
'section_id' => '61',
'questionid' => '2037',
'time_spent' => '0',
),
),)
how can i access section_id, and time_spent data from array
Please use the below code : Also see this link
foreach($data as $key => $values){
foreach ($values as $ikey => $secArr) {
# code...
$section_id = $secArr['section_id'];
$time_spent = $secArr['time_spent'];
echo 'Section Id: '.$section_id.' | Time Spend:'.$time_spent.'</br>';
}
}
I hope it will helps you.

PHP Array GET parent and child

Hello I Have this Array from a dataset:
Example:
array (
1 =>
array (
'id' => '1',
'name' => ' Category',
'id_parent' => '0',
'ativo' => '1',
),
2 =>
array (
'id' => '2',
'name' => ' Slippers',
'id_parent' => '0',
'ativo' => '1',
),
3 =>
array (
'id' => '3',
'name' => ' TShirts',
'id_parent' => '0',
'ativo' => '1',
),
4 =>
array (
'id' => '4',
'name' => ' BlousesSweatshirt',
'id_parent' => '0',
'ativo' => '1',
),
5 =>
array (
'id' => '5',
'name' => ' Cap',
'id_parent' => '0',
'ativo' => '1',
),
6 =>
array (
'id' => '6',
'name' => ' Stickers',
'id_parent' => '0',
'ativo' => '1',
),
7 =>
array (
'id' => '7',
'name' => ' ScreensandFrames',
'id_parent' => '0',
'ativo' => '1',
),
8 =>
array (
'id' => '8',
'name' => ' Models',
'id_parent' => '0',
'ativo' => '1',
),
9 =>
array (
'id' => '9',
'name' => ' Notebook',
'id_parent' => '6',
'ativo' => '1',
),
10 =>
array (
'id' => '10',
'name' => ' Door',
'id_parent' => '0',
'ativo' => '1',
),
11 =>
array (
'id' => '11',
'name' => ' Door',
'id_parent' => '6',
'ativo' => '1',
),
12 =>
array (
'id' => '12',
'name' => ' Kangaroo',
'id_parent' => '4',
'ativo' => '1',
),
13 =>
array (
'id' => '13',
'name' => ' KangarooRaglan',
'id_parent' => '4',
'ativo' => '1',
),
14 =>
array (
'id' => '14',
'name' => ' RoundCollar',
'id_parent' => '4',
'ativo' => '1',
),
15 =>
array (
'id' => '15',
'name' => ' Trucker',
'id_parent' => '5',
'ativo' => '1',
),
16 =>
array (
'id' => '16',
'name' => ' Basic',
'id_parent' => '3',
'ativo' => '1',
),
17 =>
array (
'id' => '17',
'name' => ' Longline',
'id_parent' => '3',
'ativo' => '1',
),
18 =>
array (
'id' => '18',
'name' => ' Raglan',
'id_parent' => '3',
'ativo' => '1',
),
19 =>
array (
'id' => '19',
'name' => ' Raglan3/4',
'id_parent' => '3',
'ativo' => '1',
),
20 =>
array (
'id' => '20',
'name' => ' Regatta',
'id_parent' => '3',
'ativo' => '1',
),
21 =>
array (
'id' => '21',
'name' => ' Slide',
'id_parent' => '2',
'ativo' => '1',
),
22 =>
array (
'id' => '22',
'name' => ' Stickers',
'id_parent' => '8',
'ativo' => '1',
),
23 =>
array (
'id' => '23',
'name' => ' Notebook',
'id_parent' => '22',
'ativo' => '1',
),
24 =>
array (
'id' => '24',
'name' => ' T-shirt',
'id_parent' => '8',
'ativo' => '1',
),
25 =>
array (
'id' => '25',
'name' => ' Basic',
'id_parent' => '24',
'ativo' => '1',
),
26 =>
array (
'id' => '26',
'name' => ' Slippers',
'id_parent' => '8',
'ativo' => '1',
),
27 =>
array (
'id' => '27',
'name' => ' Slide',
'id_parent' => '26',
'ativo' => '1',
),
28 =>
array (
'id' => '28',
'name' => ' 1Screen',
'id_parent' => '7',
'ativo' => '1',
),
29 =>
array (
'id' => '29',
'name' => ' Set3Screens',
'id_parent' => '7',
'ativo' => '1',
),
30 =>
array (
'id' => '30',
'name' => ' Set5Screens',
'id_parent' => '7',
'ativo' => '1',
),
31 =>
array (
'id' => '31',
'name' => ' BlousesSweatshirt',
'id_parent' => '8',
'ativo' => '1',
),
32 =>
array (
'id' => '32',
'name' => ' Cap',
'id_parent' => '8',
'ativo' => '1',
),
33 =>
array (
'id' => '33',
'name' => ' ScreensandFrames',
'id_parent' => '8',
'ativo' => '1',
),
)
Whereas this array we have fathers and childs,
example:
-EX:[1] Models is father of: Stickers, T-shirt, Slippers
-EX:[2] Stickers is father of: Notebook
What I need is a function to get Model and all Childrens (till end) recursively:
I've already done a function to get Recursive Fathers from the Notebook, for example:
here:
function returnParent($id, $haystack, $arr = null){
$needle = $haystack[$id];
$arr[] = $needle;
if($needle['id_parent']){
return returnParent($needle['id_parent'], $haystack, $arr);
}else{
return $arr;
}
}
where $id = 23 and $haystack is complete dataset.
result:
Array
(
[0] => Array
(
[id] => 23
[name] => Notebook
[id_parent] => 22
)
[1] => Array
(
[id] => 22
[name] => Stickers
[id_parent] => 8
)
[2] => Array
(
[id] => 8
[name] => models
[id_parent] => 0
)
)
The result of function to return childrens needs to be the same of returnParent, the looked up value need to be in array as key 0;
complete dataset here:
https://pastebin.com/jgTM7aLA
suggestions to make my function better is very wellcome :)
EDIT
Expected output:
0 =>
array (
'id' => '8',
'name' => ' Models',
'id_parent' => '0',
'ativo' => '1',
),
1 =>
array (
'id' => '6',
'name' => ' Stickers',
'id_parent' => '0',
'ativo' => '1',
),
2 =>
array (
'id' => '9',
'name' => ' Notebook',
'id_parent' => '6',
'ativo' => '1',
),
3 =>
array (
'id' => '24',
'name' => ' T-shirt',
'id_parent' => '8',
'ativo' => '1',
),
4 =>
array (
'id' => '25',
'name' => ' Basic',
'id_parent' => '24',
'ativo' => '1',
)... and ..etc
Something like that, the key 0 must be the searched value, and the others values, dont need to be in tree order, any order its ok, as long as get all values.
in other words:
get a array node, show him and all childrens and grandchildrens.
function returnChild($id, $haystack, $arr = []){
$needle = $haystack[$id];
array_push($arr, $needle);
foreach($haystack as $key){
if($key['id_parent'] == $needle['id']){
array_push($arr,returnChild($key['id'], $haystack));
}
}
return $arr;
}
I hope this is the solution to your problem!
I believe this is what you are looking for.
I loop the unique parents and find what items have a matching parent.
I had to add a "NULL" value to parents and IDs because your array is not zero indexed.
That took me a long time to figure out...
Anyways the return is a 'parent' array and children below.
$temp = array_column($arr, "id");
$ids[0] = "NULL";
$ids = array_merge($ids, $temp);
$temp = array_column($arr, "id_parent");
$parents[0] = "NULL";
$parents = array_merge($parents, $temp);
Foreach(array_unique($parents) as $parent){
if($parent ==0) continue; // if parent is 0 we don't need to search for it (I think)
$new[$parent]['parent'] = $arr[$parent];
$new[$parent]['children'] = array_intersect_key($arr, array_intersect($parents, [$parent]));
}
Var_dump($new);
https://3v4l.org/Fbet4

Flat array to a hierarchical multi-dimensional array

I have an flat array that looks like this exemple :
array (
0 =>
array (
'TreePad_Fields' =>
array (
'#ID' => '1',
'#NAME' => '[CDATA[nomenclature exemple]]',
'#LEVEL' => '0',
),
),
1 =>
array (
'TreePad_Fields' =>
array (
'#ID' => '3',
'#NAME' => '[CDATA[droit]]',
'#LEVEL' => '1',
),
),
2 =>
array (
'TreePad_Fields' =>
array (
'#ID' => '13',
'#NAME' => '[CDATA[législation]]',
'#LEVEL' => '2',
),
),
3 =>
array (
'TreePad_Fields' =>
array (
'#ID' => '14',
'#NAME' => '[CDATA[statuts]]',
'#LEVEL' => '3',
),
),
4 =>
array (
'TreePad_Fields' =>
array (
'#ID' => '15',
'#NAME' => '[CDATA[projets de loi]]',
'#LEVEL' => '4',
),
),
5 =>
array (
'TreePad_Fields' =>
array (
'#ID' => '16',
'#NAME' => '[CDATA[réglementations]]',
'#LEVEL' => '2',
),
),
6 =>
array (
'TreePad_Fields' =>
array (
'#ID' => '17',
'#NAME' => '[CDATA[instruments statutaires]]',
'#LEVEL' => '3',
),
),
7 =>
array (
'TreePad_Fields' =>
array (
'#ID' => '2',
'#NAME' => '[CDATA[économie]]',
'#LEVEL' => '1',
),
),
8 =>
array (
'TreePad_Fields' =>
array (
'#ID' => '8',
'#NAME' => '[CDATA[analyse cout-avantage]]',
'#LEVEL' => '2',
),
),
9 =>
array (
'TreePad_Fields' =>
array (
'#ID' => '6',
'#NAME' => '[CDATA[analyse socio-économique]]',
'#LEVEL' => '2',
),
),
)
and I would like to have like this :
$data = array(
'[CDATA[nomenclature exemple]]' => array(
'[CDATA[droit]]' => array(
'[CDATA[législation]]' => array(
'[CDATA[statuts]]' => array(
'[CDATA[projets de loi]]'
),
),
'[CDATA[réglementations]]' => array(
'[CDATA[instruments statutaires]]'
),
),
'[CDATA[économie]]' => array(
'[CDATA[analyse cout-avantage]]',
'[CDATA[analyse socio-économique]]',
),
)
);
I can't figure out how to do it. I've found other examples here for converting flattened arrays into multidimensional ones but not where there's a custom child like this.
I should say it is a bit inconsistent to want to have the leaves in the datastructure have the [CDATA...] name as value of an indexed array, while in the rest of the tree they are keys.
So, I would suggest to make those leaves also keyed by the [CDATA...] name, but just with an empty array as value. That way the structure is consistent throughout.
For that structure you could use this function:
function buildTree($data) {
foreach($data as $i => $row) {
$arr = $row['TreePad_Fields'];
$level = $arr['#LEVEL'];
$key = $arr['#NAME'];
$levels[$level][$key] = [];
$levels[$level+1] = &$levels[$level][$key];
}
return $levels[0];
}
Call it like this:
$result = buildTree($data);
For the sample data given, the result would be:
array (
'[CDATA[nomenclature exemple]]' => array (
'[CDATA[droit]]' => array (
'[CDATA[législation]]' => array (
'[CDATA[statuts]]' => array (
'[CDATA[projets de loi]]' => array (),
),
),
'[CDATA[réglementations]]' => array (
'[CDATA[instruments statutaires]]' => array (),
),
),
'[CDATA[économie]]' => array (
'[CDATA[analyse cout-avantage]]' => array (),
'[CDATA[analyse socio-économique]]' => array (),
),
),
)
See it run on eval.in

changing array id in an multidimensional array with another array in php

I have 2 arrays:
$myArray = array ( 'name' => 'Dollar', 'sign' => '$', 'format' => '1', 'decimals' => '1', 'conversion_rate' => '1.324400' );
$myArrayNew = array ( 0 => 'Name', 1 => 'Sign', 2 => 'Format', 3 => 'Decimals', 4 => 'Conversion Rate');
When i use
$combinedarrays = array_combine($myArrayNew, $myArray);
the output is
Array ( [Name] => Dollar [Sign] => $ [Format] => 1 [Decimals] => 1 [Conversion Rate] => 1.324400 )
That is what I need, but the problem is when my first array is multidimensional like:
$myArray = array( array ( 'name' => 'Dollar', 'sign' => '$', 'format' => '1', 'decimals' => '1', 'conversion_rate' => '1.324400' ),
array ( 'name' => 'Euro', 'sign' => '€', 'format' => '2', 'decimals' => '1', 'conversion_rate' => '1.000000' ));
So, how to change the keys to be like below?
$myArray = array( array ( 'Name' => 'Dollar', 'Sign' => '$', 'Format' => '1', 'Decimals' => '1', 'Conversion Rate' => '1.324400' ),
array ( 'Name' => 'Euro', 'Sign' => '€', 'Format' => '2', 'Decimals' => '1', 'Conversion Rate' => '1.000000' ));
You need to loop on sub arrays.
Try this:
$myArrayNew = array ( 0 => 'Name', 1 => 'Sign', 2 => 'Format', 3 => 'Decimals', 4 => 'Conversion Rate');
$myArray = array( array ( 'name' => 'Dollar', 'sign' => '$', 'format' => '1', 'decimals' => '1', 'conversion_rate' => '1.324400' ),
array ( 'name' => 'Euro', 'sign' => '€', 'format' => '2', 'decimals' => '1', 'conversion_rate' => '1.000000' ));
$combinedarrays = array();
foreach($myArray as $subarr)
$combinedarrays[] = array_combine($myArrayNew, $subarr);
print_r($combinedarrays);

Categories