Вопрос

Overview of what's gonna happen, For the sake of the example, I provided Email Address, First Name, and Last Name. And each of columns has a check box if the user wants to include it on import or not.

enter image description here

In this example, Last Name wasn't supposed to be included. So when I pass submit, the data of checkbox is being output like this.

Array
(
    [0] => Email Address
    [1] => First Name
)

Which is correct, then what I want to happen is, to remove the Last Name on the original data (multidimensional array), Unfortunately, array_diff() doesn't work, or else I may have did something wrong.

I have these 2 arrays

$mapping_import_value = $_SESSION['mapping_import_value'];
$arr_import_column = $_POST['import_column'];

Mapping Import Value: A multi dimensional array

Array
(
    [Email Address] => Array
        (
            [0] => email11@gmail.com
            [1] => email12@gmail.com
            [2] => email13@gmail.com
        )

    [First Name] => Array
        (
            [0] => Guy 11
            [1] => Guy 12
            [2] => Guy 13
        )

    [Last Name] => Array
        (
            [0] => Stand 11
            [1] => Stand 12
            [2] => Stand 13
        )

)

Then

Arr Import Column: A single array

Array
(
    [0] => Email Address
    [1] => First Name
)

So it will be like, 2 arrays will match, if something is not matched (Last Name) it will be removed including it's child. So any help would be nice. :D

Это было полезно?

Решение

You can use array_interesect_key(), and array_flip() on $arr_import_column:

$x = array_intersect_key($mapping_import_value, array_flip($arr_import_column));

This will basically return all of the entries in $mapping_import_value whose keys are present in $arr_import_column

Другие советы

Use another variable and iterate like below.

$mapping_import_value_another = array();

foreach($arr_import_column as $v)
{
    $mapping_import_value_another[$v] = $mapping_import_value[$v];
}

So $mapping_import_value_another will have only selected columns data.

I think you have to build a function to check this:

function areArraysEqual($arrImportColumn, $mappingImportValue)
{
  foreach($mappingImportValue as $key=>$values)
  {
    if (!in_array($arrImportColumn,$key))
    {
       unset($mappingImportValue[$key]);
    }
  }
}

This function will remove all keys from $mappingImportValue that are not contained in $arrImportColumn

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top