三重循环数组

时间:2018-07-31 16:02:43

标签: php mysql

我正在尝试获取具有特定值的字符串。问题出在FIND_IN_SET上,因为如果$find_demographics不在for循环中,则$j无法正常工作。另外,如果我尝试将$$demographics_values移至$j进行循环,则值的名称将无效。

user_aboutm_z_analytics中的内容并不重要,因为我只想在$$demographics_values字符串的FIND_IN_SET内插入正确的值。

PS。如何更改代码以获得5.6版本的最快响应?

$$demographics_values字符串的期望样本:

$g_demographics_value_1 = $db->Query("$shortcode FIND_IN_SET('a01', b.about_education)");
...
$g_demographics_value_18 = $db->Query("$shortcode FIND_IN_SET('b01', b.about_personal)");
$g_demographics_value_19 = $db->Query("$shortcode FIND_IN_SET('b02', b.about_personal)");
$g_demographics_value_20 = $db->Query("$shortcode FIND_IN_SET('c01', b.about_personal)");
$g_demographics_value_21 = $db->Query("$shortcode FIND_IN_SET('c02', b.about_personal)");
$g_demographics_value_22 = $db->Query("$shortcode FIND_IN_SET('c03', b.about_personal)");
...
// the last string
$g_demographics_value_73 = $db->Query("$shortcode FIND_IN_SET('g27', b.about_work)");

代码:

$shortcode = "SELECT COUNT(a.id) AS id FROM m_z_analytics a LEFT JOIN user_about b ON a.user_id=b.id WHERE";

$a_l_demographics = [17,2,10,2,3,12,27];
$a_p_demographics  = ['a','b','c','d','e','f','g'];
$a_c_demographics = min(count($a_l_demographics), count($a_p_demographics));

for ($i = 0, $k = 1; $i < $a_c_demographics, $k <= array_sum($a_l_demographics); $i++, $k++) {

  $demographics_values = 'g_demographics_value_'.$k;

  for ($j = 1; $j <= $a_l_demographics[$i]; $j++) {

    // $i here shows 00000000000000000112222222222334445555...

    $find_demographics = $a_p_demographics[$i] . str_pad($j, 2, 0, STR_PAD_LEFT);

  }

  // $k is important for me to get value from the right rows in database. $k is based on array_sum($a_l_demographics)

  switch (true) {
    case $k <= 1:

      // from $find_demographics I should get for example `a01`, to use it later in FIND_IN_SET('a01', b.about_education)

      $$demographics_values = $db->Query("$shortcode FIND_IN_SET('".$find_demographics."', b.about_education)");

      // I expect: $g_demographics_value_1['id'] have `a01` in FIND_IN_SET, $g_demographics_value_2['id'] `a02`...

      break;
    case $k <= 17:
      $$demographics_values = $db->Query("$shortcode FIND_IN_SET('".$find_demographics."', b.about_personal)");
      break;
    case $k <= 36:
      $$demographics_values = $db->Query("$shortcode FIND_IN_SET('".$find_demographics."', b.about_work)");
      break;
  }

}

0 个答案:

没有答案