-1

In a file, I am wondering how to check if 0th element of each array is equal to one another.

For example I have CSV, while looping through it I need to compare that all the date-time for each data is measured in the same hour.

So PHP

while (($row = fgetcsv($file, 1000, ",")) !== FALSE ){
    if($headerLine){
        $headerLine = false;
    } else {
        print "<pre>";
        print_r($row);
        print "</pre>";
        echo "<hr>";
        echo $row[0];
    }
}

Output would be:

Array
(
    [0] => 2017-06-08 05:00:00
    [1] => 2234
    [2] => name1
    [3] => 0
    [4] => 32
    [5] => 242
    [6] => 324

2017-06-08 05:00:00

Array
(
    [0] => 2017-06-08 05:00:00
    [1] => 322
    [2] => name2
    [3] => 1
    [4] => 34
    [5] => 234
    [6] => 432
)

2017-06-08 05:00:00

...

So I need to check whether each 0 row is equal to one another.

mycupoftea
  • 361
  • 1
  • 3
  • 8
  • https://stackoverflow.com/questions/5678959/php-check-if-two-arrays-are-equal – hungrykoala Jun 16 '17 at 11:40
  • hm, this is between two arrays, and I only need for one element, not whole arrays. I have 3 dots below so It means that I have lots of arrays (its csv file). – mycupoftea Jun 16 '17 at 11:43
  • so you just need to compare the first index of the array? – hungrykoala Jun 16 '17 at 11:44
  • why are you not checking it while looping through your CSV? Keep the previous date_time in a variable and update it when it changes. – moni_dragu Jun 16 '17 at 11:46
  • 0th, its stated in my question. – mycupoftea Jun 16 '17 at 11:48
  • @moni_dragu smth like $row_date_time = $row[0]; if ($row_date_time == $row[0]){ echo "data is equal"; } else { echo "data is not equal"; } – mycupoftea Jun 16 '17 at 11:52
  • I was hoping that there is smth like, give me differences of that specific key in array...http://php.net/manual/en/function.array-diff.php but kind of stuck how to use it... – mycupoftea Jun 16 '17 at 11:54
  • Yes. You just need to also keep $row_date_time updated: $row_date_time = $row[0]; if ($row_date_time == $row[0]){ echo "data is equal"; } else { echo "data is not equal"; $row_date_time = $row[0]; } – moni_dragu Jun 16 '17 at 11:54
  • Hm..smth is not right. Its same like with "in_array" mentioned below, data will search within itself, so I will always get that "data is equal". Don't I need to state specific value to compare it like that? I was trying to avoid that and just compare hour within file – mycupoftea Jun 16 '17 at 12:28

2 Answers2

0

Of the top of my head you could use array_shift with in_array:

$first_element = array_shift($row);
$result = in_array($first_element, $row);
array_unshift($row, $first_element);

You will get the $result with true/false for your test

Marko Marjanovic
  • 436
  • 1
  • 4
  • 13
  • Sorry, my bad, its all about 0th element. But isnt it that with this line "in_array" it will always return me something that it is in the data itself. – mycupoftea Jun 16 '17 at 12:17
  • That is why you use array_shift it removes the first element, and after that with array_unshift returns it back... – Marko Marjanovic Jun 16 '17 at 12:41
  • $row is affected that way and I get 0 results: echo $first_element; print_r($row); output: 2017-06-08 05:00:00 Array ( [0] => 2234 [1] => name1 => 0 [3] => 32[4] => 242 [5] => 324) – mycupoftea Jun 16 '17 at 12:55
0

You can use array_intersect function. Here is a sample usage:

$row1 = ['2017-06-08 05:00:00', 2234, 'name1', 0, 32, 242, 324];
$row2 = ['2017-06-08 05:00:00', 322, 'name2', 1, 34, 234, 432];
$intersect = array_intersect($row1, $row2);
$firstElementEqual = array_key_exists(0, $intersect); // true or false
var_dump(firstElementEqual);

Result:

true

If you want to run it for all rows, do it like this:

$rows = [$row1, $row2]; // put all rows in this array
$intersect = call_user_func_array('array_intersect', $rows));
$firstElementEqualInAllRows = array_key_exists(0, $intersect); // true or false

However if you need to know which row is different and for example skip then run simple for loop (here I compare all to the first row)

$rows = [$row1, $row2];
for ($i = 1; $i < count($rows); $i++) {
    $intersect = array_intersect($row[0], $row[1]);
    $firstElementEqual = array_key_exists(0, $intersect); // true or false
}
  • Having problem with how to state all rows in "array_intersect", since currently I am using just "$row" for all arrays. How to deal with that? – mycupoftea Jun 16 '17 at 12:19