[0] => {"butapid":"50","name":"Output CST 2% ( with C form)","description":"","value":"2.00000000","butapsid":"1","butaptid":"1","created_uid":"1","updated_uid":"1","created_ts":"2017-03-31 11:36:52","updated_ts":"2018-01-12 20:10:08","butapatid":"1","butapappid":"1","alid":"199","gbutapid":"2","gbusupid":"6","butaprid":"1","tax_type":"2","company_id":"1","global_business_sub_profile":null,"businessTaxProfileType":null,"businessTaxProfileAmountType":null,"global_business_tax_profile":null}
[1] => {"butapid":"51","name":"Output Vat 5.5%","description":"f","value":"5.50000000","butapsid":"1","butaptid":"1","created_uid":"1","updated_uid":"1","created_ts":"2017-04-06 18:55:04","updated_ts":"2018-01-13 16:18:13","butapatid":"1","butapappid":"1","alid":"139","gbutapid":"1","gbusupid":"4","butaprid":"1","tax_type":"1","company_id":"1","global_business_sub_profile":null,"businessTaxProfileType":null,"businessTaxProfileAmountType":null,"global_business_tax_profile":null}
[2] => {"butapid":"57","name":"Output Service Tax 15%","description":"","value":"15.00000000","butapsid":"1","butaptid":"1","created_uid":"1","updated_uid":"1","created_ts":"2017-04-12 13:46:29","updated_ts":"2018-03-29 18:29:56","butapatid":"1","butapappid":"1","alid":"177","gbutapid":"3","gbusupid":"8","butaprid":"3","tax_type":"1","company_id":"1","global_business_sub_profile":null,"businessTaxProfileType":null,"businessTaxProfileAmountType":null,"global_business_tax_profile":null}
在每个数组的末尾,我想插入像“sub”=> 1或“sub”=> 0
[0] => {"butapid":"50","name":"Output CST 2% ( with C form)","description":"","value":"2.00000000","butapsid":"1","butaptid":"1","created_uid":"1","updated_uid":"1","created_ts":"2017-03-31 11:36:52","updated_ts":"2018-01-12 20:10:08","butapatid":"1","butapappid":"1","alid":"199","gbutapid":"2","gbusupid":"6","butaprid":"1","tax_type":"2","company_id":"1","global_business_sub_profile":null,"businessTaxProfileType":null,"businessTaxProfileAmountType":null,"global_business_tax_profile":null,"sub"=>1}
[1] => {"butapid":"51","name":"Output Vat 5.5%","description":"f","value":"5.50000000","butapsid":"1","butaptid":"1","created_uid":"1","updated_uid":"1","created_ts":"2017-04-06 18:55:04","updated_ts":"2018-01-13 16:18:13","butapatid":"1","butapappid":"1","alid":"139","gbutapid":"1","gbusupid":"4","butaprid":"1","tax_type":"1","company_id":"1","global_business_sub_profile":null,"businessTaxProfileType":null,"businessTaxProfileAmountType":null,"global_business_tax_profile":null,"sub"=>1}
[2] => {"butapid":"57","name":"Output Service Tax 15%","description":"","value":"15.00000000","butapsid":"1","butaptid":"1","created_uid":"1","updated_uid":"1","created_ts":"2017-04-12 13:46:29","updated_ts":"2018-03-29 18:29:56","butapatid":"1","butapappid":"1","alid":"177","gbutapid":"3","gbusupid":"8","butaprid":"3","tax_type":"1","company_id":"1","global_business_sub_profile":null,"businessTaxProfileType":null,"businessTaxProfileAmountType":null,"global_business_tax_profile":null,"sub"=>0}
if ($all_taxes)
{
foreach ($all_taxes as $tax)
{
$t = new BusinessTaxProfile($tax->getId());
$tax_array[] = strval($t);
$subprofile = BusinessTaxSubProfileManager::getBusinessSubProfiles($tax->getId(), 1);
}
}
如果$ subprofile不为空,那么“sub”=> 1否则“sub”=> 0。
我尝试了数组推送,但它制作了另一个数组。
答案 0 :(得分:0)
这可能看起来像这样:
if ($all_taxes)
{
foreach ($all_taxes as $tax)
{
$t = new BusinessTaxProfile($tax->getId());
$subprofile = BusinessTaxSubProfileManager::getBusinessSubProfiles($tax->getId(), 1);
$t->sub = (int)!empty($subprofile);
$tax_array[] = (string)$t;
}
}
但你必须编辑BusinessTaxProfile
__toString()
方法,因为我认为有生成json输出。
显示BusinessTaxProfile
然后我会编辑我的答案以适应。
答案 1 :(得分:0)
您可以使用foreach中的引用
来完成此操作$array = array(array("butapid"=>"50","name"=>"Output CST 2% ( with C form)","description"=>"","value"=>"2.00000000","butapsid"=>"1","butaptid"=>"1","created_uid"=>"1","updated_uid"=>"1","created_ts"=>"2017-03-31 11:36:52","updated_ts"=>"2018-01-12 20:10:08","butapatid"=>"1","butapappid"=>"1","alid"=>"199","gbutapid"=>"2","gbusupid"=>"6","butaprid"=>"1","tax_type"=>"2","company_id"=>"1","global_business_sub_profile"=>null,"businessTaxProfileType"=>null,"businessTaxProfileAmountType"=>null,"global_business_tax_profile"=>null),
array("butapid"=>"51","name"=>"Output Vat 5.5%","description"=>"f","value"=>"5.50000000","butapsid"=>"1","butaptid"=>"1","created_uid"=>"1","updated_uid"=>"1","created_ts"=>"2017-04-06 18:55:04","updated_ts"=>"2018-01-13 16:18:13","butapatid"=>"1","butapappid"=>"1","alid"=>"139","gbutapid"=>"1","gbusupid"=>"4","butaprid"=>"1","tax_type"=>"1","company_id"=>"1","global_business_sub_profile"=>null,"businessTaxProfileType"=>null,"businessTaxProfileAmountType"=>null,"global_business_tax_profile"=>null),
array("butapid"=>"57","name"=>"Output Service Tax 15%","description"=>"","value"=>"15.00000000","butapsid"=>"1","butaptid"=>"1","created_uid"=>"1","updated_uid"=>"1","created_ts"=>"2017-04-12 13:46:29","updated_ts"=>"2018-03-29 18:29:56","butapatid"=>"1","butapappid"=>"1","alid"=>"177","gbutapid"=>"3","gbusupid"=>"8","butaprid"=>"3","tax_type"=>"1","company_id"=>"1","global_business_sub_profile"=>null,"businessTaxProfileType"=>null,"businessTaxProfileAmountType"=>null,"global_business_tax_profile"=>null)
);
foreach ($array as $key => &$value) {
// ^^ it will allow you to manipulate same array
$subprofile = true; // change this with your code
if ($subprofile) {
$value['sub'] = 1;
} else {
$value['sub'] = 0;
}
}
echo "<pre>";
print_r($array); // it will have "sub" key added in respective array
答案 2 :(得分:0)
数组推送不起作用的原因是$all_taxes
的内容是JSON编码对象。因此,要添加它们,您需要先解码它们,然后更新值,然后将它们重新编码为JSON。您可以在循环中执行此操作:
foreach ($all_taxes as &$tax)
{
$t = new BusinessTaxProfile($tax->getId());
$tax_array[] = strval($t);
$subprofile = BusinessTaxSubProfileManager::getBusinessSubProfiles($tax->getId(), 1);
$dtax = json_decode($tax);
$dtax->sub = empty($subprofile) ? 0 : 1;
$tax = json_encode($dtax);
}
请注意,您必须将foreach
更改为foreach ($all_taxes as &$tax)
,以便您可以更改循环中数组元素的值。
使用$subprofile
的随机值,这将作为输出:
Array
(
[0] => {"butapid":"50","name":"Output CST 2% ( with C form)","description":"","value":"2.00000000","butapsid":"1","butaptid":"1","created_uid":"1","updated_uid":"1","created_ts":"2017-03-31 11:36:52","updated_ts":"2018-01-12 20:10:08","butapatid":"1","butapappid":"1","alid":"199","gbutapid":"2","gbusupid":"6","butaprid":"1","tax_type":"2","company_id":"1","global_business_sub_profile":null,"businessTaxProfileType":null,"businessTaxProfileAmountType":null,"global_business_tax_profile":null,"sub":1}
[1] => {"butapid":"51","name":"Output Vat 5.5%","description":"f","value":"5.50000000","butapsid":"1","butaptid":"1","created_uid":"1","updated_uid":"1","created_ts":"2017-04-06 18:55:04","updated_ts":"2018-01-13 16:18:13","butapatid":"1","butapappid":"1","alid":"139","gbutapid":"1","gbusupid":"4","butaprid":"1","tax_type":"1","company_id":"1","global_business_sub_profile":null,"businessTaxProfileType":null,"businessTaxProfileAmountType":null,"global_business_tax_profile":null,"sub":0}
[2] => {"butapid":"57","name":"Output Service Tax 15%","description":"","value":"15.00000000","butapsid":"1","butaptid":"1","created_uid":"1","updated_uid":"1","created_ts":"2017-04-12 13:46:29","updated_ts":"2018-03-29 18:29:56","butapatid":"1","butapappid":"1","alid":"177","gbutapid":"3","gbusupid":"8","butaprid":"3","tax_type":"1","company_id":"1","global_business_sub_profile":null,"businessTaxProfileType":null,"businessTaxProfileAmountType":null,"global_business_tax_profile":null,"sub":1}
)