1

I want to check if the data is same then don't print the data but push that data to the first data.

for example I have data array like this:

0 => array:4 [▼
    "StartTime" => "00:00:00"
    "type" => 1
    "DateAppointment" => "2019-02-24"
    "SDay" => "Sun"
  ]
1 => array:4 [▼
    "StartTime" => "00:00:00"
    "type" => 1
    "DateAppointment" => "2019-02-25"
    "SDay" => "Mon"
  ]
2 => array:4 [▼
    "StartTime" => "00:00:00"
    "type" => 1
    "DateAppointment" => "2019-02-26"
    "SDay" => "Tue"
  ]
3 => array:4 [▼
    "StartTime" => "00:10:00"
    "type" => 1
    "DateAppointment" => "2019-02-24"
    "SDay" => "Sun"
  ]

data array 0, 1, 2 has the same StartTime, so I want my data to be like this

0 => array:10 [▼
    "StartTime" => "00:00:00"
    "type" => 1
    "DateAppointment" => "2019-02-24"
    "SDay" => "Sun"
    "type1" => 1
    "DateAppointment1" => "2019-02-25"
    "SDay1" => "Mon"
    "type2" => 1
    "DateAppointment2" => "2019-02-26"
    "SDay2" => "Tue"
  ]
1 => array:4 [▼
    "StartTime" => "00:10:00"
    "type" => 1
    "DateAppointment" => "2019-02-24"
    "SDay" => "Sun"
  ]

is that possible?

my script is like this

$interval = 10;
$hourMinInterval = 60 - $interval;
for ($i = 0; $i <= 23; $i++){
    for ($j = 0; $j <= $hourMinInterval; $j+=$interval){

        $h = sprintf('%02d',$i);
        $m = sprintf('%02d',$j);
        $clock[] = $h.':'.$m.':00';

        if ($lastdate != 'lastsunday') {
            $timestampa = $lastdate;
        } else {
            $timestampa = strtotime('last Sunday');
        }

        for($ble=0;$ble<7;$ble++){
            $timeC = sprintf('%02d',$i).':'.sprintf('%02d',$j).':00';
            $tgla = strftime('%Y_%m_%d', $timestampa);
            $dateS = str_replace('_','-',$tgla);
            $sday = strftime('%a', $timestampa);
            $timeCs = array('StartTime'=>$timeC, 'type'=>1, 'DateAppointment'=>$dateS, 'SDay'=>$sday);
            $timestampa = strtotime('+1 day', $timestampa);

            if ($timeC == $timeCs['StartTime']) {
                $statusssss[] = $timeCs;
            }else{
                $statusssss[] = '';
            }
        }
    }
}

If you have another way or any example with jsfiddle will be appreciated.

2
  • 2
    IMO it's bad idea to add some numbers to keys. Maybe better to convert them to arrays? Commented Mar 1, 2019 at 7:31
  • im not sure, i cant think another way, if u have any suggestion with example will be appreciated. Commented Mar 1, 2019 at 7:33

2 Answers 2

2

Try this code

$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);

Result:

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"
    }
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Try this isn't considered the best of explanations of answers - please add something more meaningful!
"more meaningful".. about what ? How to unset array or how to add new elements to array ?
sorry but this is not like what i want, i want to group it, like what i say in the question.
This is exactly what you asked, grouping array with StartTime, if you want to access it inside your foreach loop, just use foreach($arrayItems as $key => $value)
yup, but i need the index and the starttime, cause i want to print it in my page. see my screenshot i.imgur.com/6ESDxGM.jpg
|
0

This will give u the desired result!

$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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.