2013-07-17 84 views
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 
         ) 

值以上陣列是我的輸出數組,但我需要得到product_id=8的所有cat_id。我怎樣才能做到這一點?如何從多維數組值

+0

迭代這個數組,並找到它... – 2013-07-17 11:53:35

回答

1

可以做這樣的事情

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

處理這個which'll留給你有8

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

產品ID貓ID數組的simpliest解決方案是

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

其中$yourArray是您已發佈轉儲的數組。

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

您可以使用array_filter

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

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

其中$myArray是顯示在您的帖子中的數組。

+0

但是如果你需要,因爲它是在你有再通過所有的數組遍歷問題中提到'cat_id's的只是陣列。 – zavg

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