Reputation: 167
In PHP, I have array like this:
Array
(
[0] => 09:26-DOWN
[1] => 09:26-UP
[2] => 09:26-UP
[3] => 09:26-UP
[4] => 09:26-UP
[5] => 09:26-UP
[6] => 09:26-UP
[7] => 09:26-UP
[8] => 09:26-DOWN
[9] => 09:26-UP
[10] => 09:26-UP
[11] => 09:26-UP
[12] => 09:26-UP
[13] => 09:26-UP
[14] => 09:26-UP
[15] => 09:26-UP
[16] => 09:26-UP
[17] => 09:26-UP
)
and I want to compare it with below array, only with the last characters (UP and DOWN):
Array
(
[0] => 09:31-DOWN
[1] => 09:31-UP
[2] => 09:31-UP
[3] => 09:31-UP
[4] => 09:31-UP
[5] => 09:31-UP
[6] => 09:31-UP
[7] => 09:31-UP
[8] => 09:31-UP
[9] => 09:31-UP
[10] => 09:31-UP
[11] => 09:31-UP
[12] => 09:31-UP
[13] => 09:31-UP
[14] => 09:31-UP
[15] => 09:31-UP
[16] => 09:31-UP
[17] => 09:31-UP
)
So the purpose is to check if it up or down between the specific time.
Upvotes: 1
Views: 105
Reputation: 7447
Not exactly sure what you're trying to do, but here's a simple option. Assuming that the format of each element will always be the same, ie HH:II-[UP or DOWN]
, you could do this:
$result = array();
foreach ($arr1 as $k=>$v) {
$result[] = $v[6] . $arr2[$k][6];
}
Given your two arrays, $arr1
and $arr2
, this will yield a third array like:
Array
(
[0] => DD
[1] => UU
[2] => UU
[3] => UU
[4] => UU
....
)
Then you can use this array's values, UU
, DD
, UD
, DU
, to figure out what is in each position. For example key 3 will have UU
, so you know it is UP, UP....
Update Here's the one-liner version using array_map():
$result = array_map(function($a, $b){return $a[6] . $b[6];}, $arr1, $arr2);
Upvotes: 3
Reputation: 1341
Give the two arrays
<?php
$array1 = array('UP', 'DOWN', 'UP', 'DOWN');
$array2 = array('UP', 'DOWN', 'DOWN', 'UP');
foreach ($array2 as $key => $new_value)
{
$old_value = $array1[$key];
$new_array[$key] = ($old_value <= $new_value ? (($old_value < $new_value) ? 'UP' : 'NO CHANGE') : 'DOWN');
}
var_dump($new_array);
Will output something like this
array(4) {
[0]=>
string(9) "NO CHANGE"
[1]=>
string(9) "NO CHANGE"
[2]=>
string(4) "DOWN"
[3]=>
string(2) "UP"
}
Upvotes: 1
Reputation: 12168
Try to use strstr()
+ trim()
to cut apart state / status.
$changed = []; // to store changed statuses
foreach($old as $i => $value){
$from = trim(strstr($value, '-'), '-'); // getting status (UP / DOWN) from first array
$to = trim(strstr($new[$i], '-'), '-'); // getting status (UP / DOWN) from second array
if($from != $to){ // if statuses are different
$changed[$i] = [
'from' => $from,
'to' => $to
]; // add to changed array
}
}
Full example:
<?php
header('Content-Type: text/plain; charset=utf-8');
$old = [
'09:26-DOWN',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-DOWN',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-UP',
'09:26-UP'
];
$new = [
'09:31-DOWN',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP',
'09:31-UP'
];
$changed = [];
foreach($old as $i => $value){
$from = trim(strstr($value, '-'), '-');
$to = trim(strstr($new[$i], '-'), '-');
if($from != $to){
$changed[$i] = [
'from' => $from,
'to' => $to
];
}
}
print_r($changed);
?>
Output:
Array
(
[8] => Array
(
[from] => DOWN
[to] => UP
)
)
Upvotes: 1