0

I have this array

Array
(
    [0] => Array
        (
            [id] => 1
            [job_id] => 100
        )
    [1] => Array
        (
            [id] => 2
            [job_id] => 100
        )
    [2] => Array
        (
            [id] => 3
            [job_id] => 101
        )
)

Now here how to check if "job_id" is different. eg all have same job_id or not and how many different job_id array have.

Thanks

2

2 Answers 2

1

Try this using array function:

$data = Array
(
[0] => Array
    (
        [id] => 1
        [job_id] => 100
    )
[1] => Array
    (
        [id] => 2
        [job_id] => 100
    )
[2] => Array
    (
        [id] => 3
        [job_id] => 101
    )
)
$totalDifferentJobId = count(array_unique(array_column($data, 'job_id')));

It will give you total unique job

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

1 Comment

@Nawin $data is your array. Don't copy my $data variable. just assign your array to $data variable. It will works perfectly.
1

Well, if it is not a too large array, you can do the following:

<?php

$arr = [
    ['id' => 1, 'job_id' => 100],
    ['id' => 2, 'job_id' => 100],
    ['id' => 3, 'job_id' => 101]
];

$jobids = array_unique(array_column($arr, 'job_id'));

var_dump($jobids);

This looks like a db-result, in wich case it would be better to use a group-by statement?

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.