从PHP select创建多维数组


while ($selected_row = $stmt - > fetch(PDO::FETCH_ASSOC)) {
    $tmp_key = $selected_row['tin']; //adding a temp key
    //$tmp_key = $selected_row['tin']; //adding a temp key
    $new_data[$tmp_key]['tin'] = $selected_row['ownertin'];
    $new_data[$tmp_key]['lastname'] = $selected_row['ownerlastname'];
    $new_data[$tmp_key]['firstname'] = $selected_row['ownerfirstname'];
    $new_data[$tmp_key]['ownershipfrom'] = $selected_row['ownershipfrom'];
    $new_data[$tmp_key]['type'] = $selected_row['ownership'];
    $new_data[$tmp_key]['middleinitial'] = $selected_row['ownermiddlename'];
    $new_data[$tmp_key]['suffix'] = $selected_row['ownersuffix'];
    $sudky = (isset($new_data[$tmp_key]['Address'])) ? count($new_data[$tmp_key]['Address']) : 0; //getting the key for student child array
    $new_data[$tmp_key]['Address'][$sudky]['contactflag'] = $selected_row['contact_flag'];
    $new_data[$tmp_key]['Address'][$sudky]['tin'] = $selected_row['ownertin'];
    $new_data[$tmp_key]['Address'][$sudky]['mobile'] = $selected_row['mobile'];
    $new_data[$tmp_key]['Address'][$sudky]['landline'] = $selected_row['landline'];
    $new_data[$tmp_key]['Address'][$sudky]['email'] = $selected_row['email'];
    $new_data[$tmp_key]['Address'][$sudky]['province'] = $selected_row['addressprovince'];
    $new_data[$tmp_key]['Address'][$sudky]['municipality'] = $selected_row['addressmunicipality'];
    $new_data[$tmp_key]['Address'][$sudky]['barangay'] = $selected_row['addressbarangay'];
    $new_data[$tmp_key]['Address'][$sudky]['street'] = $selected_row['addressstreet'];
    $new_data[$tmp_key]['Address'][$sudky]['zipcode'] = $selected_row['addresszipcode'];
}
$new_data = array_values($new_data);
$input = array_map("unserialize", array_unique(array_map("serialize", $new_data)));
//print_r($new_data);
echo json_encode($new_data, JSON_UNESCAPED_UNICODE);

有没有更好的方法来创建这个多维数组没有重复的地址

我更喜欢这种构建数组的结构,因为(至少对我来说)它更清楚什么在哪里。

$new_data = array();
while($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $tmp_key = $selected_row['tin'];
    // don't write the same data over and over again
    if(!isset($newData[$tmp_key])) {
        $newData[$tmp_key] = array(
            'tin'           => $selected_row['ownertin'],
            'lastname'      => $selected_row['ownerlastname'],
            'firstname'     => $selected_row['ownerfirstname'],
            'ownershipfrom' => $selected_row['ownershipfrom'],
            'type'          => $selected_row['ownership'],
            'middleinitial' => $selected_row['ownermiddlename'],
            'suffix'        => $selected_row['ownersuffix'],
            'Address'       => array()
        );
    }
    $newData[$tmp_key]['Address'][$selected_row['contact_flag']] = array(
        'contactflag'  => $selected_row['contact_flag'],
        'tin'          => $selected_row['ownertin'],
        'mobile'       => $selected_row['mobile'],
        'landline'     => $selected_row['landline'],
        'email'        => $selected_row['email'],
        'province'     => $selected_row['addressprovince'],
        'municipality' => $selected_row['addressmunicipality'],
        'barangay'     => $selected_row['addressbarangay'],
        'street'       => $selected_row['addressstreet'],
        'zipcode'      => $selected_row['addresszipcode']
    );
}

由于地址没有重复,这应该产生所需的数组结构

相关内容

  • 没有找到相关文章

最新更新