【问题标题】:php multidimension array generated from database [closed]从数据库生成的php多维数组[关闭]
【发布时间】:2014-10-28 13:58:24
【问题描述】:

我正在编写一个代码,我从这篇文章中获得了大部分功能: https://stackoverflow.com/a/19582946/1316372

但我尝试从 Mysql 数据库中获取数据,如下所示:

//my 2 db queries
  $query = array();
  $properties_array = array();

  $selector_query = tep_db_query("SELECT * FROM bts_selectors");
  while ($selector = tep_db_fetch_array($selector_query)) {
  $query[] = array('id' => (int)$selector['id'], 
                         'selector' => $selector['selector']
                         );
 }

 $properties_query = tep_db_query("SELECT * FROM bts_properties WHERE selector_id= '".(int)$selector['id']."'");
  while ($properties_result = tep_db_fetch_array($properties_query)) {
  $properties_array[] = array('id' => (int)$properties_result['id'], 
                            'selector_id' => (int)$properties_result['selector_id'],
                            'css_element' => $properties_result['css_el'],
                            'element_value' => $properties_result['css_val']
                         );
} 

//this is a working static array
$probs_good =array(
    1 => array(
        array('id' => 1, 'selector_id' => 1, 'css_element' => 'border', 'element_value' => '3px solid'),
        array('id' => 2, 'selector_id' => 1, 'css_element' => 'padding', 'element_value' => '10px')
    )
); 
///here the output should be generated
$css = '';
foreach($query as $selector){

    //$properties = $probs_good[$selector['id']]; //this the working static array
    $properties = $properties_array[$selector['id']];

    $rules = '';
    foreach($properties as $element){
        $rules .= "\n \t$element[css_element]:$element[element_value];";
    }

    $css .= "$selector[selector]".'{'."$rules \n".'}'."\n\n";
}
echo "<pre>";
echo "$css";
echo "</pre>";

我知道我遍历foreach 的方式要求数组应该具有与$probs_good 相同的结构,但老实说,我每次尝试都失败了。 在我所指的帖子中,有一个引用的查询。 我也做了一些尝试来遵守这一点。 现在我认为最好只显示干净/简单的代码并解释我试图实现的目标。

【问题讨论】:

  • 我预计答案很快就会关闭,我需要做更多调查。
  • 我希望在我接受自己的答案之前不要关闭它,因此对其他人有帮助。

标签: php mysql arrays multidimensional-array foreach


【解决方案1】:

好的,经过研究,我找到了解决方案(很简单,但从来没有……)

      $selectors_array = array();
      $selector_query = tep_db_query("SELECT * FROM bts_selectors");
      while ($selector = tep_db_fetch_array($selector_query)) {
      $selectors_array[] = array('id' => $selector['id'], 
                             'selector' => $selector['selector']
                             );
    }

    $css = '';
    foreach ( $selectors_array as $selector ) { 
    $rules = '';

    $properties_query = tep_db_query("SELECT * FROM bts_properties WHERE selector_id = '" . $selector['id'] . "' ");

   while ($properties = tep_db_fetch_array($properties_query)) {    
    $rules .= "\n \t$properties[css_el]:$properties[css_val];";
    }
        $css .= "$selector[selector]".'{'."$rules \n".'}'."\n\n";
    }

    echo "<pre>";
    echo "$css";
    echo "</pre>";

生成和想要的输出:

.thisone{
border:1px solid;
padding:10px; 
}

#thatone{
border:1px solid; 
}

.body{
width:40px;
height:40px; 
}

【讨论】:

    猜你喜欢
    • 2016-12-07
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多