1

Sorry if this question is worded incorrectly or doesn't make any sense. What I am trying to do is write an if statement that checks if:

array(6) {
    [5]=>
    string(17) "Quality Assurance"
    [6]=>
    string(7) "Analyst"
    [7]=>
    string(19) "Developer/Front end"
    [8]=>
    string(18) "Developer/Back end"
    [9]=>
    string(4) "Test"
    [10]=>
    string(2) "hi"
}

Any of those keys, in this case, 5, 6, 7, 8, 9, 10 is in:

array(4) {
    [0]=>
    object(stdClass)#195 (2) {
       ["labour_type_id"]=>
       int(5)
       ["required_labour_type_hours"]=>
       int(40)
    }
    [1]=>
    object(stdClass)#193 (2) {
       ["labour_type_id"]=>
       int(6)
       ["required_labour_type_hours"]=>
       int(80)
    }
}

This second arrays "labour_type_id".

In this example, 5 and 6 would match.

I am trying to use the in_array() function but I am not sure how to access the labour_type_id of the second array.

My best attempt at the moment:

        @foreach($labourTypes as $id => $name)
            @if(in_array($id, $reqLabourTypes->labour_type_id))

Where labourTypes is the first array, and reqLabourTypes is the 2nd array.

Thanks.

0

1 Answer 1

1

I've cleaned up this little search for you to try and find it as you require:

$new = array_filter(array_map(function(&$item) use($requiredLabour, $labourTypes){

    $key = array_search($item, $labourTypes);
    foreach($requiredLabour as $elem){
        if($elem['labour_type_id'] == $key) {

            return array(
                $key => $item,
                'options' => $elem
            );

        }
    }

}, $labourTypes));

Everything will be accessible in $new if found. It returns:

Array
(
    [5] => Array
        (
            [5] => Quality Assurance
            [options] => Array
                (
                    [labour_type_id] => 5
                    [required_labour_hours] => 40
                )

        )

    [6] => Array
        (
            [6] => Analyst
            [options] => Array
                (
                    [labour_type_id] => 6
                    [required_labour_hours] => 40
                )

        )

)

The above is just the output, you can change it to whatever you need it to be by simply editing the return array(..... inside to whatever you require.

Example/Demo

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

5 Comments

Would it be possible for the new array to also take the corresponding required_labour_hours from the previous example?
Awesome thanks! How could I access the required_labour_hours from the new array?
Simply loop through the array with foreach, and access it, or direct call if you know the hours haha $new[5]['options']['required_labour_hours']
THANK YOU! I managed to implement it to my generated checkbox/input field finally. I've been struggling with this all day thanks so much for the help.
Beautiful! Congrats and glad I could help!

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.