📜  如何根据PHP的匹配值推送值?

📅  最后修改于: 2022-05-13 01:54:11.391000             🧑  作者: Mango

如何根据PHP的匹配值推送值?

在PHP,要在匹配时推送数组中的值,我们需要一个带有键值对的数组。包含键值对的数组称为关联数组。

方法:创建两个不同的关联数组,分别命名为array1array2 。然后将 array1 的值与 array2 的键进行比较,如果我们得到匹配,那么我们将推送一个静态键和值对,如下所示:

方案一:

 "1",
    "GFG2" => "2",
    "GFG3" => "3"
];
  
// Creating a multi-dimensional
// associative array
$array2 = [
    "1" => ["Subject" => "HTML"],
    "2" => ["Subject" => "CSS"],
    "3" => ["Subject" => "JavaScript"]
];
  
// Nested foreach loop to compare
// array1 and array2 elements
foreach( $array1 as $key1 => $value1 ) {
      
    foreach( $array2 as $key2 => $value2 ) {
          
        // Compare the key of array2 with
        // the value of array1
        if ($key2 == $value1) {
              
            // If a match is found then push
            // the element in array2 based
            // on the key
            $array2[$value1]["Organization"]
                        = "GeeksforGeeks";
        }
    }
}
  
// Display the array elements
print_r($array2);
  
?>
输出:
Array
(
    [1] => Array
        (
            [Subject] => HTML
            [Organization] => GeeksforGeeks
        )

    [2] => Array
        (
            [Subject] => CSS
            [Organization] => GeeksforGeeks
        )

    [3] => Array
        (
            [Subject] => JavaScript
            [Organization] => GeeksforGeeks
        )

)

在上面的程序中,该值被推送到现有数组中。如果您想在一个全新的数组中推送值,请参阅以下程序。
方案二:



 "1",
    "GFG2" => "2",
    "GFG3" => "3"
];
  
// Creating a multi-dimensional
// associative array
$array2 = [
    "1" => ["Subject" => "HTML"],
    "2" => ["Subject" => "CSS"],
    "3" => ["Subject" => "JavaScript"]
];
  
// Create an empty array
$array3 = [];
  
// Nested foreach loop to compare
// array1 and array2 elements
foreach( $array1 as $key1 => $value1 ) {
      
    foreach( $array2 as $key2 => $value2 ) {
          
        // Compare the key of array2 with
        // the value of array1
        if ($key2 == $value1) {
              
            // If a match is found then push
            // the array element into array3
            // based on the key
            $array3[$value1] = $array2[$value1];
        }
    }
}
  
// Display the array elements
print_r($array3);
  
?>
输出:
Array
(
    [1] => Array
        (
            [Subject] => HTML
        )

    [2] => Array
        (
            [Subject] => CSS
        )

    [3] => Array
        (
            [Subject] => JavaScript
        )

)

上面的程序会将值从 array2 推送到 array3,其中 array3 为空。

方案三:

 "IT",
    "dept2" => "CE",
    "dept3" => "CS",
    "dept4" => "EC"
];
  
// Create a multi-dimensional associative array
$student = [
    "IT" => ["total_students" => "60"],
    "CE" => ["total_students" => "65"],
    "CS" => ["total_students" => "62"]
];
  
// Nested foreach loop to compare
// $department and $student elements
foreach ($department as $dept => $d) {
    foreach ($student as $std => $s) {
          
        // Compare the key of $student array with
        // the value of $department array
        if ($std == $d) {
            $student[$d]["HOD"] = "XYZ";
        }
    }
}
  
// Display the array elements
print_r($student);
  
?>
输出:
Array
(
    [IT] => Array
        (
            [total_students] => 60
            [HOD] => XYZ
        )

    [CE] => Array
        (
            [total_students] => 65
            [HOD] => XYZ
        )

    [CS] => Array
        (
            [total_students] => 62
            [HOD] => XYZ
        )

)

在上面的程序中,student数组中的dept4没有匹配项,因此没有显示在输出部分。