0

I'm so confused with arrays. Can any one help me to solve this?? I have 4 arrays, these all are related.

My array structure is like:

Array 1:
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
Array 2:
Array ( [0] => 500 [1] => 500 [2] => 1 [3] => 2 [4] => 3 [5] => 3 )
Array 3:
Array ( [0] => 2 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )
Array 4:
Array ( [0] => 1 [1] => 2 [2] => 1 [3] => 1 [4] => 2 [5] => 1 )

I have to map all 1 value from Array 1 to another arrays.

3
  • What is the output structure? Commented Feb 24, 2018 at 5:18
  • i have fetch all unique values from array 1 and map to the coressponding positions of other arrays Commented Feb 24, 2018 at 5:22
  • first value 1 fetches corresponding 500,2,1 of other arrays ,like this all 1 of array 1 fetches its corresponding of others Commented Feb 24, 2018 at 5:28

1 Answer 1

1

If you want to map the values of 4 arrays to each position, you can:

$arr1 = array(1, 2, 1, 1, 2, 1 );
$arr2 = array(500, 500, 1, 2, 3, 3 );
$arr3 = array(2, 2, 1, 1, 2, 1 );
$arr4 = array(1, 2, 1, 1, 2, 1 );

$results = array_map(function($v1, $v2, $v3, $v4) {
    return array($v1, $v2, $v3, $v4);
}, $arr1, $arr2, $arr3, $arr4);

echo "<pre>";
print_r( $results );
echo "</pre>";

This will result to:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 500
            [2] => 2
            [3] => 1
        )

    [1] => Array
        (
            [0] => 2
            [1] => 500
            [2] => 2
            [3] => 2
        )

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

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

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

    [5] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 1
            [3] => 1
        )

)

Doc: http://php.net/manual/en/function.array-map.php

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.