如何从多维数组值中获取值

时间:2013-07-17 11:50:59

标签: php multidimensional-array

    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。我怎么能这样做?

7 个答案:

答案 0 :(得分:1)

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

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

将为您提供一系列产品ID为8

的cat ID

答案 1 :(得分:1)

$newarr = array();
foreach( $arr['updateCategories']['products'] as $myarr)
{
  if($myarr['product_id'] == 8)
  $newarr[] =  $myarr['cat_id'];
}

答案 2 :(得分:1)

最简单的解决方案是

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

其中$yourArray是您已发布的转储数组。

答案 3 :(得分: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

答案 4 :(得分: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'];
    }
    }

答案 5 :(得分:1)

您可以使用 array_filter

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

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

$myArray是帖子中显示的数组。

答案 6 :(得分: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);
?>