English 中文(简体)
Merge a 3D Array with 2D Array based on the Common Values
原标题:Merge a 3D Array with 2D Array Based on Shared Values
  • 时间:2011-10-22 01:23:48
  •  标签:
  • php
  • arrays

我需要把三维阵列与两维阵组合合并起来,其基础是共同的补贴。

在下文中,“乔治·华盛顿”有1名助理。 我需要评估他的价值,这在第二个阵列中,也有1个。

“约翰·阿达姆斯”为2,这在第二阵列中没有。 因此,必须将他的价值定为0(或NUL)。

最终结果是三维阵列,其价值已加到原阵列的每一项目。

www.un.org/Depts/DGACM/index_spanish.htm Array #1

Array
(
     [0] => Array
         (
             [0] => Array
                 (
                     [id] => 1
                     [name] => "George Washington"
                 )
             [total] => 8
             [average] => 2.5
         )
     [1] => Array
         (
             [0] => Array
                 (
                     [id] => 2
                     [name] => "John Adams"
                 )
             [total] => 6
             [average] => 3.0
         )
     [2] => Array
         (
             [0] => Array
                 (
                     [id] => 5
                     [name] => "James Monroe"
                 )
             [total] => 9
             [average] => 2.0
         )
)

<>12>

Array
(
     [0] => Array
         (
             [id] => 1
             [value] => 12
         )
     [1] => Array
         (
             [id] => 5
             [value] => 18
         )
)

<<>Desired Result:

Array
(
     [0] => Array
         (
             [0] => Array
                 (
                     [id] => 1
                     [name] => "George Washington"
                 )
             [total] => 8
             [average] => 2.5
             [value] => 12
         )
     [1] => Array
         (
             [0] => Array
                 (
                     [id] => 2
                     [name] => "John Adams"
                 )
             [total] => 6
             [average] => 3.0
             [value] => 0
         )
     [2] => Array
         (
             [0] => Array
                 (
                     [id] => 5
                     [name] => "James Monroe"
                 )
             [total] => 9
             [average] => 2.0
             [value] => 18
         )
 )

www.un.org/Depts/DGACM/index_spanish.htm 我迄今所尝试的内容:。

我把所有补贴价值从头几个阵列分离成一个新阵列,称为ids。 然后,在通过第二阵列的物品时,我检查一下,第二阵列的补贴是否出现在女兵阵列中。

But then I m stuck because I don t know how to specify which item in the first array needs to receive the new value . Also, this seems like a messy solution because of the overhead involved in creating the $ids array.

最佳回答

甚至在我撰写这封信之前,你就试图把身份证列入名单,但在此,你却走了路。 <>Good luck!>

$array1_size = count($array1);
$array2_size = count($array2);

// Creates a list containing all available IDs of Array1
for ($i = 0; $i < $array1_size; $i ++) {
    $id_list[] = $array1[$i][0][ id ];
    $array1[$i][ value ] = NULL; // Adds a NULL to all value fields
}

// Loops through Array2
for ($i = 0; $i < $array2_size; $i++) {
    if (in_array($array2[$i][ id ], $id_list)) { // Checks if each ID exists in the ID list

        $key = array_search($array2[$i][ id ], $id_list); // Gets the key of the matching ID
        $array1[$key][ value ] = $array2[$i][ value ]; // Adds the value

    }
}

Edit: 现在,对全国扫盲运动的数值按拖欠计算,因此,如果需要,以后才会发生变化。

问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...