A
A
alihang2018-10-11 20:03:19
PHP
alihang, 2018-10-11 20:03:19

How to check the correspondence between categories?

Hello, such a question. There are products on one site, they are located in categories / subcategories, I copy these products to my site using a parser, but I ran into such a problem. On a third-party site, categories of this type are "Children's World » Children's Shoes", "Children's World » Toys", i.e. category and sub-category. On my site, the categories are slightly different from these, and I have collected an array like 'category from a third-party site' => 'my category'. But the catch is that I need to somehow compare these categories in order to correctly enter products into the database. I'm trying to do so

foreach ($result as $s) {

    foreach ($categories as $cat) {
        if (key($categories) == $category){
            echo current($cat);
        }
    }
}

but the condition is not met because the array key does not match the contents of $category. So, is it possible to somehow get out of this situation without rewriting the dictionary array.
Own code

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
spaceatmoon, 2018-10-11
@spaceatmoon

Why don't you just do it?

if (isset($categories[$category]) {
 echo $category;
}

And it’s still not clear why you are making a request to the database and scrolling your condition inside the result, do you definitely have something coming from the database?
spoiler
$sql = mysqli_query($db_connect, "SELECT `fk_i_category_id`, `s_name` FROM `oc_t_category_description` WHERE 1");
        $result = mysqli_fetch_all($sql);
 
        echo $category . "<br>";
 
        foreach ($result as $s) {
 
            foreach ($categories as $cat) {
 
                if (key($categories) == $category){
 
                    echo current($cat);
 
                }
 
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question