如何通过唯一的ID组合对象的PHP数组?

时间:2018-11-16 13:34:05

标签: php arrays multidimensional-array

我有这两个数组。

第一个数组来自用户输入public class ADTService { AWSCredentials awsCredentials = null; AmazonDynamoDBClient amazonDynamoDBClient = null; public Reply sendADTMessage() throws HL7Exception { Reply reply = new Reply(); PatientInfo patientInfo = new PatientInfo(); byte[] valueDecoded= Base64.decode("TVNIfF5+XFwmfFNtYXJ0fDgwMDB8fEZ8MjAxMzA3MzExMzIyNTl8fEFEVF5BMDF8NDM0MzQzfFB8Mi44fHx8QUx8TkUKRVZOfFAwM3wyMDEzMDczMTEzMjI1OXx8T3x8MjAxMzA3MzExMzIyNTkKUElEfDF8UjQzNTQzNXxSNDM1NDM1fHxCQVRJU1RFXkFOVE9JTkVefHwxOTI1MDIyODAwMDAwMHxNfHx8fHx8fHx8fHw0MzgyNjEzMDcKUFYxfDF8SXxVLTAxXjQwN15BfHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHx8fHwyMDEwMTAwNTAwMDAwMA=="); String decodedADT = new String(valueDecoded); System.out.println("Decoded value is " + decodedADT); HapiContext context = new DefaultHapiContext(); context.setValidationContext(new NoValidation()); try { Parser p = context.getGenericParser(); Message hapiMsg = p.parse(decodedADT); System.out.println("Decoded value 1 is " + hapiMsg); ADT_A01 adtMsg = (ADT_A01)hapiMsg; MSH msh = adtMsg.getMSH(); PID pid = adtMsg.getPID(); EVN event = adtMsg.getEVN(); PV1 pv1 = adtMsg.getPV1(); System.out.println("MSH sending application "+msh.getSendingApplication().getNamespaceID().getValue()); System.out.println("MSH sending facility "+msh.getSendingFacility().getNamespaceID().getValue()); System.out.println("MSH receiving application "+msh.getReceivingApplication().getNamespaceID().getValue()); System.out.println("MSH receiving facility "+msh.getReceivingFacility().getNamespaceID().getValue()); System.out.println("MSH datetime of message "+msh.getDateTimeOfMessage().getValue()); System.out.println("MSH message control id "+msh.getMessageControlID().getValue()); System.out.println("MSH ADT Event "+msh.getMessageType().getMessageCode().getValue()+"-"+msh.getMessageType().getTriggerEvent().getValue()); System.out.println(); System.out.println("EVN recorded datetime "+event.getRecordedDateTime().getValue()); System.out.println(); System.out.println("PID patient id "+pid.getPatientIdentifierList(0).getIDNumber().getValue()); System.out.println("PID patient id "+pid.getPatientID().getValue()); System.out.println("PID last name "+pid.getPatientName(0).getFamilyName().getFn1_Surname().getValue()); System.out.println("PID first name "+pid.getPatientName(0).getGivenName().getValue()); System.out.println("PID dob "+pid.getDateTimeOfBirth().getValue()); System.out.println("PID gender "+pid.getAdministrativeSex().getIdentifier().getValue()); System.out.println(); System.out.println("PV1 admit datetime "+pv1.getAdmitDateTime().getValue()); System.out.println("PV1 bed status "+pv1.getBedStatus().getValueSetOID()); System.out.println("PV1 assigned patient location bed "+pv1.getAssignedPatientLocation().getBed().getNamespaceID().getValue()); System.out.println("PV1 assigned patient location room "+pv1.getAssignedPatientLocation().getRoom().getNamespaceID().getValue()); System.out.println("PV1 assigned patient location floor "+pv1.getAssignedPatientLocation().getFloor().getNamespaceID().getValue()); System.out.println("PV1 assigned patient location building "+pv1.getAssignedPatientLocation().getBuilding().getNamespaceID().getValue()); System.out.println("PV1 prior patient location bed "+pv1.getPriorPatientLocation().getBed().getNamespaceID().getValue()); System.out.println("PV1 prior patient location room "+pv1.getPriorPatientLocation().getRoom().getNamespaceID().getValue()); System.out.println("PV1 prior patient location fllor "+pv1.getPriorPatientLocation().getFloor().getNamespaceID().getValue()); System.out.println("PV1 prior patient location building "+pv1.getPriorPatientLocation().getBuilding().getNamespaceID().getValue()); System.out.println("PV1 discharge datetime "+pv1.getDischargeDateTime().getValue()); patientInfo.setSendingApplication(msh.getSendingApplication().getNamespaceID().getValue()); patientInfo.setSendingFacility(msh.getSendingFacility().getNamespaceID().getValue()); patientInfo.setPatientId(pid.getPatientIdentifierList(0).getIDNumber().getValue()); patientInfo.setGivenName(pid.getPatientName(0).getGivenName().getValue()); patientInfo.setAdministrativeSex(pid.getAdministrativeSex().getIdentifier().getValue()); } catch (Exception e) { e.getStackTrace(); } finally{ try { context.close(); } catch (IOException e) { context = null; e.printStackTrace(); } } patientInfo.setPatientDetails(decodedADT); ADTService adtService = new ADTService(); adtService.savePatientDetails(patientInfo); //System.out.println(ADTMessage.Contents); reply = SubProcess(); return reply; }

$cart

第二个数组,我做一个数据库array(3) { [0]=> array(3) { ["id"]=>"3" ["weight"]=>"20" ["percentage"]=>"80" } [1]=> array(3) { ["id"]=>"1" ["weight"]=>"50" ["percentage"]=>"80" } [2]=> array(3) { ["id"]=>"2" ["weight"]=>"40" ["percentage"]=>"80" } } ,得到SELECT id, stock WHERE id IN (3,1,2)

$db_item

我想将第二个数组中的stock属性添加到第一个数组中。 这是我尝试过的方法,并且有效,但是我认为没有必要array(3) { [0]=> array(2) { ["id"]=>"1" ["stock"]=>"9539.00" } [1]=> array(2) { ["id"]=>"2" ["stock"]=>"9468.00" } [2]=> array(2) { ["id"]=>"3" ["stock"]=>"9295.00" } } foreacharray_filter

array_column

任何人都有更好的主意如何优化它?


编辑:在Mohammad's answer之后,我可以在第二个数组中使用更多属性

foreach ($cart as $key => $cart_item) {
    $item = array_filter($db_item, function($item) use ($cart_item) {
        return $item['id'] === $cart_item['id'];
    });
    $cart[$key]['stock'] = array_column($item, 'stock')[0];
}

EDIT2 :我们可以使用$keys = []; foreach ($arr2 as $item) { $keys[$item['id']] = array( 'attr1' => $item['attr1'], 'attr2' => $item['attr2'], // and so on ); } $newArr = array_map(function($item) use($keys){ $item['attr1'] = $keys[$item['id']]['attr1']; $item['attr2'] = $keys[$item['id']]['attr2']; // and so on return $item; }, $arr1); 仅用一行就可以简化foreach循环。

array_column

2 个答案:

答案 0 :(得分:2)

使用array_flip()array_column()的组合来创建包含id和第二个数组的索引的数组。

然后使用array_map()向第一个数组添加新键stock

$keys = array_flip(array_column($arr2, 'id'));
$newArr = array_map(function($item) use($keys, $arr2){ 
    $item['stock'] = $arr2[$keys[$item['id']]]['stock'];
    return $item;
}, $arr1);

demo中查看结果

您还可以使用foreach代替array_flip()

$keys = [];
foreach ($arr2 as $item)
    $keys[$item['id']] = $item['stock'];

$newArr = array_map(function($item) use($keys){ 
    $item['stock'] = $keys[$item['id']];
    return $item;
}, $arr1);

demo中查看结果

答案 1 :(得分:1)

这也有帮助,一个array_column +一个array_map:

$arr2=array_column($arr2,'stock','id');
$arr1=array_map(function($val)use($arr2){$val['stock']=$arr2[$val['id']];return $val;},$arr1);