gpt4 book ai didi

php - 从 php select 创建多维数组

转载 作者:行者123 更新时间:2023-11-29 02:53:54 24 4
gpt4 key购买 nike

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']
);
}

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

关于php - 从 php select 创建多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32752734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com