2014-10-22 73 views
0

我有一個SQL語句檢索一個數組,我想訪問該數據。如何訪問陣列數據

我的SQL(的Joomla語法):

$fields->select(array('a.virtuemart_product_id', 
     'a.virtuemart_custom_id', 'a.virtuemart_custom_id', 'v.value', 'r.intval')) 
      ->from('#__virtuemart_product_customfields AS a') 
      ->join('INNER', '#__virtuemart_product_custom_plg_param_ref AS r 
    ON (a.virtuemart_custom_id = r.virtuemart_custom_id 
    AND a.virtuemart_product_id = r.virtuemart_product_id)') 
      ->join('INNER', '#__virtuemart_product_custom_plg_param_values 
    AS v ON (r.val = v.id)') 
      ->where('a.virtuemart_product_id='.$vehicle_id) 
      ->order('a.virtuemart_custom_id ASC'); 

    $db->setQuery($fields); 
    // Load the results as a list of stdClass objects. 
    $customs = $db->loadObjectList(); 

我陣的$customs輸出爲一個示例

Array 
(
[0] => stdClass Object 
    (
     [virtuemart_product_id] => 675 
     [virtuemart_custom_id] => 38 
     [value] => 2200 TD 
     [intval] => 0 
    ) 

[1] => stdClass Object 
    (
     [virtuemart_product_id] => 675 
     [virtuemart_custom_id] => 39 
     [value] => 6 Berth 
     [intval] => 0 
    ) 

[2] => stdClass Object 
    (
     [virtuemart_product_id] => 675 
     [virtuemart_custom_id] => 40 
     [value] => Coachbuilt 
     [intval] => 0 
    ) 

[3] => stdClass Object 
    (
     [virtuemart_product_id] => 675 
     [virtuemart_custom_id] => 41 
     [value] => 30990 
     [intval] => 0 
    ) 

[4] => stdClass Object 
    (
     [virtuemart_product_id] => 675 
     [virtuemart_custom_id] => 42 
     [value] => MX08 JVR 
     [intval] => 0 
    ) 

[5] => stdClass Object 
    (
     [virtuemart_product_id] => 675 
     [virtuemart_custom_id] => 43 
     [value] => Manual 
     [intval] => 0 
    ) 

[6] => stdClass Object 
    (
     [virtuemart_product_id] => 675 
     [virtuemart_custom_id] => 44 
     [value] => L23'7'' 
     [intval] => 0 
    ) 

[7] => stdClass Object 
    (
     [virtuemart_product_id] => 675 
     [virtuemart_custom_id] => 47 
     [value] => 2008 
     [intval] => 0 
    ) 

[8] => stdClass Object 
    (
     [virtuemart_product_id] => 675 
     [virtuemart_custom_id] => 53 
     [value] => Front Lounge 
     [intval] => 0 
    ) 

[9] => stdClass Object 
    (
     [virtuemart_product_id] => 675 
     [virtuemart_custom_id] => 54 
     [value] => UNDER 3500kg 
     [intval] => 0 
    ) 

)上面

foreach內,簡單地選擇從所有產品數據庫。 $vehicle_id

的最終目標是輸出XML由virtuemart_custom_id數組,所以:

<Example> [value where virtuemart_custom_id = 1 ] </Example> 
<ExampleTwo> [value where virtuemart_custom_id = 2] </ExampleTwo> 

問題:如果我的目標$海關[1],數據可能行是否空改變..壞?即echo $ customs [1] - >值。

當我選擇virtuemart_custom_id我需要時,我可以達到上述輸出的最佳方式是什麼?

回答

0
foreach($customs as $key=>$object){ 
     if($object->virtuemart_custom_id==1){ 
      //<Example> [value where virtuemart_custom_id = 1 ] </Example> 
      echo $object->value; 
     } 
} 

事實上,你可以提高後,它並擁有所有的值的新陣列很好地這樣

$values=array(); 
foreach($customs as $key=>$object){ 
    $values[$object->virtuemart_custom_id] = $object->value; 
} 
print_r($values); // This will give you all values indexed on their custom ids 
0

用作

foreach($customs as $single){ 
     if($single->virtuemart_custom_id==1){ 
      //<Example> [value where virtuemart_custom_id = 1 ] </Example> 
      echo $single->value; 
     } 
}