0
    Array
(
    [updateCategories] => Array
        (
            [products] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 3
                            [position] => 2
                            [product_id] => 8
                        )

                    [1] => Array
                        (
                            [cat_id] => 4
                            [position] => 11
                            [product_id] => 8
                        )

                    [2] => Array
                        (
                            [cat_id] => 3
                            [position] => 4
                            [product_id] => 39
                        )

                    [3] => Array
                        (
                            [cat_id] => 4
                            [position] => 9
                            [product_id] => 8
                        )

                    [4] => Array
                        (
                            [cat_id] => 3
                            [position] => 6
                            [product_id] => 41
                        )

                    [5] => Array
                        (
                            [cat_id] => 11
                            [position] => 7
                            [product_id] => 8
                        )

上面的数组是我的输出数组,但我需要获取所有cat_id. product_id=8我怎样才能做到这一点?

4

7 回答 7

1

用这个

 foreach($array['updateCategories']['products'] as $product) {
      if(isset($product['product_id']) && $product['product_id']==8) {
        //do anything you want i am echoing
         echo $product['cat_id'];
    }
    }
于 2013-07-17T11:56:19.940 回答
1

您可以使用array_filter

function filterProducts($product) {
    return ($product['product_id'] == 8);
}

$myProducts = array_filter(
    $myArray['updateCategories']['products'], 
    'filterProducts'
);

$myArray您的帖子中显示的数组在哪里。

于 2013-07-17T11:56:40.427 回答
1

可以通过做这样的事情来处理这个

$matching_products = array();
foreach ($products as $key => $value) {
    if($value['product_id'] == 8) {
        $matching_products[] = $value['cat_id'];
    }
}

这将为您留下一系列产品 ID 为 8 的猫 ID

于 2013-07-17T11:53:45.420 回答
1
$newarr = array();
foreach( $arr['updateCategories']['products'] as $myarr)
{
  if($myarr['product_id'] == 8)
  $newarr[] =  $myarr['cat_id'];
}
于 2013-07-17T11:55:14.170 回答
1

最简单的解决方案是

$result = array();
foreach($yourArray['updateCategories']['products'] as $product)
  if($product['product_id] == 8)
    $product[] = $product['cat_id'];

$yourArray您发布的转储数组在哪里。

于 2013-07-17T11:55:21.030 回答
1

尝试这样的事情:

$arr = array();
foreach ($products as $key => $value) 
{
    if($value['product_id'] == 8) 
    {
        $arr[] = $key;

    }
}
print_r($arr); // <-- this should output the array of products with key as 8
于 2013-07-17T11:55:54.900 回答
1

这应该能够从给定的 product_id 中检索所有 cat_id。此函数产生一个对象,可以迭代该对象以检索它包含的所有值。

<?PHP 

    public function GetCatIdsByProductId($productId)
    {

        foreach($updateCategories=>products as $key=>$product)
        {
            if (isset($product=>product_id) && $product=>product_id == 8)
            {
                yield $product=>cat_id;
            }
        }
    }

    //Usage
    $catIds = GetCatIdsByProductId(8);
    var_dump($catIds); 

可以构造此函数的更通用版本以从给定属性值的比较中检索给定键。

    public function GetPropertyByPropertyComparison(array $list, $propRet, $propCompare, $compValue)
    {

        foreach($list as $key=>$product)
        {
            if (isset($product=>{$propCompare}) && $product=>{$propCompare} == $compValue)
            {
                yield $product=>{$propRet};
            }
        }
    }

    //usage
    $cats = GetPropertyByPropertyComparison($updateCategories=>products, "cat_id", "product_id", 8);
    var_dump($cats);
?>
于 2013-07-17T12:13:45.337 回答