如何将$ index传递给数组值?

时间:2018-03-13 08:15:23

标签: php angularjs

$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
print_r($suppliername);
print_r($vals);

foreach($Product as $index => $value)
{   
    if($vals[$index]>1)
    {
        echo $suppliername[$index]."+++"."Multiple Entries";
        // Here i have to get S0001
    }
    else
    {
        echo $suppliername[$index]."+++"."Single Entry";
        // Here i have to get S0002
    }

}

如何将$index传递给数组值?我试图检查索引的值是否大于或不在条件。怎么弄这个?

2 个答案:

答案 0 :(得分:1)

现在使用您的代码,由于您的print_r($val)结果为:

,因此无法运行
Array
(
    [S0001] => 2
    [S0002] => 1
)

$vals的密钥是$suppliername数组的结果,所以请尝试这样:

$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
print_r($suppliername);
print_r($vals);

foreach($Product as $index => $value)
{
    if($vals[$suppliername[$index]]>1)
    {
        echo $suppliername[$index]."+++"."Multiple Entries";
    }
    else
    {
        echo $suppliername[$index]."+++"."Single Entry";
    }
}

结果:

S0001+++Multiple Entries
S0002+++Single Entry
S0001+++Multiple Entries

这是你在找什么?

答案 1 :(得分:-1)

我不确定我是否理解你想要的东西,但在这里它会做你想要的结果:

<?php

$Product = array("aaa","bbb","ccc");
$suppliername = array("S0001","S0002","S0001");
$vals = array_count_values($suppliername);
echo "<pre>";
var_dump($suppliername);
echo "</pre>";
echo "<pre>";
var_dump($vals);
echo "</pre>";

foreach($Product as $index => $value)
{
    if($index>1)
    {
        echo "<br />" . $suppliername[$index]."+++"."Multiple Entries";
        // Here i have to get S0001
    }
    else
    {
        echo "<br />" . $suppliername[$index]."+++"."Single Entry";
        // Here i have to get S0002
    }

}