PHP循环进入JSON数组

时间:2017-05-30 18:59:14

标签: php arrays json

我试图创建JSON数组,使用while循环中的信息将它们回传给Slack。

以下是我提出的代码:

$adset = null;

while ($row = mysqli_fetch_array($leadsAdsetsQuery)) {
  if ($row["adset"] != $adset) {
    $adset = $row["adset"];
    echo "{$adset}<br>";
  }
  echo $row["id"] . "<br>";
}

输出结果为:

Adset Name 1
20
Adset Name 2
34
Adset Name 3
11

我需要为每个Adset Name设置一个新的JSON数组,其中包含引线结构的数量,如下所示:

  'attachments' => 
  array (
    0 => 
    array (
      'fallback' => 'XXX',
      'text' => 'XXX',
      'fields' => 
      array (  
        0 => 
        array (
          'title' => 'Adset Name 1',
          'value' => '20',
          'short' => true,
        ),
        1 => 
        array (
          'title' => 'Adset Name 2',
          'value' => '34',
          'short' => true,
        ),
        2 => 
        array (
          'title' => 'Adset Name 3',
          'value' => '11',
          'short' => true,
        ),
      ),
      'color' => '#F35A00',
    ),
  )

我不是JSON的大专家,也不是PHP的专家,谷歌似乎没有帮助。

1 个答案:

答案 0 :(得分:2)

require(xgboost)
X <- matrix(rnorm(10000, mean = 10),nrow = 100)
y <- sample(c(0,1) , 100, replace = T)

bst1 <- xgboost(data = X, label = y, nrounds = 20)

# gonna scale test and not train
# test predictions are the same
p <- predict(  bst1, newdata = scale(X))
> p
  [1] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
  [9] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [17] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [25] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [33] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [41] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [49] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [57] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [65] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [73] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [81] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [89] 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985 0.3643985
 [97] 0.3643985 0.3643985 0.3643985 0.3643985