1

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] => [email protected]
            [1] => [email protected]
            [2] => [email protected]
        )

    [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

2
  • It is hard to understand from your descr. Could you provide source data and desired result? Commented May 7, 2014 at 8:37
  • have you tried unset? Like, iterate over the keys of your mapping_import_value array using foreach(array_keys($mapping_import_value) as $key) { and check if that $key is in $arr_import_column using in_array. If it is not in_array use unset($mapping_import_value[$key]) Commented May 7, 2014 at 8:37

3 Answers 3

3

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

2

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.

1
  • Cleaner than my solution Commented May 7, 2014 at 8:41
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.