多维数组使用php查找和更新特定索引中的值

时间:2016-02-18 06:14:41

标签: php arrays

我有一个名为$ data

的后续多维数组
$data =
Array
(
[0] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => 
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 1000
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 0
        [StartOfAllocation] => 1000
        [ApprovedActivities] => 0
        [AvailableBalance] => 1000
        [TotalApprovedClaims] => 0
        [Balance] => 1000
        [TotalUnApprovedClaims] => 0
        [Exposure] => 1000
    )

[1] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => 
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 0
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 50
        [StartOfAllocation] => 50
        [ApprovedActivities] => 0
        [AvailableBalance] => 50
        [TotalApprovedClaims] => 0
        [Balance] => 50
        [TotalUnApprovedClaims] => 0
        [Exposure] => 50
    )

[2] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => EMEA
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 0
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 25
        [StartOfAllocation] => 25
        [ApprovedActivities] => 0
        [AvailableBalance] => 25
        [TotalApprovedClaims] => 0
        [Balance] => 25
        [TotalUnApprovedClaims] => 0
        [Exposure] => 25
    )

[3] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => APJeC
        [SubRegion] => India
        [Country] => India
        [typeofrec] => 0
        [TotalAllocations] => 500
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 0
        [StartOfAllocation] => 500
        [ApprovedActivities] => 0
        [AvailableBalance] => 500
        [TotalApprovedClaims] => 0
        [Balance] => 500
        [TotalUnApprovedClaims] => 0
        [Exposure] => 500
    )

)

$amount =10;
$index = 2  // selected array index for change

以上信息我需要根据以下计算用新的[AvailableBalance]更新数组。

接下来我需要从数组$ index(索引2)

获取[AvailableBalance]
$existing_available = 25;

从$ existing_availablet

中减去$ amount

$ remaining_amount = 15; //其中($ existing_available - $ amount)

使用数组$ index(index2)更新[AvailableBalance] = 15($ remaining_amount)。

所以我的最终数组如下所示:

$data =
Array
(
[0] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => 
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 1000
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 0
        [StartOfAllocation] => 1000
        [ApprovedActivities] => 0
        [AvailableBalance] => 1000
        [TotalApprovedClaims] => 0
        [Balance] => 1000
        [TotalUnApprovedClaims] => 0
        [Exposure] => 1000
    )

[1] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => 
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 0
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 50
        [StartOfAllocation] => 50
        [ApprovedActivities] => 0
        [AvailableBalance] => 50
        [TotalApprovedClaims] => 0
        [Balance] => 50
        [TotalUnApprovedClaims] => 0
        [Exposure] => 50
    )

[2] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => EMEA
        [SubRegion] => 
        [Country] => 
        [typeofrec] => 0
        [TotalAllocations] => 0
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 25
        [StartOfAllocation] => 25
        [ApprovedActivities] => 0
        [AvailableBalance] => 15      // updated value 
        [TotalApprovedClaims] => 0
        [Balance] => 25
        [TotalUnApprovedClaims] => 0
        [Exposure] => 25
    )

[3] => Array
    (
        [AllocationPool] => TEST do not USE
        [Quarter] => 2016-Q4
        [Region] => APJeC
        [SubRegion] => India
        [Country] => India
        [typeofrec] => 0
        [TotalAllocations] => 500
        [TotalTransfersOut] => 0
        [TotalTransfersIn] => 0
        [StartOfAllocation] => 500
        [ApprovedActivities] => 0
        [AvailableBalance] => 500
        [TotalApprovedClaims] => 0
        [Balance] => 500
        [TotalUnApprovedClaims] => 0
        [Exposure] => 500
    )

)

请帮我尝试这个问题。

我是php和堆栈溢出的新手。

提前致谢..

3 个答案:

答案 0 :(得分:2)

有解决方案:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="Win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" publicKeyToken="6595b64144ccf1df" language="*" processorArchitecture="*"/>
    </dependentAssembly>
  </dependency>

  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
      <!-- Windows 10 -->
      <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
      <!-- Windows 8.1 -->
      <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
      <!-- Windows 8 -->
      <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
      <!-- Windows Vista -->
      <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
      <!-- Windows 7 -->
    </application>
  </compatibility>

</assembly>

工作示例(为简单起见,省略了其他字段):CLICK

答案 1 :(得分:0)

试试这个:

    create or replace procedure table_test as
  type v_table1_type is table of table1%rowtype;
  v_tab1_list v_table1_type;
  v_sql       varchar2(4000);
  v_order_sql varchar2(2000);
  v_order     varchar2(2000);
begin
  v_order_sql := 'SELECT listagg(column_name,'','') within group  (order by column_id)
      FROM ALL_TAB_COLUMNS
      WHERE TABLE_NAME = ''TABLE2''
      group by table_name';

  dbms_output.put_line(v_order_sql);
  execute immediate v_order_sql
    into v_order;

  v_sql := 'select *  from table2 order by ' || v_order;
  dbms_output.put_line(v_sql);
  execute immediate v_sql bulk collect
    into v_tab1_list;

  forall x in v_tab1_list.first .. v_tab1_list.last
    insert into table1 values v_tab1_list (x);
  commit;
exception
  when others then
    dbms_output.put_line(sqlerrm);
end;

演示:https://eval.in/520720

答案 2 :(得分:0)

如果计算数组索引和数量($index$amount),则将值直接更新为数组$data

$index = 2;
$amount = 10;     
$data[$index]['AvailableBalance'] = $data[$index]['AvailableBalance'] - $amount;