skip to Main Content

I’m trying to compare two PHP arrays with different information.

First array:

Array
(
    [B1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
        )

    [B2] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
        )

    [B3] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
        )

    [B4] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
            [8] => 9
        )

)

Second array:

Array
(
    [1] => B4
    [2] => B3
    [3] => B1
)

Now I want this result:

Array
(
    [B1] => Array
        (
            [0] => ok
            [1] => ok
            [2] => ok
            [3] => ok
            [4] => ok
            [5] => ok
            [6] => ok
            [7] => ok
        )

    [B2] => Array
        (
            [0] => null
            [1] => null
            [2] => null
            [3] => null
        )

    [B3] => Array
        (
            [0] => ok
            [1] => ok
            [2] => ok
            [3] => ok
            [4] => ok
            [5] => ok
            [6] => ok
            [7] => ok
        )

    [B4] => Array
        (
            [0] => ok
            [1] => ok
            [2] => ok
            [3] => ok
            [4] => ok
            [5] => ok
            [6] => ok
            [7] => ok
            [8] => ok
        )

)

I’ve tried various PHP functions, but they didn’t return expected result.

I’ve tried various PHP functions such as in_array and stristr to get the differences, but they returns different results and empty arrays.

Hope someone can help me, and thanks.

4

Answers


  1. Solution:

    <?php
    
    //fill the first array
    $a = [
        "B1" => [1, 2, 3, 4, 5, 6, 7, 8],
        "B2" => [1, 2, 3, 4],
        "B3" => [1, 2, 3, 4, 5, 6, 7, 8],
        "B4" => [1, 2, 3, 4, 5, 6, 7, 8, 9],
    ];
    
    //fill the second array
    $b = ["B1", "B3", "B4"];
    
    //loop through first array
    foreach ($a as $aKey => $val) {
        //count the length of arrays
        $arrLength = count($a[$aKey]);
    
        //find the key from second array
        $found = false;
        foreach ($b as $bKey) {
            if ($aKey == $bKey) {
                $found = true;
                break;
            }
        }
    
        //loop through inner arrays and
        //set the null value or 'ok' text for every element
        for ($i = 0; $i < $arrLength; $i++) {
            if ($found) {
                $a[$aKey][$i] = "ok";
            } else {
                $a[$aKey][$i] = null;
            }
        }
    }
    
    print_r($a);
    ?>
    

    The result is:

    Array
    (
        [B1] => Array
            (
                [0] => ok
                [1] => ok
                [2] => ok
                [3] => ok
                [4] => ok
                [5] => ok
                [6] => ok
                [7] => ok
            )
    
        [B2] => Array
            (
                [0] => 
                [1] => 
                [2] => 
                [3] => 
            )
    
        [B3] => Array
            (
                [0] => ok
                [1] => ok
                [2] => ok
                [3] => ok
                [4] => ok
                [5] => ok
                [6] => ok
                [7] => ok
            )
    
        [B4] => Array
            (
                [0] => ok
                [1] => ok
                [2] => ok
                [3] => ok
                [4] => ok
                [5] => ok
                [6] => ok
                [7] => ok
                [8] => ok
            )
    
    )
    

    Definition:
    This solution stores the result inside the first array, that means data from the first array will be lost. If you want to keep the data you can check this answer to copy data to another array:
    https://stackoverflow.com/a/17729234/10109156

    Or, you can create a new array to store the result:

    <?php
    
    //fill the first array
    $a = [
        "B1" => [1, 2, 3, 4, 5, 6, 7, 8],
        "B2" => [1, 2, 3, 4],
        "B3" => [1, 2, 3, 4, 5, 6, 7, 8],
        "B4" => [1, 2, 3, 4, 5, 6, 7, 8, 9],
    ];
    
    //fill the second array
    $b = ["B1", "B3", "B4"];
    
    $result = [];
    
    //loop through first array
    foreach ($a as $aKey => $val) {
        //count the length of arrays
        $arrLength = count($a[$aKey]);
    
        //find the key from second array
        $found = false;
        foreach ($b as $bKey) {
            if ($aKey == $bKey) {
                $found = true;
                break;
            }
        }
        
        //create an empty inner array
        $result[$aKey] = [];
    
        //loop through inner arrays and
        //set the null value or 'ok' text for every element
        for ($i = 0; $i < $arrLength; $i++) {
            if ($found) {
                $result[$aKey][$i] = "ok";
            } else {
                $result[$aKey][$i] = null;
            }
        }
    }
    
    print_r($result);
    ?>
    

    And result is same.

    Login or Signup to reply.
  2. <?php
    
    $array = array(
        "B1" => array(1, 2, 3, 4, 5, 6, 7, 8),
        "B2" => array(1, 2, 3, 4),
        "B3" => array(1, 2, 3, 4, 5, 6, 7, 8),
        "B4" => array(1, 2, 3, 4, 5, 6, 7, 8, 9),
    );
    
    foreach ($array as $key => $subarray) {
        if (count($subarray) == 8) {
            $array[$key] = array_fill(0, 8, "ok");
        } else {
            $array[$key] = array_fill(0, count($subarray), null);
        }
    }
    
    print_r($array);
    

    trying with this code

    Login or Signup to reply.
  3. 
    $data = [
        "B1" => [1, 2, 3, 4, 5, 6, 7, 8],
        "B2" => [1, 2, 3, 4],
        "B3" => [1, 2, 3, 4, 5, 6, 7, 8],
        "B4" => [1, 2, 3, 4, 5, 6, 7, 8, 9],
    ];
    
    $keys = ["B1", "B3", "B4"];
    
    // Will contain the output
    $out = [];
    
    foreach( $data as $key => $values ){
    
        // Construct the output entry,
        // fill with okays if $key is present 
        // in the $keys array, or with
        // nulls otherwise
        $out[ $key ] = array_fill(
            0,
            count($values),
            in_array($key, $keys) ? 'ok' : null
        );
    };
    
    print_r($out);
    
    Login or Signup to reply.
  4. $arr1 = [
      'B1' => [ 1, 2, 3, 4, 5, 6, 7, 8 ],
      'B2' => [ 1, 2, 3, 4 ],
      'B3' => [ 1, 2, 3, 4, 5, 6, 7, 8 ],
      'B4' => [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
    ];
    
    $arr2 = [ 'B1', 'B3', 'B4' ];
    
    $result = array_map(
      static fn($list, $key) => array_fill(0, count($list), in_array($key, $arr2, true) ? 'ok' : null),
      $arr1,
      array_keys($arr1)
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search