Когда я спотыкаюсь на эту проблему, я просто передаю объекты во внутренний класс через конструктор. Если мне нужно передать примитивы или неизменяемые объекты (как в этом случае), необходим класс-оболочка.
Изменить: На самом деле я вообще не использую анонимный класс, но соответствующий подкласс:
public class PriceData {
private double lastPrice = 0;
private double price = 0;
public void setlastPrice(double lastPrice) {
this.lastPrice = lastPrice;
}
public double getLastPrice() {
return lastPrice;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
}
public class PriceTimerTask extends TimerTask {
private PriceData priceData;
private Price priceObject;
public PriceTimerTask(PriceData priceData, Price priceObject) {
this.priceData = priceData;
this.priceObject = priceObject;
}
public void run() {
priceData.setPrice(priceObject.getNextPrice(lastPrice));
System.out.println();
priceData.setLastPrice(priceData.getPrice());
}
}
public static void main(String args[]) {
int period = 2000;
int delay = 2000;
PriceData priceData = new PriceData();
Price priceObject = new Price();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new PriceTimerTask(priceData, priceObject), delay, period);
}
Попробуйте этот код
$arrayItems = [
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-24",
"SDay" => "Sun"
],
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-25",
"SDay" => "Mon",
],
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-26",
"SDay" => "Tue"
],
[
"StartTime" => "00:10:00",
"type" => 1,
"DateAppointment" => "2019-02-24",
"SDay" => "Sun"
]
];
$mergedArray = [];
foreach($arrayItems as $arrayItem) {
$startTime = $arrayItem['StartTime'];
unset($arrayItem['StartTime']);
$mergedArray[$startTime][] = $arrayItem;
}
echo '<pre>';
var_dump($mergedArray);
Результат:
array(2) {
["00:00:00"]=>
array(3) {
[0]=>
array(3) {
["type"]=>
int(1)
["DateAppointment"]=>
string(10) "2019-02-24"
["SDay"]=>
string(3) "Sun"
}
[1]=>
array(3) {
["type"]=>
int(1)
["DateAppointment"]=>
string(10) "2019-02-25"
["SDay"]=>
string(3) "Mon"
}
[2]=>
array(3) {
["type"]=>
int(1)
["DateAppointment"]=>
string(10) "2019-02-26"
["SDay"]=>
string(3) "Tue"
}
}
["00:10:00"]=>
array(1) {
[0]=>
array(3) {
["type"]=>
int(1)
["DateAppointment"]=>
string(10) "2019-02-24"
["SDay"]=>
string(3) "Sun"
}
}
}
Это даст вам желаемый результат!
$arrayItems = [
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-24",
"SDay" => "Sun"
],
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-25",
"SDay" => "Mon",
],
[
"StartTime" => "00:00:00",
"type" => 1,
"DateAppointment" => "2019-02-26",
"SDay" => "Tue"
],
[
"StartTime" => "00:10:00",
"type" => 1,
"DateAppointment" => "2019-02-24",
"SDay" => "Sun"
]
];
$mergedArray = [];
$i = 0;
foreach($arrayItems as $arrayItem) {
$keys = array_keys($arrayItem);
foreach($keys as $key){
if(array_key_exists($key, $mergedArray[$arrayItem["StartTime"]])){
$newkey = $key.$i;
$mergedArray[$arrayItem["StartTime"]][$newkey] = $arrayItem[$key];
}
else{
$mergedArray[$arrayItem["StartTime"]][$key] = $arrayItem[$key];
}
}
$i++;
}
echo '<pre>';
var_dump($mergedArray);