如何访问阵列数据


How to access Array data

我有一个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的My Array输出

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

最终目标是通过virtualmart_custom_id从数组中输出XML,因此:

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

问题:如果我的目标是$custom[1],那么如果一行为空,数据可能会更改。。令人不快的即echo$custom[1]->value。

在"选择"我需要的virtuemart_custom_id的同时,我能实现上述输出的最佳方式是什么?

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

用作

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