PHP中数组内的关联数组

时间:2021-08-20 08:52:58

After fetching a result from the database, and preparing the array for the JSON enconde, I face a dilemma on how to reference the array 'data' inside the main array.

在从数据库中获取结果并为JSON enconde准备数组之后,我面临着如何在主数组中引用数组'data'的两难选择。

The "[0]" by far is an error of my logic...

到目前为止“[0]”是我逻辑错误的...

while ($row =$result->fetch()) {
    $name     = $row['country'];
    $id       = $row['id']
    $username = $row['username'];
    $subtotal = $row['subtotal'];

    if ( /* If $id exist in array row['series']}*/ ) {

        ///?????/////
        /// Add array to 'DATA'
        ////?????////

        $rows['series']["$id"]["$name"]['data'][]=array("$username", $subtotal);
    }
    else {
        $rows['series'][]= array('id' => "$id", 'name' => "$name", 'data' => array("$username", $subtotal));
    }

The vardump show as follow:

vardump显示如下:

array(1) {
  ["series"]=>
  array(2) {
    [0]=>
    array(3) {
      ["id"]=>
      string(7) "hn"
      ["name"]=>
      string(8) "HN"
      ["data"]=>
      array(2) {
        [0]=>
        string(17) "GK_5"
        [1]=>
        string(5) "86040"
      }
    }
    [1]=>
    array(3) {
      ["id"]=>
      string(7) "hn"
      ["name"]=>
      string(8) "HN"
      ["data"]=>
      array(2) {
        [0]=>
        string(17) "GK_8"
        [1]=>
        string(5) "20358"
      }
    }
  }
}

But I want to add the last item with same id/name like this:

但我想添加具有相同ID /名称的最后一项,如下所示:

array(1) {
  ["series"]=>
  array(2) {
    [0]=>
    array(3) {
      ["id"]=>
      string(7) "hn"
      ["name"]=>
      string(8) "HN"
      ["data"]=>
      array(2) {
        [0]=>
        string(17) "GK_5"
        [1]=>
        string(5) "86040"
      }
      array(2) {
        [0]=>
        string(17) "GK_8"
        [1]=>
        string(5) "20358"
      }
    }
  }
}

1 个解决方案

#1


0  

The most natural way would be to change your data structure a bit, so that the series array is indexed by the row ID instead of an arbitrary running index. The would allow you to rewrite your loop to (using mysqli syntax as an example):

最自然的方法是稍微更改一下数据结构,以便系列数组由行ID而不是任意运行索引编制索引。这将允许您重写您的循环(使用mysqli语法作为示例):

$stmt->bind_result($id, $country, $username, $subtotal);

$series = array();
while ( $stmt->fetch() ) {
     $series[$id]['country'] = $country;
     $series[$id]['data'][] = array(
         'username' => $username,
         'subtotal' => $subtotal,
     );
}

which will give you a data structure like:

这将为您提供如下数据结构:

$series = array(
    'hn' => array(
        'country' => 'HN',
        'data' => array(
            0 => array(
                'username' => 'GK_5',
                'subtotal' => 86040
            ),
            1 => array(
                'username' => 'GK_8',
                'subtotal' => 20358
            ),
            // ...
        )
    ),
    // ...
);

If you need the data in the exact format shown in your post, you can of course loop over this array with foreach to transform it, e.g.:

如果你需要你帖子中显示的确切格式的数据,你当然可以用foreach循环这个数组来转换它,例如:

$rows = array();
foreach ( $series as $id => $record ) {
    $record['id'] = $id;
    $rows['series'][] = $record;
}

#1


0  

The most natural way would be to change your data structure a bit, so that the series array is indexed by the row ID instead of an arbitrary running index. The would allow you to rewrite your loop to (using mysqli syntax as an example):

最自然的方法是稍微更改一下数据结构,以便系列数组由行ID而不是任意运行索引编制索引。这将允许您重写您的循环(使用mysqli语法作为示例):

$stmt->bind_result($id, $country, $username, $subtotal);

$series = array();
while ( $stmt->fetch() ) {
     $series[$id]['country'] = $country;
     $series[$id]['data'][] = array(
         'username' => $username,
         'subtotal' => $subtotal,
     );
}

which will give you a data structure like:

这将为您提供如下数据结构:

$series = array(
    'hn' => array(
        'country' => 'HN',
        'data' => array(
            0 => array(
                'username' => 'GK_5',
                'subtotal' => 86040
            ),
            1 => array(
                'username' => 'GK_8',
                'subtotal' => 20358
            ),
            // ...
        )
    ),
    // ...
);

If you need the data in the exact format shown in your post, you can of course loop over this array with foreach to transform it, e.g.:

如果你需要你帖子中显示的确切格式的数据,你当然可以用foreach循环这个数组来转换它,例如:

$rows = array();
foreach ( $series as $id => $record ) {
    $record['id'] = $id;
    $rows['series'][] = $record;
}