3

I want to create an overview of the first dates of every event. So the event-title must be unique. My idea was to create a helper-function where I loop over the result of my query and check the title of every item. To make sure every title passes only once, I want to push the title into an array ($checklist). If it does not exist, I add that item to the result-array. If it does, just continue to the next item.

I always get the error:

in_array() expects parameter 2 to be array, integer given

This is my code:

function showFirstEvenst($collection) {
    $checklist = array();
    $result = array();

    foreach ($collection as $item) {
        $title = strtolower($item['events']['title']);

        if (!in_array($title, $checklist)) {
            $checklist = array_push($checklist, $title);
            $result = array_push($result, $item);
        }
    }

    return $result;
}

I already tried to cast $checklist and $result as array in the foreach loop but without result.

What do I need to change?

6
  • php.net/manual/en/function.array-push.php returns an integer, and you overwrite your array in the loop Commented Jun 12, 2017 at 18:21
  • 1
    And why use array_push()??? Just $checklist[] = $title; Commented Jun 12, 2017 at 18:23
  • @AbraCadaver is that more performant? Commented Jun 12, 2017 at 18:25
  • @bflydesign it won't make a huge difference, but yes, it should be. Commented Jun 12, 2017 at 18:27
  • 1
    Found this related question in regards to performance of $array[] = $value vs array_push($value, $array): stackoverflow.com/questions/559844/… Commented Jun 12, 2017 at 18:35

3 Answers 3

5

Adding to @Lawrence Cherone and @Ravinder Reddy's answers, instead of using array_push, you could use native array syntax to push to the array:

if (!in_array($title, $checklist)) {
    $checklist[] = $title;
    $result[] = $item;
}
Sign up to request clarification or add additional context in comments.

1 Comment

In fact, the array_push documentation recommends doing it this way when you're just appending one item.
3

array_push function will return the count of array after an element is added to array. so dont assing the output of the function to array.

Replace

  if (!in_array($title, $checklist)) {
                $checklist = array_push($checklist, $title);
                $result = array_push($result, $item);
            }

with

 if (!in_array($title, $checklist)) {
               array_push($checklist, $title);
               array_push($result, $item);
            }

1 Comment

that is indeed the problem...wasn't looking for that.
2

Its happening because within your loop your assigning $checklist with the value of array_push() which will be the new number of elements in the array.

http://php.net/manual/en/function.array-push.php

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.