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
Related
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;
I've a multidimensional array looks like below-
Array
(
[0] => Array
(
[0] => Account Number
[1] => Account Title
[2] => Account Type
[3] => Currency
[4] => Available Balance
[5] => Ledger Balance
)
[1] => Array
(
[0] => xxxxxxxx YYYYYYY
)
[2] => Array
(
[0] =>
)
[3] => Array
(
[0] => Date
[1] => Description
[2] => Withdrawal
[3] => Deposit
[4] => Balance
)
[4] => Array
(
[0] =>
)
[5] => Array
(
[0] => `30/08/2018
[1] => EXIM 5287609
[2] => -
[3] => 25000.00
[4] => 62672.880
)
[6] => Array
(
[0] => `03/09/2018
[1] => PRIME 9373059
[2] => -
[3] => 191920.00
[4] => 254592.880
)
[7] => Array
(
[0] => `04/09/2018
[1] => RV CLEARING FEE-CHQ 9373059
[2] => 8.70
[3] => -
[4] => 254584.180
)
[8] => Array
(
[0] => `04/09/2018
[1] => VAT AT SOURCE
[2] => 1.30
[3] => -
[4] => 254582.880
)
[9] => Array
(
[0] => `04/09/2018
[1] => ATM WDR AT 4503 13:55:02
[2] => 20000.00
[3] => -
[4] => 234582.880
)
[10] => Array
(
[0] => `08/09/2018
[1] => ATM WDR AT 4504 11:58:19
[2] => 10000.00
[3] => -
[4] => 224582.880
)
[11] => Array
(
[0] => `09/09/2018
[1] => ATM WDR AT 4500 20:05:19
[2] => 40000.00
[3] => -
[4] => 184582.880
)
[12] => Array
(
[0] => `09/09/2018
[1] => ATM WDR AT 4500 20:05:59
[2] => 40000.00
[3] => -
[4] => 144582.880
)
[13] => Array
(
[0] => `09/09/2018
[1] => ATM WDR AT 4500 20:06:37
[2] => 20000.00
[3] => -
[4] => 124582.880
)
[14] => Array
(
[0] => `12/09/2018
[1] => B ASIA 5904486
[2] => -
[3] => 490000.00
[4] => 614582.880
)
[15] => Array
(
[0] => `12/09/2018
[1] => B ASIA 5904486 RETURN
[2] => 490000.00
[3] => -
[4] => 124582.880
)
[16] => Array
(
[0] => `12/09/2018
[1] => RV CLEARING FEE-CHQ 5904486
[2] => 8.70
[3] => -
[4] => 124574.180
)
[17] => Array
(
[0] => `12/09/2018
[1] => VAT AT SOURCE
[2] => 1.30
[3] => -
[4] => 124572.880
)
[18] => Array
(
[0] => `12/09/2018
[1] => ATM WDR AT 4500 12:31:55
[2] => 20000.00
[3] => -
[4] => 104572.880
)
[19] => Array
(
[0] => `13/09/2018
[1] => ATM WDR AT 4503 14:59:28
[2] => 20000.00
[3] => -
[4] => 84572.880
)
[20] => Array
(
[0] => `16/09/2018
[1] => ATM WDR AT 4500 17:10:04
[2] => 10000.00
[3] => -
[4] => 74572.880
)
[21] => Array
(
[0] => `19/09/2018
[1] => B ASIA 5904486
[2] => -
[3] => 490000.00
[4] => 564572.880
)
[22] => Array
(
[0] => `19/09/2018
[1] => ATM WDR AT 4502 16:24:29
[2] => 40000.00
[3] => -
[4] => 524572.880
)
[23] => Array
(
[0] => `19/09/2018
[1] => ATM WDR AT 4502 16:25:12
[2] => 10000.00
[3] => -
[4] => 514572.880
)
[24] => Array
(
[0] => `20/09/2018
[1] => B ASIA 5904486 RETURN
[2] => 490000.00
[3] => -
[4] => 24572.880
)
[25] => Array
(
[0] => `20/09/2018
[1] => RV CLEARING FEE-CHQ 5904486
[2] => 8.70
[3] => -
[4] => 24564.180
)
[26] => Array
(
[0] => `20/09/2018
[1] => VAT AT SOURCE
[2] => 1.30
[3] => -
[4] => 24562.880
)
[27] => Array
(
[0] => `22/09/2018
[1] => ATM WDR AT 4500 14:09:11
[2] => 15000.00
[3] => -
[4] => 9562.880
)
[28] => Array
(
[0] => `24/09/2018
[1] => CASH DEPOSIT
[2] => -
[3] => 100000.00
[4] => 109562.880
)
[29] => Array
(
[0] => `25/09/2018
[1] => EFT TO PRIME BANK LT
[2] => 45000.00
[3] => -
[4] => 64562.880
)
[30] => Array
(
[0] => `26/09/2018
[1] => ATM WDR AT 4503 11:06:25
[2] => 5000.00
[3] => -
[4] => 59562.880
)
)
Now I need a new array looks like below-
array(
[1] => Array
(
[0] => Date
[1] => Description
[2] => Withdrawal
[3] => Deposit
[4] => Balance
)
[2] => Array
(
[0] => `30/08/2018
[1] => EXIM 5287609
[2] => -
[3] => 25000.00
[4] => 62672.880
)
[3] => Array
(
[0] => `03/09/2018
[1] => PRIME 9373059
[2] => -
[3] => 191920.00
[4] => 254592.880
)
[4] => Array
(
[0] => `04/09/2018
[1] => RV CLEARING FEE-CHQ 9373059
[2] => 8.70
[3] => -
[4] => 254584.180
)
[5] => Array
(
[0] => `04/09/2018
[1] => VAT AT SOURCE
[2] => 1.30
[3] => -
[4] => 254582.880
)
[6] => Array
(
[0] => `04/09/2018
[1] => ATM WDR AT 4503 13:55:02
[2] => 20000.00
[3] => -
[4] => 234582.880
)
[7] => Array
(
[0] => `08/09/2018
[1] => ATM WDR AT 4504 11:58:19
[2] => 10000.00
[3] => -
[4] => 224582.880
)
[8] => Array
(
[0] => `09/09/2018
[1] => ATM WDR AT 4500 20:05:19
[2] => 40000.00
[3] => -
[4] => 184582.880
)
[9] => Array
(
[0] => `09/09/2018
[1] => ATM WDR AT 4500 20:05:59
[2] => 40000.00
[3] => -
[4] => 144582.880
)
[10] => Array
(
[0] => `09/09/2018
[1] => ATM WDR AT 4500 20:06:37
[2] => 20000.00
[3] => -
[4] => 124582.880
)
[11] => Array
(
[0] => `12/09/2018
[1] => B ASIA 5904486
[2] => -
[3] => 490000.00
[4] => 614582.880
)
[12] => Array
(
[0] => `12/09/2018
[1] => B ASIA 5904486 RETURN
[2] => 490000.00
[3] => -
[4] => 124582.880
)
[13] => Array
(
[0] => `12/09/2018
[1] => RV CLEARING FEE-CHQ 5904486
[2] => 8.70
[3] => -
[4] => 124574.180
)
[14] => Array
(
[0] => `12/09/2018
[1] => VAT AT SOURCE
[2] => 1.30
[3] => -
[4] => 124572.880
)
[15] => Array
(
[0] => `12/09/2018
[1] => ATM WDR AT 4500 12:31:55
[2] => 20000.00
[3] => -
[4] => 104572.880
)
[16] => Array
(
[0] => `13/09/2018
[1] => ATM WDR AT 4503 14:59:28
[2] => 20000.00
[3] => -
[4] => 84572.880
)
[17] => Array
(
[0] => `16/09/2018
[1] => ATM WDR AT 4500 17:10:04
[2] => 10000.00
[3] => -
[4] => 74572.880
)
[18] => Array
(
[0] => `19/09/2018
[1] => B ASIA 5904486
[2] => -
[3] => 490000.00
[4] => 564572.880
)
[19] => Array
(
[0] => `19/09/2018
[1] => ATM WDR AT 4502 16:24:29
[2] => 40000.00
[3] => -
[4] => 524572.880
)
[20] => Array
(
[0] => `19/09/2018
[1] => ATM WDR AT 4502 16:25:12
[2] => 10000.00
[3] => -
[4] => 514572.880
)
[21] => Array
(
[0] => `20/09/2018
[1] => B ASIA 5904486 RETURN
[2] => 490000.00
[3] => -
[4] => 24572.880
)
[22] => Array
(
[0] => `20/09/2018
[1] => RV CLEARING FEE-CHQ 5904486
[2] => 8.70
[3] => -
[4] => 24564.180
)
[23] => Array
(
[0] => `20/09/2018
[1] => VAT AT SOURCE
[2] => 1.30
[3] => -
[4] => 24562.880
)
[24] => Array
(
[0] => `22/09/2018
[1] => ATM WDR AT 4500 14:09:11
[2] => 15000.00
[3] => -
[4] => 9562.880
)
[25] => Array
(
[0] => `24/09/2018
[1] => CASH DEPOSIT
[2] => -
[3] => 100000.00
[4] => 109562.880
)
[26] => Array
(
[0] => `25/09/2018
[1] => EFT TO PRIME BANK LT
[2] => 45000.00
[3] => -
[4] => 64562.880
)
[27] => Array
(
[0] => `26/09/2018
[1] => ATM WDR AT 4503 11:06:25
[2] => 5000.00
[3] => -
[4] => 59562.880
)
)
I know by using a loop I can separate my desired arrays and can crate a new array. But without any loop, is it possible to create a new array by PHP built in method?
Advance thanks for all of you who will give me a nice solution.
Note:- array will be too long. It's a bank transaction array so need a fast method which will give output within the shortest time.
You can filter arrays based on it length using array_filter()
$newArr = array_filter($arr, function($item){
return sizeof($item) == 5;
});
If your array is large and you need a fast method, use foreach instead
foreach ($arr as $key=>$item)
sizeof($item) != 5 ? unset($arr[$key]) : "";
All activities on one go...
$length=count($arr);
for($i=0;$i<$length;$i++){
if(count($arr[$i])==4){
// execute desired process
}
}
I have this php array:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 1
)
[1] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[2] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 0
[9] => 0
[10] => 0
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 0
[10] => 0
)
[4] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 0
[9] => 1
[10] => 0
)
)
I want i new array:
1) if element [0] exists in the array I want to control elements 8, 9 and 10 and if one of this is 1 I want to have 1 in the final array, but I don't want to have the same array 2 times (the index [0] is the key).
My final array shound be:
Array
(
[0] => Array
(
[0] => 2505
[1] => Lima
[2] => Daniels
[3] => 0996995904
[4] =>
[5] => 755971
[6] => 1454284800
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 1
)
[3] => Array
(
[0] => 2525
[1] => Lomarca
[2] => Miro
[3] => 0099778877
[4] =>
[5] => 768131
[6] => 1454976000
[7] => Cat. A (Moto)
[8] => 1
[9] => 1
[10] => 0
)
)
I have only one time the index 2505 and 2505 and index 8 and 9 and 10 are 1 for the 2505 and the index 8 and 9 are 1 for the 2525.
this does the job: (assuming that your first array is $arrays and your desired result is $result)
$result = array();
foreach($arrays as $array)
{
if(!isset($result[$array[0]]))
{
$result[$array[0]] = $array;
}
else
{
$result[$array[0]][8] = $array[8] == 1 ? 1 : $result[$array[0]][8];
$result[$array[0]][9] = $array[9] == 1 ? 1 : $result[$array[0]][9];
$result[$array[0]][10] = $array[10] == 1 ? 1 : $result[$array[0]][10];
}
}
but wherever you get your data from, e.g. a database, I would use element[0] as the key, so you don't get multiple entries in $arrays. Another option would be to create a class which holds your data...
been woking on a project of mine for a few days now, and essentialy what i'm triing to do in this project is a comparison of csv file that a user normaly does in excel, to do it in php automaticly every night.
I got the info from the CSV's intro arrays, but i'm having trouble combinig them to get the right info for every book, hence the folowing exemple and question.
I have the folowing array (array1) from a csv file :
Array
(
[0] => Array
(
[0] => book1
[1] => description1
[2] => category1
[3] => code1
[4] => editor1
[5] => 0
[6] => eur
[7] => out of stoc
)
[1] => Array
(
[0] => book2
[1] => description2
[2] => category2
[3] => code2
[4] => editor2
[5] => 0
[6] => curr2
[7] => out of stoc
)
[2] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 0
[6] => curr3
[7] => out of stoc
)
[3] =>
)
and another array (array2) from a second csv file :
Array
(
[0] => Array
(
[0] => book1
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 12
[6] => eur
[7] => in stoc
)
[1] => Array
(
[0] => book2
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 13
[6] => eur
[7] => in stoc
)
[2] => Array
(
[0] => book4
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 14
[6] => usd
[7] => in stoc
)
[3] => Array
(
[0] => book5
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 16
[6] => usd
[7] => in stoc
)
)
I would like to know how to get the values form array2 intro array1 for the books of array1 found in array2.
Ex:
Array
(
[0] => Array
(
[0] => book1
[1] => description2_from_array2
[2] => category2_from_array2
[3] => code2_from_array2
[4] => editor2_from_array2
[5] => 12
[6] => eur
[7] => in stoc
)
[1] => Array
(
[0] => book2
[1] => description_from_array2
[2] => category_from_array2
[3] => code_from_array2
[4] => editor_from_array2
[5] => 13
[6] => curr_from_array2
[7] => in stoc
)
[2] => Array
(
[0] => book3
[1] => description3
[2] => category3
[3] => code3
[4] => editor3
[5] => 0
[6] => curr3
[7] => out of stoc //because book3 is not found in array2
)
[3] =>
)
Any help for this question would be greatly appreciated, belive me!
//Add keys to array 2 to aid lookup
$array2Tmp = array();
foreach($array2 as $item){
$array2Tmp[$item[0]] = $item;
}
$array3 = array();
foreach($array1 as $item){
//Use item from second array if it exists
if(array_key_exists($item[0],$array2Tmp)){
$array3[] = $array2Tmp[$item[0]];
}else{
$array3[] = $item;
}
}
I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.
in english:
if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]
screen dumps of both arrays
array1 (
[1] => Array (
[0] => 1 [1] => 2
)
[2] => Array (
[0] => 2 [1] => 3
)
[3] => Array (
[0] => 3 [1] =>
) [7] => Array (
[0] => 7 [1] => 1
)
[8] => Array (
[0] => 8 [1] => 1
)
[9] => Array (
[0] => 9 [1] => 10
)
[10] => Array (
[0] => 10 [1] => 2
)
)
array2 (
[0] => Array (
[0] => 1
[1] => 2
[2] => 2
[3] => Jane
[4] => Smith
[5] => jsmith#internet.com
[6] => jsmith
[7] => 12345
[8] => 1
[9] => no
)
[1] => Array (
[0] => 2
[1] => 2
[2] => 3
[3] => James
[4] => Beard
[5] => jasb#bellsouth.net
[6] => jbeard03
[7] => keeper
[8] => 1
[9] => no
)
[2] => Array (
[0] => 3
[1] => 2
[2] =>
[3] => Peter
[4] => Allen
[5] => pallen#rfgg.com
[6] => pallen
[7] => pallen
[8] => 1
[9] => no
)
[3] => Array (
[0] => 7
[1] => 2
[2] => 1
[3] => Joe
[4] => Blow
[5] => jasb#bellsouth.net
[6] => jblow
[7] => blow123
[8] => 5
[9] => yes
)
[4] => Array (
[0] => 8
[1] => 2
[2] => 1
[3] => John
[4] => Smith
[5] => logtest#bellsouth.net
[6] => jnsmith
[7] => jsmith123
[8] => 4
[9] => yes
)
[5] => Array (
[0] => 9
[1] => 2
[2] => 10
[3] => Frank
[4] => Smith
[5] => pallen#test.com
[6] => fsmith
[7] => fsmith123
[8] => 4
[9] => yes
)
[6] => Array (
[0] => 10
[1] => 2
[2] => 2
[3] => Loretta
[4] => Beard
[5] => lbeard#me.net
[6] => lbeard
[7] => lbeard123
[8] => 1
[9] => no
)
)
Does this work? I'm not sure I'm entirely clear on what you're looking for.
foreach($array1 as $x => $xarray) {
foreach($array2 as $y => $yarray) {
if($xarray[1] == $yarray[0]) {
$array1[$x][2] = $array[$y][2];
}
}
}
foreach ($array1 as &$a1) {
foreach ($array2 as $a2) {
if ($a1[1] == $a2[0]) {
$a1[] = $a2[2];
continue 2;
}
}
}
BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.
And because I threatened to do so:
btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P