Merge an array with specified values into one key - php

Right now, I have array sets like these:
[9] => Array
(
[sl] => 10
[upload_dt] => 2015-04-15 14:39:58
[total_files] => 3
[file_name] => logo01.png
[remarks] => qqq
[status] => pending
[download_file] => http://localhost/web/download_file21
)
[10] => Array
(
[sl] => 10
[upload_dt] => 2015-04-15 14:39:58
[total_files] => 3
[file_name] => face.jpg
[remarks] => 5645645
[status] => pending
[download_file] => http://localhost/web/download_file22
)
[11] => Array
(
[sl] => 10
[upload_dt] => 2015-04-15 14:39:58
[total_files] => 3
[file_name] => ID_11401871809904(15).pdf
[remarks] => 567567
[status] => pending
[download_file] => http://localhost/web/download_file23
)
Now, I need to merge same values in some of the indices into one arrays.
The merged array values should look like this in the end.
[9] => Array
(
[sl] => 10
[upload_dt] => 2015-04-15 14:39:58
[total_files] => 3
[file_name] => logo01.png , face.jpg, ID_11401871809904(15).pdf
[remarks] => qqq, 5645645 , 567567
[status] => pending, pending ,pending
[download_file] => http://localhost/web/download_file21,
http://localhost/web/download_file22,
http://localhost/web/download_file23
)
Now, I tried using array_merge but it didn't actually work in this case.

Solution will be like this:
<?php
$result = array();
$ar1[9] = Array
(
'sl' => 10,
'upload_dt' => '2015-04-15 14:39:58',
'total_files' => 3,
'file_name' => 'logo01.png',
'remarks' => 'qqq',
'status' => 'pending',
'download_file' => 'http://localhost/web/download_file21'
);
$ar1[10] = Array
(
'sl' => 10,
'upload_dt' => '2015-04-15 14:39:58',
'total_files' => 3,
'file_name' => 'face.jpg',
'remarks' => '5645645',
'status' => 'pending',
'download_file' => 'http://localhost/web/download_file22'
);
$ar1[11] = Array
(
'sl' => 10,
'upload_dt' => '2015-04-15 14:39:58',
'total_files' => 3,
'file_name' => 'ID_11401871809904(15).pdf',
'remarks' => 567567,
'status' => 'pending',
'download_file' => 'http://localhost/web/download_file23'
);
foreach($ar1 as $record){
$keys = array_keys($record);
foreach($keys as $key) {
if(array_key_exists($key,$result)){
$valeInKey = explode(',', $result[$key]);
if (!in_array($record[$key], $valeInKey)){
$result[$key]= $result[$key] .",".$record[$key];
}
} else{
$result[$key]= $record[$key];
}
}
}
echo"<pre>";print_r($result);exit;
?>

You're actually merging strings inside those arrays so just ditch the array_merge idea, and just use a simple loop then use the key as your basis to concatenate strings. Rough example:
$result = array();
foreach($array as $values) {
if(!isset($result[$values['sl']])) {
$result[$values['sl']] = $values; // initial
} else {
foreach(array('file_name', 'remarks', 'status', 'download_file') as $field) {
$result[$values['sl']][$field] .= ", {$values[$field]}";
}
}
}
Sample Output

array_merge won't work, because the keys would overwrite each other.
A solution would be to merge them with a the help of a foreach. Something like this:
$new = array();
foreach($arr as $key => $value) {
$new[$key] .= ", ".$value;
}
But you would have multiple entries for every single array you want to merge. If you just want to have them for a few, you have to check the key and do something accordingly.

Related

shift multidimentional array to single array

I want to remove key 0 from parent array and set child array as parent.
Here I will get single value so one array is ok for me.
My current array looks like this
Array
(
[0] => Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27#gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
)
I want it like below. expected result
Array
(
[id] => 3
[api_key] => acount266
[auth_domain] => Tester26
[database_url] => vcc.test.acc+27#gmail.com
[project_id] => 12345
[storage_bucket] =>
[secret_key_path] =>
[fcm_server_key] => 1
[messaging_sender_id] => 0
[key_phrase] =>
[disable] => 0
[created] =>
[updated] =>
)
For this I tried like below but no success.
$new = array();
foreach ($data as $v){
$new = array_merge($new , array_values($v)) ;
}
but in my code it's removed key e.g id,api_key, etc....
I need key name also in my new array. please suggest
Remove the array_values
Solution
<?php
$test = array(
array
(
'id' => 3,
'api_key' => 'acount266'
)
);
$new = array();
foreach($test as $v){
$new = array_merge($new, $v);
}
var_dump($new);
Result
array(2) {
["id"]=>
int(3)
["api_key"]=>
string(9) "acount266"
}
According to documentation of PHP as mentioned
reset() function returns the value of the first array element, or
FALSE if the array is empty.
$array = array(
array(
'id' => 3,
'api_key' => 'acount266',
'auth_domain' => 'Tester26',
'database_url' => 'vcc.test.acc+27#gmail.com',
'project_id' => '12345',
'storage_bucket' => '',
'secret_key_path' => '',
'fcm_server_key' => 1,
'messaging_sender_id' => 0,
'key_phrase' => '',
'disable' => 0,
'created' => '',
'updated' => ''
)
);
print_r(reset($test));
I tried this:
$arr = array();
foreach ($examples as $example) {
foreach ($example as $e) {
array_push($arr, $e);
}
}
Don't overcomplicate this, reassigning the first element to the parent array is quick and easy:
<?php
$array =
array (
0 =>
array (
'first' => 'Michael',
'last' => 'Thompson'
)
);
$array = $array[0];
var_export($array);
Output:
array (
'first' => 'Michael',
'last' => 'Thompson',
)
Or:
$array = array_shift($array);

How to convert a recursive multidimensional array to a straight multidimensional array in PHP?

I have search for a solution for this but couldn't find anything giving me a "straight" multidimensional array back. Flatten is probably not the solution as long as i want to preserve the original sub structure?
In additional i want to summarize qty when the key is repeating.
This is my original array:
Array
(
[60002] => Array
(
[50001] => Array
(
[50002] => Array
(
[10001] => Array
(
[flag] => B
[qty] => 1
)
[10002] => Array
(
[flag] => B
[qty] => 1
)
[10003] => Array
(
[flag] => B
[qty] => 2
)
[flag] => M
[qty] => 1
)
[flag] => M
[qty] => 1
)
[flag] => G
[qty] => 1
)
[10001] => Array
(
[flag] => B
[qty] => 1
)
)
What i basically want is to create a new array looking like this:
Array
(
[10001] => Array
(
[flag] => B
[qty] => 2
)
[10002] => Array
(
[flag] => B
[qty] => 1
)
[10003] => Array
(
[flag] => B
[qty] => 2
)
[50001] => Array
(
[flag] => M
[qty] => 1
)
[50002] => Array
(
[flag] => M
[qty] => 1
)
[60002] => Array
(
[flag] => G
[qty] => 1
)
)
This is tested.
The key is intval().
$value['qty'] += intval($newArray[$key]['qty']);
If the [$key]['qty'] does not exist the intval() will return a zero. This is much faster than using an if else to check if a [$key]['qty'] already exists.
The only possible problem I could anticipate is if the Flag value is different when the key value is the same:
[10001] => Array(
[flag] => M
[qty] => 1
),
[10001] => Array(
[flag] => B
[qty] => 1
)
When this is an issue I resolve the priority with a logic table in an array.
$priority['M']['B'] = 'M'
$priority['B']['M'] = 'M'
$priority['']['M'] = 'M'
$priority['M'][''] = 'M'
$priority['B'][''] = 'B'
$priority['B'][''] = 'B'
settype($newArray[$key]['flag'],'string');
[$newArray[$key]['flag'] = $priority[$value['flag']][$newArray[$key]['flag']]
Data:
$array = array('60002' => Array('50001' => Array('50002' => Array('10001' => Array('flag' => 'B','qty' => 1),'10002' => Array('flag' => 'B','qty' => 1),'10003' => Array('flag' => 'B','qty' => 2),'flag' => 'M','qty' => 1),'flag' => 'M','qty' => 1),'flag' => 'G','qty' => 1),'10001' => Array('flag' => 'B','qty' => 1));
PHP
$newArray = array();
getValues($data);
function getValues($array){
global $newArray;
foreach ($array as $key => $value){
if(is_numeric($value['qty'])) {
$value['qty'] += intval($newArray[$key]['qty']);
$newArray[$key] = array('flag'=>$value['flag'],'qty'=>$value['qty']);
}
if (gettype($value) != 'array'){return;}
getValues($value);
}
}
ksort($newArray);
var_export($newArray);
Result:
array (
10001 =>
array (
'flag' => 'B',
'qty' => 2,
),
10002 =>
array (
'flag' => 'B',
'qty' => 1,
),
10003 =>
array (
'flag' => 'B',
'qty' => 2,
),
50001 =>
array (
'flag' => 'M',
'qty' => 1,
),
50002 =>
array (
'flag' => 'M',
'qty' => 1,
),
60002 =>
array (
'flag' => 'G',
'qty' => 1,
),
)
This seemed to work:
function extractArray(array $source, array &$destination, $originalIndex)
{
foreach($source as $index => $value)
{
if(is_array($value))
extractArray($value, $destination, $index);
else
$destination[$originalIndex][$index] = $value;
}
}
$test = array(
60002 => array
(
50001 => array
(
50002 => array
(
10001 => array
(
'flag' => 'B',
'qty' => 1
),
10002 => array
(
'flag' => 'B',
'qty' => 1
),
10003 => array
(
'flag' => 'B',
'qty' => 2
),
'flag' => 'M',
'qty' => 1
),
'flag' => 'M',
'qty' => 1
),
'flag' => 'G',
'qty' => 1
),
10001 => array
(
'flag' => 'B',
'qty' => 1
)
);
$new = array();
foreach($test as $index => $value)
extractArray($value, $new, $index);
var_dump($new);
die();
You can use a recursive approach to iterate over all levels of the array. For each item you check that it is an array and if it has any of the keys you want checked, add the array consisting of the found attributes.
function flattenArray($array, $keysToCheck) {
$result = array();
foreach($array as $item) {
// check if the current array item is a candidate to
// be added to the flattened array
if(is_array($item)) {
$foundAttributes = array();
foreach($item as $key=>$value) {
if(in_array($key, $keysToCheck) {
$foundAttributes[$key] = $value;
}
}
// we found at least one matching attribute
if(count($foundAttributes)) {
array_push($result, $foundAttributes);
}
// recursively go to the next level and merge the results from there
$result = array_merge($result, flattenArray($item, $keysToCheck);
}
}
return $result;
}
// usage example
$flattenedArray = flattenArray($originalArray, array('flag', 'qty'));
The above solution allows you to customise it for other type of objects, by passing different $keysToCheck arguments to the function.
If you also need the flattened array sorted, you can use usort() to achieve this.
Please pardon any syntax errors, I don't have a PHP interpreter at hand.

Clean multidimensional array in php

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.

Loop PHP array based in first array

I just found out how to loop an array based on another array, but my problem is that if the second array is just 1 object than it works fine but I want to make it work with two objects.
Here is an example how it do works,
$shorten = array(
0 => 'ECAR',
1 => 'CCAR',
2 => 'ICAR',
3 => 'SCAR',
4 => 'FCAR',
5 => 'PCAR',
);
$data = array(
'Hertz' => array(
'ECAR' => '49.21',
'CCAR' => '71.04',
'ICAR' => '89.58',
'SCAR' => '100.00',
)
),
'Avis' => array(
'ECAR' => '412.00',
'CCAR' => '347.00',
'ICAR' => '285.00',
'SCAR' => '224.00',
'FCAR' => '165.00',
'PCAR' => '100.00',
)
),
);
// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
foreach($data as &$array) {
// merge to get set members
$array = array_merge($shorten, $array);
}
unset($array);
print_r($data);
But I want to make it work with this one,
$shorten = array(
0 => 'ECAR',
1 => 'CCAR',
2 => 'ICAR',
3 => 'SCAR',
4 => 'FCAR',
5 => 'PCAR',
);
$data = array(
'Hertz' => array(
'NYCT01' => array(
'ECAR' => '49.21',
'CCAR' => '71.04',
'ICAR' => '89.58',
'SCAR' => '100.00',
)
),
'Avis' => array(
'NYCT01' => array(
'ECAR' => '412.00',
'CCAR' => '347.00',
'ICAR' => '285.00',
'SCAR' => '224.00',
'FCAR' => '165.00',
'PCAR' => '100.00',
)
),
);
// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
foreach($data as $firstArray) {
foreach($firstArray as &$array){
// merge to get set members
$array = array_merge($shorten, $array);
}
}
unset($array);
print_r($data);
And this is what I want it to be,
Array
(
[Hertz] => Array
(
[ECAR] => 49.21
[CCAR] => 71.04
[ICAR] => 89.58
[SCAR] => 100.00
[FCAR] => n/a
[PCAR] => n/a
)
[Avis] => Array
(
[ECAR] => 412.00
[CCAR] => 347.00
[ICAR] => 285.00
[SCAR] => 224.00
[FCAR] => 165.00
[PCAR] => 100.00
)
)
foreach($data AS $company => $nyc){
$inner = $nyc['NYCT01'];
foreach($shorten AS $car){
if(array_key_exists($car, $inner)){
$output[$company][$car] = $inner[$car];
}else {
$output[$company][$car] = "n/a";
}
}
}
Just loop through the data and check if the key of any of the cars exists inside the NYCT01 array, if it does set the value, else, "n/a".
My output:
Array
(
[Hertz] => Array
(
[ECAR] => 49.21
[CCAR] => 71.04
[ICAR] => 89.58
[SCAR] => 100.00
[FCAR] => n/a
[PCAR] => n/a
)
[Avis] => Array
(
[ECAR] => 412.00
[CCAR] => 347.00
[ICAR] => 285.00
[SCAR] => 224.00
[FCAR] => 165.00
[PCAR] => 100.00
)
)
Ok, got it work this is the real code I figured,
foreach($data as $company => $nyc){
foreach($nyc as $inner => $s){
foreach($shorten as $car){
if(array_key_exists($car, $nyc[$inner])){
$output[$company][$car] = $nyc[$inner][$car];
}else {
$output[$company][$car] = "n/a";
}
}
}
}
Thanks so much for your help Marcus,
Good luck.

How to use array keys as new values for a derived array

I have the following array which I would like to edit:
Array
(
[qty_black_34] =>
[qty_black_36] => 2
[qty_black_38] =>
[qty_black_40] =>
[qty_black_42] =>
[qty_black_44] =>
[qty_black_48] =>
[qty_powder_34] =>
[qty_powder_36] =>
[qty_powder_38] =>
[qty_powder_40] =>
[qty_powder_42] => 1
[qty_powder_44] =>
[qty_powder_48] =>
[qty_red_34] =>
[qty_red_36] =>
[qty_red_38] => 2
[qty_red_40] =>
[qty_red_42] =>
[qty_red_44] =>
[qty_red_48] => )
What I want to do is to build another array to hold only the elements with a value.
The new array must look like this"
Array
(
[0] => Array
(
[color] => black
[size] => 36
[quantity] => 2
)
[1] => Array
(
[color] => powder
[size] => 42
[quantity] => 1
)
[2] => Array
(
[color] => red
[size] => 38
[quantity] => 2
)
)
PHP is the language I'm using.
loop through array and take elements, which have set a value. for the additional key/values in your final array split the string used as key in original array
$new_array = array();
foreach ($old_array as $key => value) {
if ($value) {
$key_split = explode('_', $key);
$new_array[] = array('color' => $key_split[1], 'size' => $key_split[2], 'quantity' => $value);
}
}
$products = array();
foreach($quantities as $key => $quantity){
if($quantity != '') {
list($q, $color, $size) = explode('_', $key);
$products[] = array(
'color' => $color,
'size' => $size,
'quantity' => $quantity
);
}
}
Demo

Categories