Duplicate php array with keys - php

I have two arrays like so:
[day0_units] => 0
[day1_units] => 12
[day1_start] => 08:00:00
[day1_end] => 11:00:00
[day2_units] => 12
[day2_start] => 07:00:00
[day2_end] => 11:00:00
[day3_units] => 0
[day4_units] => 12
[day4_start] => 10:00:00
[day4_end] => 12:00:00
[day5_units] => 12
[day5_start] => 08:00:00
[day5_end] => 03:00:00
[day6_units] => 0
[day0_units] => 0
[day0_start] => 10:00:00
[day0_end] => 01:00:00
[day1_units] => 12
[day1_start] => 08:00:00
[day1_end] => 11:00:00
[day2_units] => 12
[day2_start] => 07:00:00
[day2_end] => 11:00:00
[day3_units] => 0
[day3_start] =>
[day3_end] =>
[day4_units] => 12
[day4_start] => 10:00:00
[day4_end] => 12:00:00
[day5_units] => 12
[day5_start] => 08:00:00
[day5_end] => 03:00:00
[day6_units] => 0
All I want to do is duplicate the array keys, both arrays should have the same keys in the same positions and retain the values if any. Any suggestions?

Get the keys from both and merge them, then flip and merge with the arrays:
$keys = array_flip(array_merge(array_keys($array1), array_keys($array2)));
$array1 = array_merge($keys, $array1);
$array2 = array_merge($keys, $array2);

Related

Counting arrays based on specific object property

Is it possible count arrays based on specific object property?
[0] => stdClass Object
(
[ID] => 12345
[Name] => TEST 1
[position] => POS 1
[2023-02-01] => 22:00 - 06:00
[2023-02-02] => REST DAY
[2023-02-03] => 14:00 - 22:00
[2023-02-04] => 14:00 - 22:00
[2023-02-05] => 14:00 - 22:00
[2023-02-06] => 22:00 - 06:00
[2023-02-07] => REST DAY
[2023-02-08] => 14:00 - 22:00
[2023-02-09] => 22:00 - 06:00
[2023-02-10] => DAY OFF
[2023-02-11] => 14:00 - 22:00
[2023-02-12] => -
[2023-02-13] => -
[2023-02-14] => -
[2023-02-15] => -
)
[1] => stdClass Object
(
[ID] => 67890
[Name] => TEST 2
[position] => POS 2
[2023-02-01] => 06:00 - 14:00
[2023-02-02] => REST DAY
[2023-02-03] => 06:00 - 14:00
[2023-02-04] => 06:00 - 14:00
[2023-02-05] => 06:00 - 14:00
[2023-02-06] => REST DAY
[2023-02-07] => 06:00 - 14:00
[2023-02-08] => 06:00 - 14:00
[2023-02-09] => 06:00 - 14:00
[2023-02-10] => DAY OFF
[2023-02-11] => 06:00 - 14:00
[2023-02-12] => -
[2023-02-13] => -
[2023-02-14] => -
[2023-02-15] => -
)
I've been trying to use filter, but since the version is 5.3 its not applicable. I've also not been using PHP so I've been a hard time with the syntax. Note that this is a legacy system.
You can loop through the array of objects and use conditional statements to check the values of specific properties, and increment a counter variable each time the condition is met. Here's an example to count the number of "REST DAY" values for the property "2023-02-02":
$array = [
(object) [
'ID' => 12345,
'Name' => 'TEST 1',
'position' => 'POS 1',
'2023-02-01' => '22:00 - 06:00',
'2023-02-02' => 'REST DAY',
// ...
],
(object) [
'ID' => 67890,
'Name' => 'TEST 2',
'position' => 'POS 2',
'2023-02-01' => '06:00 - 14:00',
'2023-02-02' => 'REST DAY',
// ...
]
];
$counter = 0;
foreach ($array as $object) {
if ($object->{"2023-02-02"} === "REST DAY") {
$counter++;
}
}
echo $counter; // Outputs: 2

Get Unique Values from Array 2 Compared to Array 1 and Output in Array 3

I have 2 arrays and need to find the unique values in the second array that are not in the first array and output those to a third array.
Array 1
$array1 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value79',
);
Array 2
$array2 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value81',
);
Output Array
$array3 = array(
0 => 'value81',
);
I have tried array_diff a few different ways and it only gives me an array of both array1 and array2 combined together.
UPDATE:
array_diff works fine on the example arrays above, but I can't get it to work for the actual arrays I'm using below.
Array 1
$array1 = array (
0 => 'e8B283B606A212d82036F74f88177375125440F6',
1 => '633AD0A20588237b6908Ee35B23be06bf5c17b90',
2 => '81C025abAc1a3d51dfcae7a2C226ADD463B794d7',
3 => '1fEc22B25E2200cA88D30B6Ba1d8B5696b2b7D52',
4 => 'AA7A7C2DECB180f68F11E975e6D92B5Dc06083A6',
5 => '09a054B60bd3B908791B55eEE81b515B93831E99',
6 => 'f155825650d85fdc50a2e7bbc27b35b666f4b0a4',
7 => '7e42976faf925dbacedd116a2b6a2bb5c9adef4f',
8 => '556754785B5428cB5C04B798Ea993e5d3c550A66',
9 => '5a4904f474077a021eee8829f7d11d50fe76993f',
10 => '36C39434dB7987Cae67233D06de9427c4C67E787',
11 => '3c1ddb1fb17d83788039d5750c1136017c637a55',
12 => '0f94e2a44ced2a368da8a1edeaa337f13a8560d0',
13 => '05227e4FA98a6415ef1927E902dc781AA7eD518a',
14 => 'EC2E833E90654EE6DD71e7b9EA3bE8B97299CC03',
15 => 'B279F56B1F71F85d59Dc1499E697E4F005298c14',
16 => '73d4F0e3eD1dE3BD278a71C02Adbaf6AD51A610C',
17 => '7D10f0fa8aB734328718212f21Aa8018CCcEd0f4',
18 => 'C9040Ca5e0A7c675343facbD28fe698594168522',
19 => '79F8fFa1502EE1DD6602c2459ECE6960D46012Ac',
20 => '66EA40Bb3F67b8Dc101fB01e6273A4F913Aac559',
21 => 'eC2A9A192929ed1f280f7Fd8935b881e3a649dc0',
22 => 'ba944AbD6Ad261B220dcc8ba6b53410Cb024FCB1',
23 => 'edb477Ca9ADE4d1152666eE914cB279179ffED92',
24 => 'C683946C5dF8eC48D20d64CaF58257b6135faEbB',
25 => '949990EccC1735681C6dFc64516aC882e4948cE2',
26 => '5a6dc9dbce3FdDfDE2905B7ce45b87BFe5bd6872',
27 => '177A56bb19024737C945eb040902fe2124c63fc2',
28 => 'cfd090de8cc555a48b5e742325d88c5937b9074c',
29 => '91671d0C02c7465b0948e819d2C2d261405301cF',
30 => 'A5c5f7fF2186898283e000352770e1cb140d1a00',
31 => 'Db5a91E6f925A22FE995BfB403eacc74DFE34F04',
32 => '26d13C17659e1305344eE31635B5A2b72e94d82E',
33 => '136f4b5b6a306091b280e3f251fa0e21b1280cd5',
34 => '4cdc990d2EE172534d503Fec256B16B5fD09394f',
35 => '04EeA222B3CC6F1F667915E952539eCa8468b3FE',
);
Array 2
$array2 = array (
0 => '28697b4a1618a77b1d61a85e99174616b519f629',
1 => 'e8b283b606a212d82036f74f88177375125440f6',
2 => '633ad0a20588237b6908ee35b23be06bf5c17b90',
3 => '81c025abac1a3d51dfcae7a2c226add463b794d7',
4 => 'fead04e9b311eebdc5f1e963b7472af15b21430d',
5 => '23d3a453c7bd4634b81219d63b12d5e3ceec04f6',
6 => '716f412c236236845d8592873bd1e78d024fadf5',
7 => '460eebde20336aa615370f4b7b7a916e5656d4e6',
8 => '5ff58e03ac620166f6093f85c2edec521fac7a26',
9 => 'f155825650d85fdc50a2e7bbc27b35b666f4b0a4',
10 => '556754785b5428cb5c04b798ea993e5d3c550a66',
11 => '5a4904f474077a021eee8829f7d11d50fe76993f',
12 => 'e8577ff0c2a982b1550993a40c34cf26ad4732ff',
13 => '36c39434db7987cae67233d06de9427c4c67e787',
14 => '1fec22b25e2200ca88d30b6ba1d8b5696b2b7d52',
15 => '7e42976faf925dbacedd116a2b6a2bb5c9adef4f',
16 => '3c1ddb1fb17d83788039d5750c1136017c637a55',
17 => 'd7644fa6fe75eb7338c46807a640fa35b2c6dd12',
18 => '9175e0e8434cb6c5cc45a64f8a80f66ec81b47bf',
19 => '91c700c233491fad57ee7b91e0a5de5cbbb63080',
20 => 'ec2e833e90654ee6dd71e7b9ea3be8b97299cc03',
21 => '73d4f0e3ed1de3bd278a71c02adbaf6ad51a610c',
22 => '6522bbd602e1cdb3c1e12a04c8a5d1a9f5352a1e',
23 => '92598e499f4c576104105279cf926df447f2d044',
24 => 'bb826a44f1c8069207248d34a56ce05bc72d2901',
25 => '66ea40bb3f67b8dc101fb01e6273a4f913aac559',
26 => '7d10f0fa8ab734328718212f21aa8018ccced0f4',
27 => 'f7435817b7d4165cbf7b56539426267cd8872895',
28 => '73ddf33aff4274dc706241c4a1263196e5e6e872',
29 => 'edb477ca9ade4d1152666ee914cb279179ffed92',
30 => 'c683946c5df8ec48d20d64caf58257b6135faebb',
31 => '949990eccc1735681c6dfc64516ac882e4948ce2',
32 => '5a6dc9dbce3fddfde2905b7ce45b87bfe5bd6872',
33 => 'f0e6f7b1a9e917cdd76981b20265719c0b96875c',
34 => 'a7c4cea7f9263ceabb958118636929521b61ec80',
35 => '2ad41576364037cf25fd08948f425b63610d6cf3',
36 => 'cc73a3c13fe628823b6759ad07199794a252a41d',
37 => 'b30a9afb76e4ba949f5523a28bda7ade35e6f5d0',
38 => '650ff5efc2f881236d3079e465cc3e087c6ddca5',
39 => '8dc82ddbf1efb4befcb577b1c0a6efb812a0c562',
40 => '1b2a811c808d30aa2a0270b6729609a84a6b9e82',
41 => '442b7055dd9b2c483884ad250734172b2ae7fc76',
42 => 'c9040ca5e0a7c675343facbd28fe698594168522',
43 => 'e32bb01574b9a36bf16884cc7160977efc9dd6b3',
44 => 'caad2020967f0f314fb8a150413f7f9fc26c0f73',
45 => 'ad4273e44ce8896c943ee6851d96a307b14205e5',
46 => '6f0784344fe67eaf46d74171f5469274b690648f',
47 => '81036acab4da3d559fe11299d48d43b3d31551a7',
48 => '0f94e2a44ced2a368da8a1edeaa337f13a8560d0',
);
code:
$result = array_diff($array2, $array1);
echo '<pre>' . var_export($result, true) . '</pre><br>';
Did you try with array_diff() or array_diff_assoc() by swapping the parameter? ,
<?php
$array1 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value79',
);
$array2 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value81',
);
$result=array_diff_assoc($array2,$array1);
print_r($result);
?>
WORKING DEMO: https://3v4l.org/aNjUm
You can achieve the desired array by array_diff as
<?php
$array1 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value79',
);
$array2 = array(
0 => 'value12',
1 => 'value34',
2 => 'value56',
3 => 'value81',
);
$value = array_diff($array2, $array1);
$array3[] = $value[3];
print_r($array3);
?>
Output
Array
(
[0] => value81
)
Note: If you swap the parameters of array_diff function you will get diff results as
1:
$value = array_diff($array2, $array1);
//Output
Array
(
[0] => value81
)
2:
$value = array_diff($array1, $array2);
//Output
Array
(
[0] => value79
)
The issue is case sensitivity in my actual arrays that I posted in the Update of my question.
This is what ended up working for me. I used the array_udiff with a callback function of strcasecmp. And this gives me the desired result.
strcasecmp ignores case and treats A as a or B as b and vise versa.
$result = array_udiff($array2, $array1, 'strcasecmp');

How to merge array with same end time with next array start time

I want to combine the array into one array if the end time of first array and start time of second array are same.
[2019-10-07] => Array
(
[0] => Array
(
[start_time] => 09:00:00
[end_time] => 12:00:00
[working_id] => 55
)
[1] => Array
(
[start_time] => 12:00:00
[end_time] => 13:00:00
[working_id] => 58
)
[2] => Array
(
[start_time] => 13:00:00
[end_time] => 18:00:00
[working_id] => 56
)
)
Output:-
[2019-10-07] => Array
(
[0] => Array
(
[start_time] => 09:00:00
[end_time] => 18:00:00
)
)
You need to use foreach loop and for each loop you can use array_column to get all values of a start_time and end_time. Then you can use max and min function to get the max and min value respectively.
$data = array(
'2019-10-07' =>
array
(
'0' => array
(
'start_time' => '09:00:00',
'end_time' => '12:00:00',
'working_id' => 55,
),
'1' => array
(
'start_time' => '12:00:00',
'end_time' => '13:00:00',
'working_id' => 58,
),
'2' => array
(
'start_time' => '13:00:00',
'end_time' => '18:00:00',
'working_id' => 56,
),
),
);
$format = array();
foreach( $data as $key => $a ) {
$format[$key][0] = array();
$startTimes = array_column( $a, 'start_time'); //get all values with key start_time
$endTimes = array_column( $a, 'end_time'); //get all values with key end_time
$format[$key][0]['start_time'] = min( $startTimes ); //get minimum value
$format[$key][0]['end_time'] = max( $endTimes ); //get maximum value
}
echo '<pre>';
print_r($format);
Here is the Demo.

How to sort a PHP Array with values as strings that are split by '-' [duplicate]

This question already has answers here:
Sort array of strings with leading numbers followed by non-numeric characters
(3 answers)
Closed 5 months ago.
I have the below array :
array (
0 => '4-0',
1 => '4-1',
2 => '4-2',
3 => '4-3',
4 => '4-4',
5 => '4-5',
6 => '4-6',
7 => '4-7',
8 => '4-8',
9 => '4-9',
10 => '4-10',
11 => '4-11',
12 => '4-12',
13 => '4-13',
14 => '4-14',
15 => '4-15',
16 => '4-16',
17 => '8-0',
18 => '9-0',
19 => '2-0',
20 => '5-0',
21 => '7-1',
)
What i want is to order ASC by value. So the value at array[19] should be the first one and value at array[18] the last one. I have tried some sorting functions such as asort() but it is not doing what i exactly want.
asort() :
array (
0 => '2-0',
1 => '4-0',
2 => '4-1',
3 => '4-10',
4 => '4-11',
5 => '4-12',
6 => '4-13',
7 => '4-14',
8 => '4-15',
9 => '4-16',
10 => '4-2',
11 => '4-3',
12 => '4-4',
13 => '4-5',
14 => '4-6',
15 => '4-7',
16 => '4-8',
17 => '4-9',
18 => '5-0',
19 => '7-1',
20 => '8-0',
21 => '9-0',
)
Is there a specific PHP Function for this or should i implement my own algorithm? Any ideas?
You can use sort() with the SORT_NATURAL flag for this:
<?php
$array = array (
0 => '4-0',
1 => '4-1',
2 => '4-2',
3 => '4-3',
4 => '4-4',
5 => '4-5',
6 => '4-6',
7 => '4-7',
8 => '4-8',
9 => '4-9',
10 => '4-10',
11 => '4-11',
12 => '4-12',
13 => '4-13',
14 => '4-14',
15 => '4-15',
16 => '4-16',
17 => '8-0',
18 => '9-0',
19 => '2-0',
20 => '5-0',
21 => '7-1',
);
sort( $array, SORT_NATURAL );
print_r( $array );
Output:
Array
(
[0] => 2-0
[1] => 4-0
[2] => 4-1
[3] => 4-2
[4] => 4-3
[5] => 4-4
[6] => 4-5
[7] => 4-6
[8] => 4-7
[9] => 4-8
[10] => 4-9
[11] => 4-10
[12] => 4-11
[13] => 4-12
[14] => 4-13
[15] => 4-14
[16] => 4-15
[17] => 4-16
[18] => 5-0
[19] => 7-1
[20] => 8-0
[21] => 9-0
)
You can use natsort http://php.net/manual/en/function.natsort.php.
It will sort the array content as string.
natsort($your_array)
Its simple, just sort function will do the job.
sort($array);
Please note, Parameter is pass by reference and not pass by value.
<?php
$array=array (
0 => '4-0',
1 => '4-1',
2 => '4-2',
....
....
);
sort($array);
print_r($array);
?>
Will give the same answer

php join array with same initial value

here's my array
Array
(
[0] => Array
(
[0] => Jan 2010
[1] => 65.75
)
[1] => Array
(
[0] => Jan 2010
[1] => 211.05
)
[2] => Array
(
[0] => Jan 2010
[1] => 582.7
)
[3] => Array
(
[0] => Feb 2010
[1] => 136.3
)
[4] => Array
(
[0] => Feb 2010
[1] => 215.32
)
[5] => Array
(
[0] => Feb 2010
[1] => 413.9
)
[6] => Array
(
[0] => Mar 2010
[1] => 156.35
)
[7] => Array
(
[0] => Mar 2010
[1] => 210.54
)
[8] => Array
(
[0] => Mar 2010
[1] => 585.15
)
[9] => Array
(
[0] => Apr 2010
[1] => 126.1
)
[10] => Array
(
[0] => Apr 2010
[1] => 255.47
)
[11] => Array
(
[0] => Apr 2010
[1] => 329.1
)
[12] => Array
(
[0] => May 2010
[1] => 109
)
[13] => Array
(
[0] => May 2010
[1] => 170
)
[14] => Array
(
[0] => May 2010
[1] => 716.7
)
)
is there any way I can make all arrays with the same value in [0] merge? i want it to be something like this:
Array[0] = (Jan 2010, 65.75, 211.05, 582.7)
Array[1] = (Feb 2010, 136.3, 215.32, 413.9)
and so on...
The closest I can think to do this "simply" (read: taking advantage of native PHP features) is switching to text keys (associative array) for your result. This makes sense from a data modeling perspective as well, since in your sample result arrays you are mixing "key" data and "value" data (e.g. the first value carries the responsibility of being the label for the set == bad). The trick is to use the implicit "push" operator [], which appends a new value to an array.
foreach($sourceArray as $currentSubArray) {
$resultArray[$currentSubArray[0]][] = $currentSubArray[1];
}
Your result will look like this:
Array (
'Jan 2010' => Array (
0 => 65.75,
1 => 211.05,
2 => 582.7,
)
...
)
This is a variant of what #ctrahey suggested, that operates on the input array directly:
foreach($array as $key => &$entry) {
list($month, $value) = $entry;
if (isset($ptr[$month])) {
$ptr[$month][] = $value;
unset($array[$key]);
} else {
$ptr[$month] = &$entry;
}
}
unset($ptr);
Output with your example data:
Array
(
[0] => Array
(
[0] => Jan 2010
[1] => 65.75
[2] => 211.05
[3] => 582.7
)
[3] => Array
(
[0] => Feb 2010
[1] => 136.3
[2] => 215.32
[3] => 413.9
)
[6] => Array
(
[0] => Mar 2010
[1] => 156.35
[2] => 210.54
[3] => 585.15
)
[9] => Array
(
[0] => Apr 2010
[1] => 126.1
[2] => 255.47
[3] => 329.1
)
[12] => Array
(
[0] => May 2010
[1] => 109
[2] => 170
[3] => 716.7
)
)
actionMerge($inputArray);
function actionMerge($inputArray){
$month = array();
$earn = array();
$parentKey = 0;
$callback = function ($value, $key) use (&$month, &$earn, &$parentKey) {
if(!is_array($value)){
if($key == 0){
if(!in_array($value, $month)){
array_push($month, $value);
$earn[$value] = array();
}
$parentKey = $value;
}elseif($key == 1){
array_push($earn[$parentKey], $value);
}
}
};
array_walk_recursive($inputArray, $callback);
echo 'You should use this array';
var_dump($earn); // group money by month, I recommend you to use it
$Array = array();
foreach($month as $m){
$arr = array($m);
$arr = array_merge($arr, $earn[$m]);
array_push($Array, $arr);
}
echo '...Intead of array result what you expect';
var_dump($Array); // your expect result
}
?>
Here is result:
//You should use this array
array (size=5)
'Jan 2010' =>
array (size=3)
0 => float 65.75
1 => float 211.05
2 => float 582.7
'Feb 2010' =>
array (size=3)
0 => float 136.3
1 => float 215.32
2 => float 413.9
'Mar 2010' =>
array (size=3)
0 => float 156.35
1 => float 210.54
2 => float 585.15
'Apr 2010' =>
array (size=3)
0 => float 126.1
1 => float 255.47
2 => float 329.1
'May 2010' =>
array (size=3)
0 => int 109
1 => int 170
2 => float 716.7
//...Intead of array result what you expect
array (size=5)
0 =>
array (size=4)
0 => string 'Jan 2010' (length=8)
1 => float 65.75
2 => float 211.05
3 => float 582.7
1 =>
array (size=4)
0 => string 'Feb 2010' (length=8)
1 => float 136.3
2 => float 215.32
3 => float 413.9
2 =>
array (size=4)
0 => string 'Mar 2010' (length=8)
1 => float 156.35
2 => float 210.54
3 => float 585.15
3 =>
array (size=4)
0 => string 'Apr 2010' (length=8)
1 => float 126.1
2 => float 255.47
3 => float 329.1
4 =>
array (size=4)
0 => string 'May 2010' (length=8)
1 => int 109
2 => int 170
3 => float 716.7
It's quite horrible,but it works...
$resarray=Array(
0 => Array(0 => 'Jan 2010',1 => 65.75),
1=> Array(0 => 'Jan 2010',1 => 211.05),
2 => Array(0 => 'Jan 2010',1 => 582.7),
3 => Array(0 => 'Feb 2010',1 => 136.3),
4 => Array(0 => 'Feb 2010',1 => 215.32),
5 => Array(0 => 'Feb 2010',1 => 413.9),
6 => Array(0 => 'Feb 2010',1 => 156.35),
7 => Array(0 => 'Feb 2010',1 => 210.54),
8 => Array(0 => 'Mar 2010',1 => 585.15),
9 => Array(0 => 'Apr 2010',1 => 126.1),
10 => Array(0 => 'Apr 2010',1 => 255.47),
11 => Array(0 => 'Apr 2010',1 => 329.1),
12 => Array(0 => 'May 2010',1 => 109),
13 => Array(0 => 'May 2010',1 => 170),
14 => Array(0 => 'May 2010',1 => 716.7)
);
$CatArray=array();
$FinArray=array();
$count=count($resarray);
for($i=0;$i<$count;$i++){
$index=array_search($resarray[$i][0],$CatArray);
if(is_numeric($index) && $index>=0)
$FinArray[$index][]=$resarray[$i][1];
else{
$CatArray[]=$resarray[$i][0];
$FinArray[]=array($resarray[$i][0],$resarray[$i][1]);
}
}
unset($CatArray);
unset($resarray);
$count=count($FinArray);
for($i=0;$i<$count;$i++){
$resarray[$i]=implode(',',$FinArray[$i]);
}
Output:
Array
(
[0] => Jan 2010,65.75,211.05,582.7
[1] => Feb 2010,136.3,215.32,413.9,156.35,210.54
[2] => Mar 2010,585.15
[3] => Apr 2010,126.1,255.47,329.1
[4] => May 2010,109,170,716.7
)

Categories