1

I have this query from first table:

     $this->db->table('categories')
        ->select('id, name, parent_id')
        ->orderBy('parent_id')
        ->get()
        ->getResultObject();

data output is:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => cat1
            [parent_id] => 0
        )

    [1] => stdClass Object
        (
            [id] => 2
            [name] => cat2
            [parent_id] => 0
        )

    [2] => stdClass Object
        (
            [id] => 5
            [name] => cat5
            [parent_id] => 0
        )

    [3] => stdClass Object
        (
            [id] => 6
            [name] => cat6
            [parent_id] => 0
        )

    [4] => stdClass Object
        (
            [id] => 3
            [name] => cat3
            [parent_id] => 2
        )

    [5] => stdClass Object
        (
            [id] => 4
            [name] => cat4
            [parent_id] => 2
        )

)

and query for second table:

$this->db->table('post_category')
    ->select('category_id')
    ->where('post_id', $id)
    ->get()->getResultObject();

output data is:

Array
(
    [0] => stdClass Object
        (
            [category_id] => 1
        )

    [1] => stdClass Object
        (
            [category_id] => 4
        )

)

Now I need to add selected in array and add data(category_id) from table two in table one if (category_id = id) like this:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => cat1
            [parent_id] => 0
            [selected] => 1
        )

    [1] => stdClass Object
        (
            [id] => 2
            [name] => cat2
            [parent_id] => 0
            [selected] => 
        )

    [2] => stdClass Object
        (
            [id] => 5
            [name] => cat5
            [parent_id] => 0
            [selected] => 
        )

    [3] => stdClass Object
        (
            [id] => 6
            [name] => cat6
            [parent_id] => 0
            [selected] => 
        )

    [4] => stdClass Object
        (
            [id] => 3
            [name] => cat3
            [parent_id] => 2
            [selected] => 
        )

    [5] => stdClass Object
        (
            [id] => 4
            [name] => cat4
            [parent_id] => 2
            [selected] => 1
        )

)

How do can i add data from table two in table one(if category_id from table two = id from table one) like my last output?!

2
  • mysqltutorial.org/mysql-join Commented Apr 10, 2020 at 0:10
  • @ikiK: please add answer with data for check and tick if sloved problem Commented Apr 10, 2020 at 8:54

1 Answer 1

1

If you just want to populate the ['selected'] item of a subarray in the first array ("categories") with "1" based on whether there's a "post_category" with that value you can do this:

foreach ($categories as &$category) {
    $filtered_post_categories = array_filter($post_categories, function($post_category) use ($category) {
        return $post_category->category_id == $category->id;
    });

    if (count($filtered_post_categories) > 0) {
        $category->selected = 1;
    }
    else {
        $category->selected = null;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The code will modify the first array you provided, so print it out however you like/need for your use. var_dump($categories); would spit out what you said you wanted in the OP.
Sure, not true work. in action for my data, your code add selected to first array with id = 1 but i need to add selected to categories array for id =1 and id = 4. can u update your answer with data for better choice, please.
Sorry. Fixed it. The break line actual broke the functionality.

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.