English 中文(简体)
购买力平价比较了两种阵列和产出,在不同的记录中均为零。
原标题:PHP compare two arrays and output all with zero at different record

我有两个阵列:

Array1:

ID: 1
ID: 2
ID: 3
ID: 4
ID: 5

Array2, with num Value:

ID: 2, NUM: 200
ID: 4, NUM: 400

我希望产出如下:(如果阵列没有记录,就再增加零)。

ID: 1, NUM: 0
ID: 2, NUM: 200
ID: 3, NUM: 0
ID: 4, NUM: 400
ID: 5, NUM: 0

I am new to PHP, tried array_diff and array_intersect but not find the clue, could you please let me know how can I do that?

感谢。

问题回答

You can loop through your array1 and add the value of array2 for the current key or 0 if the key is not set in array2.

foreach ($array1 as $key => $value) {
  if (array_key_exists($key, $array2) {
    $array1[$key] = $array2[$key];
  } else {
    $array1[$key] = 0;
  }
}

为了避免对第二阵列进行低效的搜索,有人为便于即时获得价值而进行相关的勘测/勘测。 如果在调查中找不到身份证,则对新要素的违约程度为0。

守则:(民主党)

$array1 = [
    [ ID  => 1],
    [ ID  => 2],
    [ ID  => 3],
    [ ID  => 4],
    [ ID  => 5],
];
$array2 = [
    [ ID  => 2,  NUM  => 200],
    [ ID  => 4,  NUM  => 400],
];

$lookup = array_column($array2, null,  ID );
var_export(
    array_map(
        fn($row) => $row + ($lookup[$row[ ID ]] ?? [ NUM  => 0]),
        $array1
    )
);




相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签