Merge multiple arrays values into one array value - php

I have 4 arrays with each 10 elements:
$vidtype = Array ( [0] => Array ( [0] => Sales [1] => Sales [2] => Sales [3] => Sales [4] => Sales [5] => Sales [6] => Sales [7] => Sales [8] => Sales [9] => Sales
$vidcategory = Array ( [0] => Array ( [0] => comfortsystemen [1] => multimediasystemen [2] => assistentiesystemen [3] => multimediasystemen [4] => productfilms [5] => assistentiesystemen [6] => multimediasystemen [7] => productfilms [8] => productfilms [9] => productfilms
$vidurl = Array ( [0] => Array ( [0] => www.youtube.com/video/bOejnFiC88E [1] => www.youtube.com/video/FsVAxrvi6iE [2] => www.youtube.com/video/3lGfTZdVK1s [3] => www.youtube.com/video/hcw8dl73-W4 [4] => www.youtube.com/video/NlHJ5njWVFE [5] => www.youtube.com/video/II68oVm4zro [6] => www.youtube.com/video/tpg9IaOfno4 [7] => www.youtube.com/video/mzbG0JAICu8 [8] => www.youtube.com/video/OgPodRbJ3So [9] => www.youtube.com/video/YfPLB30MSCU
$vidtitle = Array ( [0] => Array ( [0] => Gesture Control [1] => Volkswagen – Parkmobile [2] => Multi Collision braking system [3] => Mobiele telefoon interface Premium [4] => Maps & More Dock in de up! [5] => Lane Assist [6] => Car-Net Connect [7] => Volkswagen Allesdrager [8] => Volkswagen Bagagebox [9] => Volkswagen fietsendrager
The arrays are all in the same order, so value 1 of $vidtype matches value 1 of $vidcategory,$vidurl and $vidtitle. I am exporting them to an excel file but want to sort $vidtype and $vidtitle in alphabetical order.
If I do that the array values won't match anymore, so my idea was to merge all the values with the same key together and sort $vidtype and $vidtitle in alphabetical order after merging. So the values will still match.
I got stuck on the merge part, I have been searching on stackoverflow for a long time but couldn't find the right answer.
Is my idea clear? Any thoughts?

this will work
$abc=array();
for($i=0;$i<10;$i++){
$abc[$i]=array($vidtype[$i],$vidcategory[$i],$vidurl[$i],$vidtitle[$i]);
}

Related

How to find a different array between the two multidimentional arrays in php

Assume that I have two multidimensional arrays as follows:
Multidimensional array 1:
$newArray = Array
(
[0] => Array
(
[0] => 45521
[1] => Clothing
[2] => Percent discount
[3] => Get 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Germany
)
[1] => Array
(
[0] => 45516
[1] => Apparel
[2] => Percentage Off
[3] => 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Australia
)
[2] => Array
(
[0] => 45672
[1] => Food & Drink
[2] => Free Shipping
[3] => Summer Collection Now. Shop Now!
[4] => 2020-05-17
[5] => 2020-08-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40761312
[8] => Fancy
[9] => US
)
[3] => Array
(
[0] => 45866
[1] => Food & Drink
[2] => Percentage off
[3] => 20% Off
[4] => 2020-05-17
[5] => 2026-05-15
[6] => OFF20
[7] => https://domain[dot]com/AYuANDEqU40780908
[8] => Biata
[9] => US
)
[4] => Array
(
[0] => 45524
[1] => Health & Wellness
[2] => Other
[3] => Jet's monthly bottle of probiotics. Try it now!
[4] => 2020-05-02
[5] => 2026-05-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40758409
[8] => Jet
[9] => US
)
)
[5] => Array
(
[0] => 45524
[1] => Health & Wellness
[2] => Other
[3] => Jet seasonal probiotics. No hidden cost.
[4] => 2020-05-02
[5] => 2026-05-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40758408
[8] => Jet
[9] => US
)
Multidimension array 2:
$oldArray = Array
(
[0] => Array
(
[0] => 45521
[1] => Clothing's
[2] => Percent discount
[3] => Get 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Germany
)
[1] => Array
(
[0] => 45516
[1] => Apparel
[2] => Percentage Off
[3] => 15% off With Code ABCD15
[4] => 2020-05-18
[5] => 2020-05-31
[6] => ABCD15
[7] => https://domain[dot]com/AYuANDEqU40
[8] => Boutique
[9] => Australia
)
[2] => Array
(
[0] => 45672
[1] => Food & Drink
[2] => Free Shipping
[3] => Summer Collection Now. Shop Now!
[4] => 2020-05-17
[5] => 2020-08-01
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40761312
[8] => Fancy
[9] => US
)
[3] => Array
(
[0] => 45866
[1] => Food & Drink
[2] => Percentage off
[3] => 20% Off
[4] => 2020-05-17
[5] => 2026-05-15
[6] => OFF20
[7] => https://domain[dot]com/AYuANDEqU40780908
[8] => Biata
[9] => US
)
[4] => Array
(
[0] => 45706
[1] => Electronic Equipment
[2] => Percentage off
[3] => 40% Off-Set Under $200!
[4] => 2020-05-17
[5] => 2026-05-15
[6] => N/A
[7] => https://domain[dot]com/AYuANDEqU40768379
[8] => GVM
[9] => US
)
)
You can easily see that the $newArray has one different array([4]) from the $oldArray, and one new array([5]) to the $oldArray, and one different key=value pair like [1] => Clothing's from the $oldArray
What i am expecting is to compare the $newArray with the $oldArray to get the different array([4]) and the new one array([5]) in the $newArray only.
I have searched for these links:
https://www.php.net/manual/en/function.array-diff-assoc.php
and
https://www.w3schools.com/php/func_array_diff_assoc.asp
but they do not help.
Then I've searched for the function in this link:
Compare two different multidimentional arrays and highlight the changes
The function:
function arrayRecursiveDiff($aArray1, $aArray2) {
$aReturn = array();
foreach ($aArray1 as $mKey => $mValue) {
if (array_key_exists($mKey, $aArray2)) {
if (is_array($mValue)) {
$aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff;
}
} else {
if ($mValue != $aArray2[$mKey]) {
$aReturn[$mKey] = $mValue;
}
}
} else {
$aReturn[$mKey] = $mValue;
}
}
return $aReturn;
}
$arr1 = arrayRecursiveDiff($newentry,$oldentry);
It filters and shows me both the COMPLETE new arrays from the $newArray and the new arrays from the $newArray with only the key=value pairs that are different from the $oldArray.
Yes and again, what i am expecting is to compare the $newArray with the $oldArray to get:
1/ The COMPLETE different array like array([4]) from the $newArray.
2/ The new array like array([5]) from the $newArray.
3/ The arrays from the $newArray of which one of the key=value pairs is different from that of the $oldArray, but it must print all 9 key=value pairs of that array, which is the array[0] in this example. I don't want to get the new arrays from $newArray with the output of ONLY the updated key=value pairs.
Sorry for a long post with wordy explanation, but I have no choice.
Your help is appreciated.
$a1=array(1,2,3);
$a2=array(1,2,4);
$i =0;
$diff_Value = '';
foreach( $a1 as $val )
{
if($a2[$i]!=$val){
$diff_Value.= $val.','.$a2[$i];
}
$i =$i+1;
}
echo $diff_Value;

Get sum of subarray column for indexed rows

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.

how to add the particular key values in php?

Array
(
[csv_data] => Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8000
[8] => 6000
)
[2] => Array
(
[0] =>
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Multi-Touch Email Campaign
[7] => 350
[8] => 200
)
[3] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9000
[8] => 4000
)
[4] => Array
(
[0] =>
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Linkedin(Groups)
[7] => 350
[8] => 200
)
)
)
Hello, i am new in PHP.
i just want that in this array i want to add the particular array key values and identify that it sholud be has the same partner and divison after identify the values of all particular should be add in new array.
ANSWER should be like this:
Array
(
[csv_data] => Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8350
[8] => 6200
)
[2] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9350
[8] => 4200
)
)
)
Try this code, this will definitely work for you..
$removeKeys = array();
foreach($data['csv_data'] as $key => $val)///loop through array..
{
foreach($data['csv_data'] as $k => $v)
{
if($val[1] == $v[1] && $key != $k)////check if key 1 matches
{
if(!in_array($key,$removeKeys)) ////check if item is already added or not
{
$removeKeys[] = $k; ///push into removed keys because this is added into matched item
$data['csv_data'][$key][7]+=$data['csv_data'][$k][7];
$data['csv_data'][$key][8]+=$data['csv_data'][$k][8];
}
}
}
}
foreach($removeKeys as $rk)
{
unset($data['csv_data'][$rk]); ////remove all the keys in removeKeys
}
print_r($data['csv_data']);///your desired output...
This will give you :
Array
(
[0] => Array
(
[0] => CAM
[1] => Partner
[2] => Division
[3] => Domain
[4] => Year
[5] => Quarter
[6] => Tactic
[7] => Impressions
[8] => Responders
)
[1] => Array
(
[0] => CAM
[1] => Acme and Brick
[2] => Belgium
[3] => www.partnerA.com
[4] => 2016
[5] => Q2
[6] => Single Email Campaign
[7] => 8350
[8] => 6200
)
[3] => Array
(
[0] => TestR
[1] => Partner R2
[2] => India
[3] => www.partnerA.com
[4] => 2016
[5] => Q1
[6] => Single Email Campaign
[7] => 9350
[8] => 4200
)
)
LIVE EXAMPLE : CLICK HERE

Find if array exists in multidimensional array

I have a multidimensional array that look like this:
Array
[1] => Array
(
[0] => ACURA
[1] => CL
[2] => 3.2L V6 F/I
[3] => Blue
[4] => 33-2133
[5] => 33-2133
[6] => V6
[7] => F/I
[8] =>
)
[2] => Array
(
[0] => ACURA
[1] => CL
[2] => 3.2L V6 F/I
[3] => Blue
[4] => PS-1004
[5] => PS-1004
[6] => V6
[7] => F/I
[8] =>
)
)
I then have another array that looks like
Array
(
[0] => ACURA
[1] => CL
[2] => 3.2L V6 F/I
[3] => blue
[4] => HP-1004
[5] => HP-1004
[6] => V6
[7] => F/I
[8] =>
)
Is the a way to look through the multdimensional array and see if the single array exists already in the multidimensional array?
I believe in_array() should work.
Link to PHP manual.
In the changlog it mentions that as of 4.2.0 the needle (search) can be an array...

Search records with in an two dimensional array

i am trying to do one thig ,but i am not getting the point.can anyone please suggests me ..
two dimensional array like below
$array = Array
([1] => Array
([1] => EST_ID
[2] => EST_CODE
[3] => EST_EXT
[4] => EST_NAME
[5] => INCROP_ADDRESS1
[6] => INCROP_ADDRESS2
[7] => INCROP_CITY
[8] => INCROP_DIST
[9] => INCROP_STATE
[10] => INCROP_PIN
)
[2] => Array
([1] => HRKNL0006923000
[2] => 6923
[4] => VAIBHAV CARPETS
[5] => BHUL-BHULIA CHOWK
[6] => BODHWA RAM COLONY
[7] => PANIPAT
[8] => PANIPAT
[10] => 132103)
[3] => Array
([1] => HRKNL0004638000
[2] => 4638
[4] => GURU TEG BHADUR PUBLIC SCHOOL
[5] => MODEL TOWN
[6] => KARNAL
[7] => KARNAL
[8] => KARNAL
[10] => 132001)
)
i have to search the array key of either 2 or 3 from $array
Not exactly sure which one you need as I didn't fully understand the question but one of these two functions will help array_key_exists() or in_array()
It sounds like you may be trying to do what I asked in Search a multi-dimensional array for certain values

Categories