-2

I have a $data of array like below inside the [23:45] has arrays with userid and login time:

$data = array( 
   [93] => array( 
        [23:34] => array(
                [0] => array(
                    [userid]=> 93,
                    [login] =>23:34:03
                ),
                [1] => array(
                    [userid]=> 93,
                    [login] =>23:34:07
                ),
                [2] => array(
                    [userid]=> 93,
                    [login] =>23:34:02
                )
           )
   )

)

The output I want is:

$data = array( 
   [93] => array( 
        [23:45] => array(
                [0] => array(
                    [userid]=> 93,
                    [login] =>23:34:02
                )
           )
       )
)

The only left is the login with the earliest time and the other array should be remove.Please help

Zero
  • 375
  • 1
  • 5
  • 9
  • Do you have any code? Have you tried finding the lowest time, then replacing the array? – Aurel Bílý Jun 09 '16 at 12:01
  • Are there everytime all items with the sane ID ? – splash58 Jun 09 '16 at 12:01
  • yeah they are all same id. – Zero Jun 09 '16 at 12:02
  • Voted to close as too broad already, but this also seem to be a duplicate of your previous question, http://stackoverflow.com/questions/37718083/remove-partial-similar-values-in-array-php – CBroe Jun 09 '16 at 12:05
  • And if what you have shown in your _third_ question about what seems to be basically all the same topic - http://stackoverflow.com/questions/37716228/make-the-userid-element-in-array-as-parent-key-php - is your initial data that you start with, then you should rather perform that selection in that step already. – CBroe Jun 09 '16 at 12:07

2 Answers2

1

Find the lowest time:

$earliest = 0;
for ($i = 1; $i < count($data[93]["23:45"]; $i++){
    if (strcmp($data[93]["23:45"][$i]["login"], $data[93]["23:45"][$earliest]["login"]) < 0){
        $earliest = $i;
    }
}

Then replace the array with a new one, containing only the earliest login:

$data[93]["23:45"] = array(
    $data[93]["23:45"][$earliest]
);
Aurel Bílý
  • 7,068
  • 1
  • 21
  • 34
1

First of all sort the array and then unset all the items except the first item at 0 index in array..

<?php 
$data = array( 
   '93' => array( 
        '23:34' => array(
                0 => array(
                    'userid'=> 93,
                    'login' =>'23:34:03'
                ),
                1 => array(
                    'userid'=> 93,
                    'login' =>'23:34:07'
                ),
                2 => array(
                    'userid'=> 93,
                    'login' =>'23:34:02'
                )
           )
        )
    );

usort($data['93']['23:34'], build_sorter('login'));///sort array    

function build_sorter($key) ///custom sort function just pass the key
{
    return function ($a, $b) use ($key) {
        return strnatcmp($a[$key], $b[$key]);
    };
}   


foreach($data['93']['23:34'] as $key => $val)
{
    if($key != 0)
    {
        unset($data['93']['23:34'][$key]); ///remove all the items except first item..
    }
}
print_r($data);  
?>

This will give you :

Array
(
    [93] => Array
        (
            [23:34] => Array
                (
                    [0] => Array
                        (
                            [userid] => 93
                            [login] => 23:34:02
                        )

                )

        )

)

LIVE EXAMPLE : CLICK HERE

Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20
  • Seems extremely complicated / slow for a rather simple task. Why `sort` if you only need one of the items? Why `unset` the items one by one? You could have just `array_splice`'d the sorted array. – Aurel Bílý Jun 10 '16 at 10:28