0

I want to extract data from the following array where key 3 = 2017-10-27:

Array
(
    [0] => Array
        (
            [0] => 65604
            [1] => 1
            [2] => 0
            [3] => 2017-09-04 18:22:34
        )

    [1] => Array
        (
            [0] => 69
            [1] => 29
            [2] => 0
            [3] => 2017-10-27 07:27:59
        )

    [2] => Array
        (
            [0] => 70
            [1] => 1
            [2] => 0
            [3] => 2017-09-10-27:44:13
        )

    [3] => Array
        (
            [0] => 71
            [1] => 5
            [2] => 0
            [3] => 2017-09-05 07:52:54
        )

    [4] => Array
        (
            [0] => 72
            [1] => 28
            [2] => 0
            [3] => 2017-09-05 07:54:38
        )

    [5] => Array
        (
            [0] => 73
            [1] => 18
            [2] => 0
            [3] => 2017-09-05 07:54:53
        )

AS the array is too big, and contains around 20000 data. I want to pull out just the data I need. I used foreach loop but it took me too much time as I also want to update in the database

foreach($array as $k=>$val){
        $all = new DateTime($val[3]);
        $hun = $all->format('H:i:s');
        $date= $all->format('Y-m-d');
        $id= $val[1];
        if($date=='2017-10-27'){
             //$dbh->query("UPDATE table SET date='$date' WHERE id='$id' AND date IS NULL");
        }
}

Database not update, the print_r() took around 5 minutes. Any better way to filter out just the data I need.

6
  • As the date is the filter you want to use for searching in the upper array, I would recommend to make the structure of the array differently, where date is for example the key to the "main array" and the other data is followed in the "lower array". Commented Nov 1, 2017 at 6:43
  • The problem is the array comes from a device. Please can you post your proposed structure. Commented Nov 1, 2017 at 6:45
  • While you fetching these result, don't select all data just all you need is to fetch only date value from database and your stuffs resolved... Commented Nov 1, 2017 at 6:52
  • What is the question? Commented Nov 1, 2017 at 6:57
  • I am not pulling the data from database, Its from bio-metric device. It gives me the array. Commented Nov 1, 2017 at 7:00

2 Answers 2

1

Use this function to reindex your array:

function reindex($arr, $commonIndex){
    $res = array(); 
    foreach( $arr as $one=>$two)
        $res[$two[$commonIndex]] = $two;
    return $res;
}

Usage:

$res = reindex($arr, 3);
echo $res['2017-10-27'];
Sign up to request clarification or add additional context in comments.

Comments

0

As an answer to your comment:

Example of a structure:

array(
    "2017-10-10" => array(  /* this date is just for filtering and structure purpose */
                    69,
                    29,
                    0,
                    "2017-10-27 07:27:59" /* optional if you still want to store the date + time in the element */
                ),
    "2017-10-10" ... etc.
)

1 Comment

I could not change the structure that comes from the device. The device utility does not support.

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.