比较两个数组(库存)

时间:2016-11-16 21:44:56

标签: php arrays

对于汽车公司(仅举例)我需要自动化库存:

  

$ purchase_array =数组("兰博基尼" =>" 5","法拉利" =>" 4",&# 34;布加迪" =>" 3","麦克拉伦" =>" 2","菲亚特" =>& #34; 10"," Mazda" =>" 20");

     

$ sales_array =数组("兰博基尼" =>" 1","法拉利" =>" 2",&# 34;布加迪"   => " 3&#34);

我希望得到这个数组的结果:

  

$ stock_array =数组("兰博基尼" =>" 4","法拉利" =>" 2",&# 34;布加迪"   => " 0","麦克拉伦" => " 2","菲亚特" => " 10","马自达" => " 20&#34);

首先我找了普通车:

$common_cars = array_keys(array_intersect_key($purchase_array, $sales_array));
foreach ($common_cars as $common_car) {
.....
}

买我无法完成它。

任何帮助将不胜感激。 提前致谢

1 个答案:

答案 0 :(得分:1)

你可以做类似的事情。如果要保留purchase_array,可以将数组复制到另一个var。

N.B。如果您可以检索/存储数量作为整数,您可以确定当您循环这些项目时,您将不会获得意外结果

<asp:SqlDataSource ID="SqlDataSourceVisitors" runat="server" ConnectionString="connstring" DeleteCommand="DELETE FROM [RegisteredVisitors] WHERE [PK] = @PK; UPDATE [dbo].[Users] SET [NumberOfVisitors] = (SELECT [NumberOfVisitors] FROM [dbo].[NumberOfVisitors] WHERE [ResidentName] = @ResidentName) -1 WHERE [ResidentName] = @ResidentName" InsertCommand="INSERT INTO [RegisteredVisitors] ([ResidentName], [ResidentAddress], [VisitorCode], [VisitorName]) VALUES (@ResidentName, @ResidentAddress, @VisitorCode, @VisitorName)" SelectCommand="SELECT * FROM [RegisteredVisitors] WHERE ([ResidentName] = @ResidentName)" UpdateCommand="UPDATE [RegisteredVisitors] SET [ResidentName] = @ResidentName, [ResidentAddress] = @ResidentAddress, [VisitorCode] = @VisitorCode, [VisitorName] = @VisitorName WHERE [PK] = @PK" ProviderName="System.Data.SqlClient">
    <DeleteParameters>
        <asp:Parameter Name="PK" Type="Int32" />
        <asp:Parameter Name="ResidentName" Type="String" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="ResidentName" Type="String" />
        <asp:Parameter Name="ResidentAddress" Type="String" />
        <asp:Parameter Name="VisitorCode" Type="Int32" />
        <asp:Parameter Name="VisitorName" Type="String" />
    </InsertParameters>
    <SelectParameters>
        <asp:ControlParameter ControlID="txtboxSelectedResident" Name="ResidentName" PropertyName="Text" Type="String" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="ResidentName" Type="String" />
        <asp:Parameter Name="ResidentAddress" Type="String" />
        <asp:Parameter Name="VisitorCode" Type="Int32" />
        <asp:Parameter Name="VisitorName" Type="String" />
        <asp:Parameter Name="PK" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>