here's the result of my array
Array
(
[1] => Array
(
[0] => ABC
[1] => abc
[2] => rodriguez
[3] => Barkleys, 15 NO.
[4] => A
[5] => 1234567890
[6] =>
[7] => YES
[8] => a
)
[2] => Array
(
[0] => DEF
[1] => def
[2] => DAM
[3] => Barkleys, 15 NO.
[4] => A
[5] => 1234567891
[6] =>
[7] => YES
[8] => b
)
[3] => Array
(
[0] => GHI
[1] => ghi
[2] => TEG
[3] => Street4
[4] => B
[5] => 1234567892
[6] => YES
[7] =>
[8] => c
)
[4] => Array
(
[0] => JKL
[1] => jkl
[2] => CHA
[3] => Street4
[4] => B
[5] => 1234567893
[6] => YES
[7] =>
[8] => d
)
)
I need some of values from this array by using other value for exp. if I have value of 4th index in two separate arrays.
array (
[0] => A
[1] => A
)
array (
[0] => B
[1] => B
)
Now I need to get values of 2nd index in two seperate array using above two array value
array (
[1] => rodriguez
[2] => DAM
)
array (
[1] => TEG
[2] => CHA
)
How can I do this please suggest solution.
You can try to do this way :
<?php
$firstArray = array();
foreach($bigArray as $key => $array) {
$value = $array[4]; //fetch the 4th indice
if(! isset($firstArray[$value])) {
$firstArray[$value] = array();
}
$firstArray[$value][$key] = $value;
}
$secondArray = array();
foreach($firstArray as $name => $array) {
foreach($array as $key => $info) {
$value = $bigArray[$key][2]; //fetch the 2nd indice
if(! isset($secondArray[$value])) {
$secondArray[$value] = array();
}
$secondArray[$value][$key] = $value;
}
}
Related
Hello I have the following scenario:
The following array is to be changed to a two-dimensional array.
Array
(
[0] => Array
(
[0] => Name1
[1] => Name2
[2] => Name3
[3] => Name4
[4] => Name5
[5] => Name6
[6] => Name7
[7] => Name8
)
[1] => Array
(
[0] => Company1
[1] => Company2
[2] => Company3
[3] => Company4
[4] => Company5
[5] => Company6
[6] => Company7
[7] => Company8
)
[2] => Array
(
[0] => Street1
[1] => Street2
[2] => Street3
[3] => Street4
[4] => Street5
[5] => Street6
[6] => Street7
[7] => Street8
)
[3] => Array
(
[0] => Date1
[1] => Date2
[2] => Date3
[3] => Date4
[4] => Date5
[5] => Date6
[6] => Date7
[7] => Date8
)
[4] => Array
(
[0] => Date_2_1
[1] => Date_2_2
[2] => Date_2_3
[3] => Date_2_4
[4] => Date_2_5
[5] => Date_2_6
[6] => Date_2_7
[7] => Date_2_8
)
[5] => Array
(
[0] => place1
[1] => place2
[2] => place3
[3] => place4
[4] => place5
[5] => place6
[6] => place7
[7] => place8
)
[6] => Array
(
[0] => break1
[1] => break2
[2] => break3
[3] => break4
[4] => break5
[5] => break6
[6] => break7
[7] => break8
)
[7] => Array
(
[0] => postcode1
[1] => postcode2
[2] => postcode3
[3] => postcode4
[4] => postcode5
[5] => postcode6
[6] => postcode7
[7] => postcode8
)
)
How the final array should look like
Array
(
[0] => Array
(
[0] => Name1
[1] => Company1
[2] => Street1
[3] => Date1
[4] => Date_2_1
[5] => place1
[6] => break1
[7] => postcode1
)
[1] => Array
(
[0] => Name2
[1] => Company2
[2] => Street2
[3] => Date2
[4] => Date_2_2
[5] => place2
[6] => break2
[7] => postcode2
)
[2] => Array
(
[0] => Name3
[1] => Company3
[2] => Street3
[3] => Date3
[4] => Date_2_3
[5] => place3
[6] => break3
[7] => postcode3
)
[3] => Array
(
[0] => Name4
[1] => Company4
[2] => Street4
[3] => Date4
[4] => Date_2_4
[5] => place4
[6] => break4
[7] => postcode4
)
[4] => Array
(
[0] => Name5
[1] => Company5
[2] => Street5
[3] => Date5
[4] => Date_2_5
[5] => place5
[6] => break5
[7] => postcode5
)
[5] => Array
(
[0] => Name6
[1] => Company6
[2] => Street6
[3] => Date6
[4] => Date_2_6
[5] => place6
[6] => break6
[7] => postcode6
)
[6] => Array
(
[0] => Name7
[1] => Company7
[2] => Street7
[3] => Date7
[4] => Date_2_7
[5] => place7
[6] => break7
[7] => postcode7
)
[7] => Array
(
[0] => Name8
[1] => Company8
[2] => Street8
[3] => Date8
[4] => Date_2_8
[5] => place8
[6] => break8
[7] => postcode8
)
)
function test($post_employee_nr){
require_once $_SERVER['DOCUMENT_ROOT'].'/module/dienstplan/_config.php';
$employee_query = $dbh->query("SELECT FT.*,M.*,O.*,E.*,K.* FROM
finish_time FT
LEFT JOIN
m_schicht M ON FT.m_schichtid = M.ID
LEFT JOIN
objekte O ON O.ID = M.objid
LEFT JOIN
mitarbeiter E ON E.ID = M.mitarbeiterid
LEFT JOIN
kunde K ON K.ID = M.kdid where FT.mitarbeiterid=$post_employee_nr")->fetchall();
foreach ($employee_query as $row) {
$employee_ID[] = $row['FT.ID'];
$customer[] = $row['kundenname'];
$street[] = $row['straße'];
$postcode[] = $row['plz'];
$place[] = $row['ort'];
$begin[] = $row['b_time'];
$end[] = $row['e_time'];
$break[] = $row['pause'];
$output = array($employee_ID, $customer,$street,$postcode,$place,$begin,$end,$break);
}
$html = $output;
$response = $html;
echo json_encode($response);
}
I hope I could make it obvious enough
EDIT
This is my solution:
$result = array();
foreach($employee_query as $employee_query) {
$result[] = array(
$days[date('l', strtotime($employee_query['b_time']))],
date("d.m.Y", strtotime($employee_query['b_time'])),
$employee_query['kundenname'],
$employee_query['strasse'],
$employee_query['plz'].' '.$employee_query['ort'],
date("H:i", strtotime($employee_query['b_time'])),
date("H:i", strtotime($employee_query['e_time'])),
'<i class="fas fa-plus-circle" style="color:green;"></i>'
);
}
echo json_encode($result);
exit();
}
If you want to change it to a 2 layer array just create a variable that holds the first element of the 3 layer array:
somthing like:
var array2D = array3D[0];
Also this bit of code seems like its not necesarry
$html = array($output);
$response = $html;
echo json_encode($response);
unless you need the array to be 3 layers when encoding it to Json. Otherwise just change it to:
echo json_encode($output);
Hopefully I understood your question and was able to help a little.
Edit
The way your foreach loop is currently running you're only creating a new array for each element then adding the same elements to their respective array and finally storing every array inside a new one (1 array with ALL id's, and one with ALL companynames etc...)
to fix it is very simple.
inside your foreach loop the $row variable looks like this:
$row => [idvalue, companyvalue, streetvalue etc....]
it's already an array containing the current $row's data, now all you need to do is directly add it to your $output array.
You're new foreach loop should look something like this:
foreach($employee_query as $row) {
$output[] = $row; // when using [] after a variable you add to that array
}
echo json_encode($output);
if you don't want to use all the data that your query collected you can specify which attributes you want to use like so:
$output[] = array($row['FT.ID'], $row['kundenname'], $row['straße'], etc...);
This pertains to PHP.
I have an array of arrays that is a recordset from a db query:
Array
(
[0] => Array
(
[0] => Amazon
[1] => AmazonSendTracking
[2] =>
[3] => 1
[4] => IN
[5] => 2020-01-07 11:32:18
[6] => 7
[7] => 5
)
[1] => Array
(
[0] => Amazon
[1] => AmazonGetOrdersAndMove
[2] =>
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 6
[7] => 4
)
[2] => Array
(
[0] => Test
[1] => RedirectTest1
[2] => data=data1&data2=data2&testvar=testvariable
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 17
)
[3] => Array
(
[0] => Test
[1] => RedirectTest2
[2] => data=value&data2=value2&testvar=value3
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 25
)
[4] => Array
(
[0] => Amazon
[1] => AmazonPushInventory
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 26
)
[5] => Array
(
[0] => Amazon
[1] => CalculateFloorCeiling
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 27
)
[6] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXML
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 28
)
[7] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXMLResetBusinessAndFlipEm
[2] =>
[3] => 1440
[4] => OUT
[5] =>
[6] => 8
[7] => 6
)
)
the Value in the 7th position (6) of the sub-array represents a "group" that I want to break out into individual arrays.
So I want the above to become one array of items not in any group and then a new array for each group like this:
Array1 - All Items not in a group
Array1
(
[0] => Array
(
[0] => Amazon
[1] => AmazonSendTracking
[2] =>
[3] => 1
[4] => IN
[5] => 2020-01-07 11:32:18
[6] => 7
[7] => 5
)
[1] => Array
(
[0] => Amazon
[1] => AmazonGetOrdersAndMove
[2] =>
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 6
[7] => 4
)
[2] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXMLResetBusinessAndFlipEm
[2] =>
[3] => 1440
[4] => OUT
[5] =>
[6] => 8
[7] => 6
)
)
Array2 - 1st group
Array2(
[0] => Array
(
[0] => Test
[1] => RedirectTest1
[2] => data=data1&data2=data2&testvar=testvariable
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 17
)
[1] => Array
(
[0] => Test
[1] => RedirectTest2
[2] => data=value&data2=value2&testvar=value3
[3] => 4
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 19
[7] => 25
)
)
Array3 - 2nd group
Array3(
[0] => Array
(
[0] => Amazon
[1] => AmazonPushInventory
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 26
)
[1] => Array
(
[0] => Amazon
[1] => CalculateFloorCeiling
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 27
)
[2] => Array
(
[0] => Amazon
[1] => AmazonSubmitPricingXML
[2] =>
[3] => 15
[4] => ALL
[5] => 2020-01-07 11:32:18
[6] => 27
[7] => 28
)
)
Im just not sure what array functions would be best used to accomplish this. I have tried getting the count of each group using the following.
I have a loop that steps through the master array (array of arrays call $rs) and while in the loop I pull out the 7th position into a variable called $HeaderRecNbr and then do this:
$GroupCount=array_count_values(array_column($rs, 6))[$HeaderRecNbr];
but Im not sure where to go from here.
Just create an array indexed by the group from index 6. This will create a sub-array for each group indexed by the group number:
foreach($array as $v) {
$groups[$v[6]][] = $v;
}
Then to get the items that are alone in a group, check if there is exactly one sub-array and add it to another array. Then remove it from the group array:
foreach($groups as $k => $v) {
if(count($v) == 1) {
$other[] = $v; // or $other[$v[0][6]][] = $v;
unset($groups[$k]);
}
}
$mainArray = ... all rows variable here..
$groupedArray = [];
$countArray = [];
foreach ($mainArray as $k => $v) {
$groupedArray[$v[6]][] = $v;
$countArray[$v[6]]++;
}
$singleItems = [];
$multipleItems = [];
foreach ($groupedArray as $k => $v) {
if ($countArray[$k] > 1) $multipleItems[] = $v;
else $singleItems = $v;
}
... do something with single and multi - groups...
1: start by grouping all items in groups by the 6th item of sub array... keep count of them at same time
2. Separate single and multiple items groups in 2 arrays...
3. Do whatever you need with multi-items groups.
This function will group them by desired index, then combine the ones that dont have any other with same index, and return an array of arrays, ungrouped, then all grouped.
function sortByIndex( $data, $index ) {
$sortedData = array();
$ungroupedData = array();
// Make sure you can loop through
if ( ! is_array( $data ) ) {
return FALSE;
}
foreach ( $data as $key => $arrayToInspect ) {
if ( ! isset( $sortedData[$arrayToInspect[$index]] ) ) {
$sortedData[$arrayToInspect[$index]] = array();
}
$sortedData[$arrayToInspect[$index]][] = $arrayToInspect;
}
// Combine as desired
foreach ( $sortedData as $groupId => $data ) {
if ( count( $data ) < 2 ) {
$ungroupedData[] = $data;
unset( $sortedData[$groupId] );
}
}
return ( array_merge( [ $ungroupedData ], array_values( $sortedData ) ) );
}
I have an array of customers/people with a subarray of projects pertaining to that customer. Projects come in two types "typeJob" or "typePipeline". I need to sum the values of individual columns in the projects subarray (specifically columns 5, 6, 7 - representing revenue for month 1, month 2, and month 3) by Client.
I have tried various possible avenues including the one below, but I seem to always end up either testing if the condition is satisfied for the array as a whole rather than for each row - or if I go the way of for each loops generate errors because either there's an array/single value mismatch or the condition just doesn't do anything.
Here is what I have tried most recently. It works in the sense that it checks if "typeJob" is in the array but then sums all rows (even those without "typeJob") if "typeJob" is found for any of the Client's projects. I want to exclude rows where "typeJob" is not found.
foreach ($arrays as $row) {
$indexJob = array_search('typeJob', array_column($row['projects'],0));
if ($indexJob !== false) {
$job_total1[$row['Client']] = array_sum(array_column($row['projects'],5));
$job_total2[$row['Client']] = array_sum(array_column($row['projects'],6));
$job_total3[$row['Client']] = array_sum(array_column($row['projects'],7));
}
}
And here is the array:
(
[331] => Array
(
[KeyAccountID] => 1234
[KeyAccountName] => John Lennon
[ClientID] => 9999
[Client] => BBC
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 915
[2] => Zyxeldy
[3] =>
[4] =>
[5] => 15000
[6] =>
[7] =>
[8] =>
)
[1] => Array
(
[0] => typeJob
[1] => 956
[2] => Awesome project, Step 1
[3] =>
[4] =>
[5] => 1833.3333
[6] => 1833.3333
[7] => 1833.3333
[8] =>
)
[2] => Array
(
[0] => typePipeline
[1] => 957
[2] => Awesome project, Step 2
[3] =>
[4] =>
[5] => 7000
[6] =>
[7] =>
[8] =>
)
)
)
[344] => Array
(
[KeyAccountID] => 1234
[KeyAccountName] => John Lennon
[ClientID] => 9998
[Client] => ABC
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 487
[2] => CRM integration
[3] =>
[4] =>
[5] =>
[6] => 98750
[7] => 98750
[8] =>
)
[1] => Array
(
[0] => typeJob
[1] => 839
[2] => Data Warehouse
[3] =>
[4] =>
[5] =>
[6] => 11643.0601
[7] =>
[8] =>
)
)
)
[350] => Array
(
[KeyAccountID] => 1236
[KeyAccountName] => Ringo Starr
[ClientID] => 9997
[Client] => XYY
[projects] => Array
(
[0] => Array
(
[0] => typeJob
[1] => 867
[2] => Data Mining
[3] =>
[4] =>
[5] => 10000
[6] =>
[7] =>
[8] =>
)
)
)
[351] => Array
(
[KeyAccountID] => 1235
[KeyAccountName] => Poul McCartney
[ClientID] => 9996
[Client] => XYZ
[projects] => Array
(
[0] => Array
(
[0] => typePipeline
[1] => 715
[2] => XYZ, CSM
[3] =>
[4] =>
[5] => 22083.3333
[6] => 22083.3333
[7] => 22083.3333
[8] =>
)
)
)
etc.
You could filter the $row['projects'] array before summing the values:
foreach ($arrays as $row) {
$typeJobs = array_filter($row['projects'], function ($v) { return $v[0] == 'typeJob'; });
$job_total1[$row['Client']] = array_sum(array_column($typeJobs,5));
$job_total2[$row['Client']] = array_sum(array_column($typeJobs,6));
$job_total3[$row['Client']] = array_sum(array_column($typeJobs,7));
}
It may be quicker to just loop through them and keep a running total...
foreach ($arrays as $row) {
$totals = [0,0,0];
foreach ( $row['projects'] as $project ) {
if ( $project[0] == 'typeJob' ) {
$totals[0] += $project[5];
$totals[1] += $project[6];
$totals[2] += $project[7];
}
}
$job_total1[$row['Client']] = $totals[0];
$job_total2[$row['Client']] = $totals[1];
$job_total3[$row['Client']] = $totals[2];
}
it doesn't look as slim as using the array_... methods, but this only processes the array once instead of once for each call.
I want to convert a multi array in 2-D array. I have following result of multi array.
Array
(
[1] => Array
(
[0] => Array
(
[0] => Id
[1] => Name
[2] => Fname
[3] => School
[4] => Photo
)
)
[2] => Array
(
[0] => Array
(
[0] => 32
[1] => kamal
[2] => hjhbg
[3] => hnp
[4] => B612_16.jpg
)
)
[3] => Array
(
[0] => Array
(
[0] => 33
[1] => dg
[2] => fa
[3] => f
[4] => bg.jpg
)
)
[4] => Array
(
[0] => Array
(
[0] => 35
[1] => mohit
[2] => bc
[3] => jhbvj
[4] => B612.jpg
)
)
)
Now I need to convert this array in below format.
Array
(
[0] => Array
(
[0] => Id
[1] => Name
[2] => Fname
[3] => School
[4] => Photo
)
[1] => Array
(
[0] => 32
[1] => kamal
[2] => hjhbg
[3] => hnp
[4] => B612_16.jpg
)
[2] => Array
(
[0] => 33
[1] => dg
[2] => fa
[3] => f
[4] => bg.jpg
)
[3] => Array
(
[0] => 35
[1] => mohit
[2] => bc
[3] => jhbvj
[4] => B612.jpg
)
)
Try this
function array_to1d($a) {
$out = array();
foreach ($a as $b) {
foreach ($b as $c) {
if (isset($c)) {
$out[] = $c;
}
}
}
return $out;
}
echo "<pre>"; print_r(array_to1d($array)); // $array your array name
The shortest solution would be using array_walk() here:
array_walk($array, function(&$v) {
$v = $v[0];
});
You just have to do
$two_d_array = array_values($three_d_array);
What I've tried so far : I have an array as shown below, what i wanted to do is, create multiple arrays based on the value BEGIN_DAY:
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
[6] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[7] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
and so on ...
I want to split this array into multiple different arrays when ever it begin with BEGIN_DAY so it should look like this:
The first :
Array
(
[0] => Array
(
[0] => BEGIN_DAY
[1] => 15
)
[1] => Array
(
[0] => 20130701
[1] => 4
[2] => 4
[3] => 3060
[4] => 1
)
[2] => Array
(
[0] => 20130702
[1] => 270
[2] => 757
[3] => 13812810
[4] => 4
)
[3] => Array
(
[0] => 20130703
[1] => 5
[2] => 123
[3] => 3894971
[4] => 2
)
[4] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
[5] => Array
(
[0] => END_DAY
[1] => 15
)
The second :
[0] => Array
(
[0] => BEGIN_DAY
[1] => 16
)
[1] => Array
(
[0] => 20130704
[1] => 290
[2] => 478
[3] => 5119617
[4] => 1
)
What I've tried so far :
$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
if($day_all[$key][0]==$split_by){
$days=array_slice($day_all, 0,$split_by);
}
}
var_dump($days);
Much Appreciated!
$sk = "first";
foreach ($array as $key=>$value)
{
if(in_array(BEGIN_DAY, $value)&&$key!=0)
{
$sk = "second";
}
$result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];
Something like this :
$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
$tempArray = $yourArray[$idx]
if($yourArray[$idx][0] == 'BEGIN_DAY'){
$finalArray[] = $tempArray;
$tempArray = [];
}
}
The final array will contain the arrays. Each time the BEGIN_DAY is found, the array will be inserted into the finalArray and a new one is created.
To improve the code, check the existence of the cell too, to avoid index exception.