Malarivtan
Malarivtan

Reputation: 414

php iterate an associative array

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?

Upvotes: 2

Views: 106

Answers (4)

HarisH Sharma
HarisH Sharma

Reputation: 1267

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.

Upvotes: 0

Poiz
Poiz

Reputation: 7617

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)

Upvotes: 0

Pyton
Pyton

Reputation: 1319

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.

Upvotes: 2

Barry
Barry

Reputation: 3318

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

Upvotes: -1

Related Questions