2

I have the following array:

array(5) {
  [83]=>
  object(stdClass)#39 (17) {
    ["id"]=>
    int(83)
    ["product_id"]=>
    int(15)
    ["area_id"]=>
    int(2)
    ["termtype_id"]=>
    int(40)
    ["name"]=>
    string(23) "XXXXXX"
  }
 [89]=>
  object(stdClass)#398 (17) {
    ["id"]=>
    int(89)
    ["product_id"]=>
    int(15)
    ["area_id"]=>
    int(2)
    ["termtype_id"]=>
    int(40)
    ["name"]=>
    string(23) "YYYYY"
  }
[102]=>
  object(stdClass)#394 (17) {
    ["id"]=>
    int(102)
    ["product_id"]=>
    int(23)
    ["area_id"]=>
    int(2)
    ["termtype_id"]=>
    int(40)
    ["name"]=>
    string(23) "ZZZZZ"
  }
[104]=>
  object(stdClass)#397 (17) {
    ["id"]=>
    int(104)
    ["product_id"]=>
    int(23)
    ["area_id"]=>
    int(2)
    ["termtype_id"]=>
    int(40)
    ["name"]=>
    string(23) "AAAAA"
  }
[107]=>
  object(stdClass)#399 (17) {
    ["id"]=>
    int(107)
    ["product_id"]=>
    int(23)
    ["area_id"]=>
    int(2)
    ["termtype_id"]=>
    int(40)
    ["name"]=>
    string(23) "KKKK"
  }
}

The above array is generated from a sql query and iterated using the following function:

 public function keyArray($arr) {
        $result = [];
        foreach($arr as $element) {
            $result[$element->id] = $element;
        }
        return $result;
    }

Is there anyway I can iterate the above ARRAY and get all data related to product_id?

following would be a result:

array(2){
[83]=>
  object(stdClass)#39 (17) {
    ["id"]=>
    int(83)
    ["product_id"]=>
    int(15)
    ["area_id"]=>
    int(2)
    ["termtype_id"]=>
    int(40)
    ["name"]=>
    string(23) "XXXXXX"
  }
 [89]=>
  object(stdClass)#398 (17) {
    ["id"]=>
    int(89)
    ["product_id"]=>
    int(15)
    ["area_id"]=>
    int(2)
    ["termtype_id"]=>
    int(40)
    ["name"]=>
    string(23) "YYYYY"
  }
}

Any help would be appreciated?

3
  • You have duplicate keys for ID... What's the logic on that? Does it need to overwrite the previous? Or just ignore it? Commented Jun 1, 2016 at 7:33
  • you are write: I have changed it Commented Jun 1, 2016 at 7:36
  • array_map(function ($ar) { if($ar->product_id == $productID){ return $ar; }}, $array); this single line code will work for you .. Commented Jun 1, 2016 at 8:33

4 Answers 4

2

You can use array_filter function:

public function keyArray(array $data, $productId) {
    return array_filter($data, function($element) use($productId) {
        return $element->product_id == $product_id;
    })
}

It returns array with only objects which product_id equal searched value.

Sign up to request clarification or add additional context in comments.

Comments

0

Convert your function like this, $searchProductId is the product_id for search all data related to product_id:

    public function keyArray($arr, $searchProductId) {
        $result = [];
        foreach($arr as $element) {
            if($element->product_id == $searchProductId){
                $result[$element->id] = $element;
            }
        }
        return $result;
    }

Also if possible, its better to filter results at database level. Just add a condition in your database query for required product_id.

Comments

0

You could sort the Arrays into a Multidimensional Array and Group them using the product_ids. Here is an Example of what is meant here:

            <?php
                //FIRST WE SIMULATE SOMETHING SIMILAR TO YOUR DB DATA TO WORK WITH    
                $objData1   = new stdclass();
                $objData2   = new stdclass();
                $objData3   = new stdclass();
                $objData4   = new stdclass();
                $objData5   = new stdclass();

                $objData1->id           = 83;
                $objData1->product_id   = 15;
                $objData1->area_id      = 2;
                $objData1->termtype_id  = 40;
                $objData1->name         = "XXXXXX";


                $objData2->id           = 83;
                $objData2->product_id   = 15;
                $objData2->area_id      = 2;
                $objData2->termtype_id  = 40;
                $objData2->name         = "YYYYYY";


                $objData3->id           = 83;
                $objData3->product_id   = 23;
                $objData3->area_id      = 2;
                $objData3->termtype_id  = 40;
                $objData3->name         = "ZZZZZZ";


                $objData4->id           = 83;
                $objData4->product_id   = 23;
                $objData4->area_id      = 2;
                $objData4->termtype_id  = 40;
                $objData4->name         = "AAAAAA";

                $objData5->id           = 83;
                $objData5->product_id   = 23;
                $objData5->area_id      = 2;
                $objData5->termtype_id  = 40;
                $objData5->name         = "KKKK";

                $arrDBData  = array(
                    83  => $objData1,
                    89  => $objData2,
                    102 => $objData3,
                    104 => $objData4,
                    107 => $objData5,
                );


               // NEXT WE ROLL-OUT A FUNCTION TO GROUP THE DATA USING PRODUCT IDS:

                function groupAllDataByProductID($arrOfObjects){
                    $arrResults                 = array();
                    $PID                        = null;
                    foreach($arrOfObjects as $intKey=>$objData){
                        if(!array_key_exists($objData->product_id, $arrResults)){
                            $PID                = $objData->product_id;
                            $arrResults[$PID]   = array();
                            $arrResults[$PID][] = $objData;
                        }else{
                            $PID                = $objData->product_id;
                            $arrResults[$PID][] = $objData;
                        }
                    }
                    return $arrResults;
                }

                // FINALLY WE TEST THE RESULT WITH PHP'S VAR_DUMP               
                var_dump( groupAllDataByProductID($arrDBData) );

And this is the Result of the var_dump(). Notice that the Key of the Outer Array represents the product_id, and the nested Arrays are Items with the same product_id...

                array (size=2)
                  15 => 
                    array (size=2)
                      0 => 
                        object(stdClass)[1]
                          public 'id' => int 83
                          public 'product_id' => int 15
                          public 'area_id' => int 2
                          public 'termtype_id' => int 40
                          public 'name' => string 'XXXXXX' (length=6)
                      1 => 
                        object(stdClass)[2]
                          public 'id' => int 83
                          public 'product_id' => int 15
                          public 'area_id' => int 2
                          public 'termtype_id' => int 40
                          public 'name' => string 'YYYYYY' (length=6)
                  23 => 
                    array (size=3)
                      0 => 
                        object(stdClass)[3]
                          public 'id' => int 83
                          public 'product_id' => int 23
                          public 'area_id' => int 2
                          public 'termtype_id' => int 40
                          public 'name' => string 'ZZZZZZ' (length=6)
                      1 => 
                        object(stdClass)[4]
                          public 'id' => int 83
                          public 'product_id' => int 23
                          public 'area_id' => int 2
                          public 'termtype_id' => int 40
                          public 'name' => string 'AAAAAA' (length=6)
                      2 => 
                        object(stdClass)[5]
                          public 'id' => int 83
                          public 'product_id' => int 23
                          public 'area_id' => int 2
                          public 'termtype_id' => int 40
                          public 'name' => string 'KKKK' (length=4)

Comments

-1

I believe this would work...

public function getProductArrays($arr,$iProductID) {
    $arrResult = array();
    foreach($arr as $id => $class) {
        if ($iProductID == $class->product_id)
            $arrResult[$id] = $class;
    }
    return $arrResult;
}

2 Comments

I already gave answer, what is the difference between my code and your code??
steady on there... don't be so aggressive! I didn't see yours while I was writing and submitting mine

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.