2014-10-22 40 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; 
     } 
}