0

I have collection of array and i want delete array of array where "answer_id" key is not exits. my array look like this.

Array
(
    [0] => Array
        (
            [question_no] => 1
            [subject_id] => 1
            [question_id] => 255
            [currect_ans_id] => 2657
            [time_taken] => 110
            [is_visited] => 1
            [is_saved] => 0
            [answer_id] => 2659
        )

    [1] => Array
        (
            [question_no] => 2
            [subject_id] => 1
            [question_id] => 256
            [currect_ans_id] => 2662
            [time_taken] => 0
            [is_visited] => 1
            [is_saved] => 0
        )


)

and want array like this(where answer_id key exits).

  Array
    (
        [0] => Array
            (
                [question_no] => 1
                [subject_id] => 1
                [question_id] => 255
                [currect_ans_id] => 2657
                [time_taken] => 110
                [is_visited] => 1
                [is_saved] => 0
                [answer_id] => 2659
            ) 
)
5
  • 3
    A simple for loop and unset? Commented May 1, 2020 at 12:53
  • i wants to delete without using loop Commented May 1, 2020 at 12:55
  • 3
    @VeerenderMishra No way of doing it without using a loop. All inbuilt functions also use a loop. Commented May 1, 2020 at 12:57
  • @vivek_23 problem has been solve.thank you so much for your quick response. Commented May 1, 2020 at 13:02
  • 1
    You would normally be expected to make some effort in solving the problem rather than just asking others to solve it for you. Commented May 1, 2020 at 13:18

1 Answer 1

4

You can use array_filter to remove entries which don't have an answer_id:

$output = array_filter($input, function ($a) { return isset($a['answer_id']); });

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.