如何验证单词是否在数组中并获取数组位置值?

时间:2019-06-17 02:56:12

标签: php arrays

我正在尝试通过检查是否在category_id中找到category_name来获得产品的product_name

采用此数组

$women_apparel = array('Tank Top', 'T-Shirt');

现在有这两个product_name:

$product_name1 = 'Next Level 6733 Ladies Triblend Racerback Tank Top';
$product_name2 = 'Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt';

我正在尝试:

if (in_array($product_name1, $women_apparel)) {
 echo 'this product belongs to category X';
}

所需的输出:

Product 1 belongs to category 0
Product 2 belongs to category 1

如何动态完成此操作(无需硬编码)?

4 个答案:

答案 0 :(得分:1)

您可以使用相似文本来对此类中的相似字符串进行计数。

<?php 



  $women_apparel = array('Tank Top', 'T-Shirt');


$product_name1 = 'Next Level 6733 Ladies Triblend Racerback Tank Top';
$product_name2 = 'Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt';

    foreach ($women_apparel as $key=>$value) {
        if (similar_text($product_name1, $value) >= strlen($value)) {
            echo "Product 1 belongs to category $key ";
        }
        else if (similar_text($product_name2, $value) >= strlen($value)) {
            echo "Product 2 belongs to category $key ";
        }
    }

答案 1 :(得分:1)

您可以使用过滤器。使用回调函数匹配数组的值:

   <?php


$women_apparel = array('Tank Top', 'T-Shirt', 'jeans');
//Add the product value in array.
$product ['product 1'] = 'Next Level 6733 Ladies Triblend Racerback Tank Top';
$product ['product 2'] = 'Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt';
$product ['product 3'] = 'product of jeans';

    for($i=0; $i<count($women_apparel);$i++){
        $value = $women_apparel[$i];

        $match = array_filter($product, function($var) use ($value) { return preg_match("/\b$value\b/i", $var); });

        //If the women_apparel value in product gets the key of product ($match)[0]) and display the required output.
        echo array_keys($match)[0].' belongs to category '.$i.'<br/>';
}

?>

答案 2 :(得分:1)

我想到了一种动态方法,该方法创建接收两个数组的函数searchProductCategory,第一个应包含产品名称,第二个应包含类别。

<?php
$categories = ['Tank Top', 'T-Shirt'];

$products = [
    'Next Level 6733 Ladies Triblend Racerback Tank Top',
    'Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt'
];

searchProductCategory($products, $categories);

function searchProductCategory(array $products, array $categories) : void
{
    foreach ($products as $product_index => $product) {
        $product_index++;

        foreach ($categories as $category_index => $category) {
            if (similar_text($product, $category) >= strlen($category)) {
                echo "Product {$product_index} belongs to category $category_index <br />";
            }
        }

    }
}

答案 3 :(得分:1)

您可以通过将自定义函数与array_walk一起使用

$women_apparel = array('Tank Top', 'T-Shirt');
$product_name1 = 'Next Level 6733 Ladies Triblend Racerback Tank Top';
$product_name2 = 'Los Angeles Apparel 2332 Fine Jersey Short Sleeve Cropped T-Shirt';
searchForCategory($product_name1, $women_apparel);//call for product 1
searchForCategory($product_name2, $women_apparel);// call for product 2

function searchForCategory($pname, $catArr){
    $res = [];
    array_walk($catArr, function($v, $k) use($pname, &$res){
        if(strpos($pname, $v) !== false )
            echo $pname.' : belongs to category '.$k.'<br/>';
    });
}

DEMO