How to compare 2 array and display them in one table

php compare two arrays for matches
how to compare elements in an array java
php compare two arrays for not matches
how to compare array elements in c
compare two arrays in javascript
php compare two arrays for equality
compare two arrays in typescript
php array_intersect multidimensional

I have 2 arrays, one array contains staff info while the second one contains staff sales.

For staff info array it contain id and name while staff sales array contain staff id and total sales.

I tried to display those 2 array but when i tried only 2 data is display.

While i needed the id without the sales to be display also.

My Code :

<?php
$employees = [
    $i = [

        "id" => "1",
        "name" => "Aliff",
    ],
    $p = [

        "id" => "2",
        "name" => "Aina",
    ],
    $a = [

        "id" => "3",
        "name" => "Ana",
    ]
];
$employee_sales = [
    $i = [
        "id" => "1",
        "amount" => "500",
    ],
    $p = [
        "id" => "3",
        "amount" => "300",
    ],
];
?>
<!-- Next Table --->
<?php
// echo '<pre>';
// var_dump($employees);
// var_dump($employee_sales);
foreach ($employees as $employee) {
    foreach ($employee_sales as $employee_sale) {
        if ($employee['id'] == $employee_sale['id']) {
        ?>
        <table>
            <tr>
                <td><?= $employee['id']; ?></td>
                <td><?= $employee['name']; ?></td>
                <td><?= $employee_sale['amount']; ?></td>
            <tr>
        </table>
        <?php
        }
    }
}
?>
<!-- Next Table --->
<br>
<br>
<br>
<?php
foreach ($employees as $employee) {
?>
    <table>
        <tr>
            <td><?= $employee['id']; ?></td>
            <td><?= $employee['name']; ?></td>
        <tr>
    </table>
<?php } ?>
<!-- Next Table --->
<br>
<br>
<br>
<?php
foreach ($employee_sales as $employee_sale) {
?>
    <table>
        <tr>
            <td><?= $employee_sale['id']; ?></td>
            <td><?= $employee_sale['amount']; ?></td>
        <tr>
    </table>
<?php } ?>

I want staff id number 2 to be display also even though that staff didnt have any total sales.

Want to display the amount = 0 because the staff didnt have any sales.

Add a variable that tells whether any sales were printed for the employee. If not, print a row with empty sales.

foreach ($employees as $employee) {
    $sales_found = false;
    foreach ($employee_sales as $employee_sale) {
        if ($employee['id'] == $employee_sale['id']) {
        $sales_found = true;
?>
    <table>
    <tr>
    <td><?= $employee['id']; ?></td>
    <td><?= $employee['name']; ?></td>
    <td><?= $employee_sale['amount']; ?></td>
    <tr>
    </table>

<?php } 
    }
    if (!$sales_found) { ?>
        <table>
        <tr>
        <td><?= $employee['id']; ?></td>
        <td><?= $employee['name']; ?></td>
        <td>0</td>
        <tr>
        </table>

    <?php 
    } 
} ?>

How to compare two arrays and find the count of match elements , How to compare two arrays and find the count of match elements be increamented when ever match found in $arr2 so that it can be saved into another table. item/element is found i.e for shop=3 and for mobile and software it should be 1. A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays.equals() to compare two arrays. Actually, there is a list of equals() methods in Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is base of all classes in Java).

First of all you can reindex your arrays to be much easier to access data by employee id. Also, you can display all data in a single table:

<table>
     <tr>
          <td>ID</td>
          <td>Name</td>
          <td>Amount</td>
     </tr>

<?php
    $employees = array_combine(array_column($employees, 'id'), $employees);
    $employee_sales = array_combine(array_column($employee_sales, 'id'), $employee_sales);

    foreach ($employees as $employeeId => $employee) {
?>
        <tr>
            <td><?= $employee['id']; ?></td>
            <td><?= $employee['name']; ?></td>
            <td><?= (isset($employee_sales[$employeeId]) ? $employee_sales[$employeeId]['amount'] : 0) ?></td>
        <tr>
<?php
    }
?>

</table>

compare two arrays Code Example, Error: It looks like you are passing several store enhancers to createStore(). This is not supported. Instead, compose them together to a single  In this program we will read two one dimensional arrays of 5 elements and compare them. Here are the functions which we designed to read, print and compare arrays. readArray() printArray() compareArray() readArray() will read array of 5 elements. printArray() will print array of 5 elements.

<?php

$employees = [
    [
        "id" => "1",
        "name" => "Aliff",
    ],
    [
        "id" => "2",
        "name" => "Aina",
    ],
    [    
        "id" => "3",
        "name" => "Ana",
    ]
];

$employee_sales = [
    [
        "id" => "1",
        "amount" => "500",
    ],    
    [
        "id" => "3",
        "amount" => "300",
    ],        
];    

$id_sales = array_column($employee_sales, 'amount', 'id');
?>
<table>
    <thead>
        <tr>
            <th>id</th>
            <th>name</th>
            <th>sales</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($employees as $employee) { ?>
        <tr>
            <td><?= $employee['id']; ?></td>
            <td><?= $employee['name']; ?></td>
            <td><?=
            isset($id_sales[$employee['id']]) 
                ? $id_sales[$employee['id']] 
                : '0' ?></td>
        </tr>
    <?php } ?>
    </tbody>
</table>

array_intersect - Manual, array_intersect() returns an array containing all the values of array1 that are present in all the arguments. The above example will output: of arrays; array_diff_assoc() - Computes the difference of arrays with additional index check array(3) { [1]=> int(2) [3]=> int(4) [5]=> int(6) } This makes it important to remember which  Step 1: We need to declare the function with two parameters. function compare(arr1,arr2){} Step 2: We need to Initialize an empty array. function compare(arr1,arr2){const finalarray =[];} Step 3: We are taking the help of forEach method in Array.protoype.forEach().

You can use below loop in your code.

<?php
foreach ($employees as $employee) {

?>
    <table>
    <tr>
    <td><?php echo $employee['id']; ?></td>
    <td><?php echo $employee['name']; ?></td>
    <td>
    <?php
        $key = array_search($employee['id'], array_column($employee_sales, 'id'));
        echo ( $key !== false ) ? $employee_sales[$key]['amount'] : 0;
    ?>
    </td>
    <tr>
    </table>

<?php
} ?>

array_search is used to find the index of matching employee_id in employee_sales array, and that index is used to fetch amount. If it is not found we return 0

array_diff - Manual, This will output : Array ( [1] => blue ) If you need compare two arrays of integers or strings you can use such function: public static function When you will have an array inside your elements, it will throw a notice error. It will throw huge  If T is an m-byn table with variables that each have one column, then each variable becomes one column in A, and A is an m-by-n array. If T contains variables that consist of more than one column, those variables become multiple columns in A, and the size of A is greater than the size of T.

Please check this one.

<?php
 $employees = array(1=> array('id' => "1" , 'name' => 'Aliff')  ,2 => array('id' => "2" , 'name' => 'Aina'),3 => array('id' => "3" , 'name' => 'Ana')  );

  $employee_sales = array(1 => array('id' => "1" , 'amount' => '500')  ,2 => array('id' => "3" , 'amount' => '300'));

  $data = array();
 foreach ($employees as $key => $value) {

   $data[$value['id']] = $value['name'];

 }

   $data1 = array();
 foreach ($employee_sales as $key1 => $value1) {
    $data1[$value1['id']] = $value1['amount'];

 }



?>


    <table>
     <?php foreach ($data as $key => $value) {?>
     <?php if(isset($data1[$key])){?>
    <tr>
    <td><?= $key; ?></td>
    <td><?= $value; ?></td>
    <td><?= $data1[$key]; ?></td>
    <tr>
    <?php }else{?> 
     <tr>
    <td><?= $key; ?></td>
    <td><?= $value; ?></td>
    <td>0</td>
    <tr> 

    <?php } ?>

<?php } ?>


    </table>

How to compare two arrays in Java?, Java provides a direct method Arrays.equals() to compare two arrays. Output: Same. How does Arrays.deepEquals() work? It compares two objects using any  When you say “compare” – do you mean equal, lexicographic comparison, or diff? Sounds like you want a diff. The set solution might look like this (assuming you want things in one not in the other, rather than the disjunction of the two): let a = [1,2,3] let b = [1,2] Set(a).subtract(b) // returns a set of {3}

PHP array_intersect() Function, Compare the values of two arrays, and return the matches: Try it Yourself » This function compares the values of two or more arrays, and return an array that​  Optionally, the function takes other 2 arguments - index with the index of the elemnt in the list and array - a reference to the original array. If you are using them, you have to be careful to avoid modifying the array argument since this will affect the original array and cause unpleasant side effects.

Documentation: 12: 9.18. Array Functions and Operators, Table 9.51 shows the operators available for array types. Table 9.51. Array Operators overlap (have elements in common), ARRAY[1,4,3] && ARRAY[2,1], t. So today, we will learn how we can use Array Formulas to compare lists of values and calculate summaries. Towards the end of this post, you can see a video that explains the solution to Donation Summary Calculation problem. Comparing List of Values – Different Scenarios. There are 2 scenarios when we compare lists of values.

comparing two tables or datasets - MATLAB Answers, Learn more about dataset, table, comparison MATLAB. It is stated in the following for datasets only. '''If A and B are tables, then setdiff returns the rows from A that are not in B, with repetitions removed. however, beware that nan~=​nan and similarly for entries of categorical arrays which are undefined. dsA={1;​2;3};. You have two tables that have overlapping, redundant, or conflicting information, and you want to consolidate them into one table. For example, suppose that you have one table named Customers and another table named Clients. The tables are nearly identical, but one table or both tables contain some records that the other is missing.

Comments
  • Do you really want a separate table with just one row for each sale?
  • Can you be a bit clearer about the display result you expect? (edit the question itself is preferable)
  • Want to display the amount = 0 because the staff didnt have any sales : if(count($array) ==0) {echo("didnt have any items!");}else{foreach($array as $item){}}
  • the first table is the combine one, while the 2nd and 3rd is just a table of staff info and staff sales. I want to combine it into 1 table on the first table as shown
  • thanks, i will consider your answer. I see that variable $sales_found can be helpful. i will try to use it on my coding
  • You could swap the ternary for the null coalescing operator: <?= $id_sales[$employee['id']] ?? '0' ?>.