I have an array where I want to group items having match strings in key.
My array looks like this:
Array
(
[ALL_trading_enabled] => 1
[ALL_enabled_pairs] => ALL
[ALL_max_trading_pairs] => 10
[SNGLSBTC_DCA_enabled] =>
[SNGLSBTC_sell_only_mode] => 1
[SNGLSBTC_sell_value] => 0.28
[SNGLSBTC_trailing_profit] => 0.009
[ENJBTC_DCA_enabled] =>
[ENJBTC_sell_only_mode] => 1
[ENJBTC_sell_value] => 0.28
[ENJBTC_trailing_profit] => 0.009
[BCPTBTC_DCA_enabled] =>
[BCPTBTC_sell_only_mode] => 1
[BCPTBTC_sell_value] => 0.28
[BCPTBTC_trailing_profit] => 0.009
)
I want to group the items that have the same string. What I want looks like this:
Array
(
[0] => Array(
[ALL_trading_enabled] => 1
[ALL_enabled_pairs] => ALL
[ALL_max_trading_pairs] => 10
)
[1] => Array(
[SNGLSBTC_DCA_enabled] =>
[SNGLSBTC_sell_only_mode] => 1
[SNGLSBTC_sell_value] => 0.28
[SNGLSBTC_trailing_profit] => 0.009
)
[2] => Array(
[ENJBTC_DCA_enabled] =>
[ENJBTC_sell_only_mode] => 1
[ENJBTC_sell_value] => 0.28
[ENJBTC_trailing_profit] => 0.009
)
[3] => Array(
[BCPTBTC_DCA_enabled] =>
[BCPTBTC_sell_only_mode] => 1
[BCPTBTC_sell_value] => 0.28
[BCPTBTC_trailing_profit] => 0.009
)
)
Any help to achieve this? Or better yet if I can assign the match as the key for the created group.
Array(
[ALL] => Array(
//items here
)
[SNGLSBTC] => Array(
//items here
)
)
Explode on _ and get the first portion, use as the key and add to that array:
foreach($array as $key => $value) {
$new_key = explode('_', $key)[0];
$result[$new_key][$key] = $value;
}
If needed to re-index (after your edit you don't need this):
$result = array_values($result);
Related
I have an array($data) like this.
Array
(
[0] => Array
(
[PID] => 1
[USER_NAME] => JOHN
[JOINED_DATE] => 2022-01-31
[JOINED_VALUE] => 23233.80
[TOPUP_AMOUNT] => 58000.00
[TOTAL_EXPENSES] => 3114.41
)
.....
I need to get a new key called TOTAL_BALANCE and it should get as follows,
TOTAL_BALANCE=JOINED_VALUE+TOPUP_AMOUNT+TOTAL_EXPENSES
Final output should look as follows,
Array
(
[0] => Array
(
[PID] => 1
[USER_NAME] => JOHN
[JOINED_DATE] => 2022-01-31
[JOINED_VALUE] => 23233.80
[TOPUP_AMOUNT] => 58000.00
[TOTAL_EXPENSES] => 3114.41
[TOTAL_BALANCE] => 78119.39
)
.....
Can someone help me to achieve this?
Here is my try, but I am not sure this is correct or not.
$r = [];
$keys = array_keys($key1+$key2);
foreach($keys as $v){
??
}
Do it like this:
$data = Array(
0 => Array(
'PID' => 1,
'USER_NAME' => 'JOHN',
'JOINED_DATE' => '2022-01-31',
'JOINED_VALUE' => 23233.80,
'TOPUP_AMOUNT' => 58000.00,
'TOTAL_EXPENSES' => 3114.41,
),
1 => Array(
'PID' => 2,
'USER_NAME' => 'JOHN_2',
'JOINED_DATE' => '2022-01-31',
'JOINED_VALUE' => 1234.80,
'TOPUP_AMOUNT' => 1000.00,
'TOTAL_EXPENSES' => 3114.41,
)
);
foreach($data as &$value){
// Sum and store
$value['TOTAL_BALANCE'] = $value['JOINED_VALUE'] + $value['TOPUP_AMOUNT'] + $value['TOTAL_EXPENSES'];
}
print_r($data);
Output:
Array
(
[0] => Array
(
[PID] => 1
[USER_NAME] => JOHN
[JOINED_DATE] => 2022-01-31
[JOINED_VALUE] => 23233.8
[TOPUP_AMOUNT] => 58000
[TOTAL_EXPENSES] => 3114.41
[TOTAL_BALANCE] => 84348.21
)
[1] => Array
(
[PID] => 2
[USER_NAME] => JOHN_2
[JOINED_DATE] => 2022-01-31
[JOINED_VALUE] => 1234.8
[TOPUP_AMOUNT] => 1000
[TOTAL_EXPENSES] => 3114.41
[TOTAL_BALANCE] => 5349.21
)
)
I want to remove the first.. how to say that outside of the array and left inside array of a multidimension array. I couldn't found out the solution.. someone knows how to do it??
This is my code. I assign a array and i try to put into another array like this.
$final_sku = array();
foreach($skus as $sku){
foreach($sku as $key => $s){
$final_sku[] = array('Sku' => $s);
}
}
$newArray = array(
"Product" => array(
"PrimaryCategory" => "1",
"AssociatedSku" => "12",
"Attributes" => array(
),
"Skus" => $final_sku
)
);
And this is the $final_sku output be like.
[0] => Array
(
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Antique White
[price] => 4
[special_price] =>
[SellerSku] => sku1
[variation] => var1
)
)
[1] => Array
(
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Apricot
[price] => 4
[special_price] =>
[SellerSku] => sku2
[variation] => var1
)
)
I want the output be like this.
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Antique White
[price] => 4
[special_price] =>
[SellerSku] => sku1
[variation] => var1
)
[Sku] => Array
(
[package_weight] => 5
[package_length] => 4
[package_width] => 3
[package_height] => 2
[package_content] =>
[tax_class] => default
[color_family] => Apricot
[price] => 4
[special_price] =>
[SellerSku] => sku2
[variation] => var1
)
UPDATE: I want to pass the array and convert to xml. So the array key would be duplicated.
It does not make sense to have the same array of 2 members under the same key.
If you want to access one of them, what will be its uniqueness over the other?
The logical solution is
[sku] => [
0 => [first sku data....],
1 => [sku data....],
]
I struggle to clean a multidimensional array. I find several Q&A:s on this topic but yet I can't get it to work.
The array $overPayments comes out (from a db call) as below.
Array (
[0] => Array (
[invoiceID] => 103080
[invoiceNumber] => 781
[faktBel] => 1500.00
[totalPayed] => 1500.00
[sumPayedOnThisJournal] => 1500.00
[totOPtoday] => 0.00
[totOPbeforeToday] => -1500.00
[totOPthisJournal] => 0.00 )
[1] => Array(
[invoiceID] => 103290
[invoiceNumber] => 7818
[faktBel] => 648.00
[totalPayed] => 893.00
[sumPayedOnThisJournal] => 893.00
[totOPtoday] => 245.00
[totOPbeforeToday] => -648.00
[totOPthisJournal] => 245.00 )
[2] => Array (
[invoiceID] => 103453
[invoiceNumber] => 202071
[faktBel] => 2250.00
[totalPayed] => 2317.00
[sumPayedOnThisJournal] => 2317.00
[totOPtoday] =>67.00
[totOPbeforeToday] => -2250.00
[totOPthisJournal] => 67.00 )
)
What I need to do is loop through the array called $overPayments containing about 200 sub arrays, and remove all "rows" (subarrays) that have $overPayment['totOPthisJournal'] <= 0. So that I end up with a either modified or new multidimensional array where the totOPthisJournal value is > 0.
I think array_filter is what you are after.
$filteredArray = array_filter($overPayments, function($value) {
return $value['totOPthisJournal'] > 0;
});
just try to unset the array index for which 'totOPthisJournal' is <=0
<?php
$array = Array ( '0' => Array ( 'invoiceID' => 103080, 'invoiceNumber' => 781, 'faktBel' => 1500.00,
'totalPayed' => 1500.00,'sumPayedOnThisJournal' => 1500.00, 'totOPtoday' => 0.00,
'totOPbeforeToday' => -1500.00, 'totOPthisJournal' => 0.00 ), '1' => Array( 'invoiceID' => 103290,
'invoiceNumber' => 7818, 'faktBel' => 648.00, 'totalPayed' => 893.00,
'sumPayedOnThisJournal' => 893.00,'totOPtoday' => 245.00, 'totOPbeforeToday' => -648.00,
'totOPthisJournal' => 245.00 ), '2' => Array ( 'invoiceID' => 103453,'invoiceNumber' => 202071,
'faktBel' => 2250.00, 'totalPayed' => 2317.00, 'sumPayedOnThisJournal' => 2317.00,
'totOPtoday' =>67.00, 'totOPbeforeToday' => -2250.00, 'totOPthisJournal' => 67.00));
foreach($array as $key=>$value){
if($array[$key]['totOPthisJournal'] <= 0){
unset($array[$key]);
}
}
print_r($array);
Put this array into a foreach loop:
foreach($overPayments as $key => $value) {
if($value['totOPthisJournal'] <= 0) {
$key = null;
}
}
This removes the overPayment where [totOPthisJournal] <= 0.
Hope this helps.
I have a dummy array, that I want to order.
How can I have following result
with foreach?
with while next()
with RecursiveIterator
with IteratorIterator
which one is the fastest?
Here is array
$files = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'wp-config.php',
'wp-content' =>
array (
'uploads' =>
array (
2013 =>
array (
'05' =>
array (
0 => 'kabeduvarkad-1024x768.jpg',
1 => 'kabeduvarkad-150x150.jpg',
2 => 'kabeduvarkad-300x225.jpg',
3 => 'kabeduvarkad-940x198.jpg',
),
10 =>
array (
),
),
2014 =>
array (
'02' =>
array (
),
),
2015 => 'de.php',
),
),
'wp-update' =>
array (
0 => 'wp-update.tar',
1 => 'wp-update.tar.gz',
2 => 'wp-update1.tar',
3 => 'wp-update1.tar.gz',
),
4 => 'wp-update.tar.gz',
);
Expected Result
$expected = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'test.php',
4 => 'wp-config.php',
5 => 'wp-content/',
6 => 'wp-content/uploads/',
7 => 'wp-content/uploads/2013/',
8 => 'wp-content/uploads/2013/05/',
9 => 'wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
10 => 'wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
11 => 'wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg',
12 => 'wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg',
13 => 'wp-content/uploads/2013/05/kabeduvarkad.jpg',
14 => '...'
);
array_walk_recursive is what you're looking for.
Example (not tested):
$expected = [];
array_walk_recursive($your_array, function($item, $key) {
// push item on to $expected
$expected[] = $item;
});
Working example
Trying to convert a "grouped" multidimensional array to a "segmented" multidimensional array. Not sure how to explain it exactly so hopefully these print_r results explain it better.
Current Array
Array
(
[qty] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[item_id] => Array
(
[0] => RR332
[1] => FF586
[2] => QW865
)
[item_name] => Array
(
[0] => Widget 1
[1] => Widget 2
[2] => Widget 3
)
[price] => Array
(
[0] => 1.00
[1] => 1.50
[2] => 1.50
)
)
Wanted Array
Array
(
[0] => Array
(
[qty] => 1
[item_id] => RR332
[item_name] => Widget 1
[price] => 1.00
)
[1] => Array
(
[qty] => 1
[item_id] => FF586
[item_name] => Widget 2
[price] => 1.50
)
[2] => Array
(
[qty] => 1
[item_id] => QW865
[item_name] => Widget 3
[price] => 1.50
)
)
This seems like it should be a simple task but I haven't been able to crack it yet.
I would preferably like to loop through the Current Array to create the Wanted Array if possible.
Any help is greatly appreciated!
Thanks.
You mean this?:
$data = array(
'qty' => array(
0 => 1,
1 => 1,
2 => 1,
),
'item_id' => array(
0 => 'RR332',
1 => 'FF586',
2 => 'QW865',
),
'item_name' => array(
0 => 'Widget 1',
1 => 'Widget 2',
2 => 'Widget 3',
),
'price' => array(
0 => '1.00',
1 => '1.50',
2 => '1.50',
),
);
$result = array();
foreach($data as $key => $values)
{
foreach($values as $number => $value)
$result[$number][$key] = $value;
}
print_r($result);
Output:
Array
(
[0] => Array
(
[qty] => 1
[item_id] => RR332
[item_name] => Widget 1
[price] => 1.00
)
[1] => Array
(
[qty] => 1
[item_id] => FF586
[item_name] => Widget 2
[price] => 1.50
)
[2] => Array
(
[qty] => 1
[item_id] => QW865
[item_name] => Widget 3
[price] => 1.50
)
)
I think this will do the job for you, though I don't have a good data set to test against.
$wanted_array = array();
// Iterate over the outer array to get the text keys & inner arrays
foreach ($current_array as $txtkey=>$arr) {
// Iterate over each inner array to get the numeric key
foreach ($arr as $numkey=>$val) {
// Set a numeric key pointing to a new array
// onto the new outer array if it isn't already there
if (!isset($wanted_array[$numkey])) $wanted_array[$numkey] = array();
// Swap the keys of the outer and inner arrays.
$wanted_array[$numkey][$txtkey] = $val;
}
}
Update: Using test array from #hakre's answer, here it is in practice:
$current_array = array(
'qty' => array(
0 => 1,
1 => 1,
2 => 1
),
'item_id' => array(
0 => 'RR332',
1 => 'FF586',
2 => 'QW865'
),
'item_name' => array(
0 => 'Widget 1',
1 => 'Widget 2',
2 => 'Widget 3'
),
'price' => array(
0 => '1.00',
1 => '1.50',
2 => '1.50'
)
);
// Output:
Array
(
[0] => Array
(
[qty] => 1
[item_id] => RR332
[item_name] => Widget 1
[price] => 1.00
)
[1] => Array
(
[qty] => 1
[item_id] => FF586
[item_name] => Widget 2
[price] => 1.50
)
[2] => Array
(
[qty] => 1
[item_id] => QW865
[item_name] => Widget 3
[price] => 1.50
)
)
Like you said, just loop through it to build it.
$wanted = array();
foreach($current['item_id'] as $key => $value) {
$wanted[$key] = array(
'qty' = $current['qty'][$key],
'item_id' = $current['item_id'][$key],
'item_name' = $current['item_name'][$key],
'price' = $current['price'][$key]
);
}
print_r($wanted);
You can loop over your original array using the foreach loop, which gives you the $key, and $value for each item in your array. From there, construct your new array as you like.
$array2 = array();
foreach ($array1 as $key => $value)
{
$array2[] = array('key' => 'value');
}