将 php 数组动态传递给 javascript


Dynamic passing of php array to javascript

我尝试在我的php代码javscript中传递我的数组。我想得到我的foreach的结果。问题是我只得到最后一个数组。如何在控制台中获取每个数组.log() ?PHP代码:

function users_nb_match($value){
  $tab_nb_match_par_user = array();
  $mois_choisi = "";
  $match_ou_catego = "matching";//"categorisation";
  if(strlen($mois_choisi)==0){
      $res = sql_dico("SELECT MAX(mois) AS zemois FROM stats_".$match_ou_catego."_univers WHERE LENGTH(mois)=7");//mois de janvier
      if($res && $rez = mysql_fetch_object($res))
          $mois_choisi = $rez->zemois;
  }
  //echo formate_pieChart("Column",$tab_values,$_GET['univers']);
  $zedate = "";
  $res = sql_dico("SELECT mois, login, nb_match FROM   stats_".$match_ou_catego."_univers 
  WHERE login != '' ".((strlen($value)>0)?"
  AND univers = '".$value."'":"")." ".((strlen($mois_choisi)>0)?"
  AND mois IN ('".$mois_choisi."')":"")."
  AND LENGTH(mois)=7 ORDER BY nb_match DESC");
  //tab_value_user c'est le nb matching par utilisateurs
  while($res && $rez = mysql_fetch_object($res)){
      if(isset($tab_nb_match_par_user[$rez->login]))
          $tab_nb_match_par_user[$rez->login] += $rez->nb_match;
      else
          $tab_nb_match_par_user[$rez->login] = $rez->nb_match;
      $zedate = $rez->mois;
  }
  arsort($tab_nb_match_par_user);   
  return $tab_nb_match_par_user;}
  foreach($tab_bases as $key => $values){
      $tab_nb_match_par_user = users_nb_match($values);
  }

Javascript(在同一页面上)

<script type="text/javascript">
  var tab_nb_match_par_user = <?php   print_r(json_encode($tab_nb_match_par_user)); ?>;
  console.log(tab_nb_match_par_user);</script>

$tab_base 包含 database_name,因此每个数据库名称都有一个查询,使用此查询我构建一个数组

编辑帖子 :在这个平台上,当我选择" Tous les univers "我只得到最后一个数组。我不动态检索我的其他人 javascript 数组。我像这样生成我的图表:

function formate_pieChart(){
 $PieChart = "<div id='chart1' style='width:400px;height:400px'></div>";
 return  $PieChart;}
   function formate_barChart(){
  $BarChart = "<div id='chart2' style= 'width:60%;height:400px'></div>";
  return $BarChart;}
function formate_imageChart(){
$imgMi = "<div id='chart3' style='width:100%;height:200px;margin-right:10%'></div>";
return $imgMi;}

function genere_code($choix){
switch($choix){
    case 0:{
        return formate_barChart();
        break;
    }
    case 1:{
        return formate_pieChart();
        break;
    }
    case 2:{
        return formate_imageChart();
        break;
    }
    default:{
        return formate_barChart();
        break;
    }
  }}
if($univers=="tous") {//la variable $bdd est fixe à cet endroit
  echo genere_code(1);
}
foreach($tab_bases as $key => $values){
    $tab_nb_match_par_user = users_nb_match($values);
    $tab_mois_nb_match = mois_nb_match($values);
    echo genere_code(1);
}

如果我点击ec_be或等图表加载,但如果我点击"tous les univers",只有最后一个图表正在运行......注意:值是"tous",表示"tous les univers"

我想得到我的foreach的结果。问题是我只 获取最后一个数组。如何在控制台中获取每个数组.log() ?

改变

foreach($tab_bases as $key => $values)
{
      $tab_nb_match_par_user = users_nb_match($values);
}

foreach($tab_bases as $key => $values)
{
      $tab_nb_match_par_user[] = users_nb_match($values);
}