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
                        )

The above array is my output array but I need to get all cat_id of product_id=8. How can I do this?

1
  • iterate over the array and find it... Commented Jul 17, 2013 at 11:53

7 Answers 7

1
$newarr = array();
foreach( $arr['updateCategories']['products'] as $myarr)
{
  if($myarr['product_id'] == 8)
  $newarr[] =  $myarr['cat_id'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

The simpliest solution is

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

where $yourArray is the array which dump you have published.

Comments

1

Try something like this:

$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

Comments

1

Use this

 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'];
    }
    }

Comments

1

You can use array_filter.

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

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

Where $myArray is the array displayed in your post.

1 Comment

But if you need just array of cat_ids as it is mentioned in question you have to iterate through all array again.
1

Can handle this by doing something like this

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

which'll leave you with an array of cat ids that have a product id of 8

Comments

1

This should be able to retrieve all of the cat_id's from a given product_id. This function yields an object that can be iterated over to retrieve all the values it contains.

<?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); 

A more generic version of this function can be constructed to retrieve a given key from a comparison on a given property value.

    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);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.