1

I have two arrays like this

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

Array
(
    [0] => Array
        (
            [1] => Test1
            [2] => Location1
            [3] => Email1
            [4] => Name1
            [5] => Address1
            [6] => Age1
            [7] => Gender1
            [8] => Phone1
            [9] => Website1
            [10] => Cell1
        )

    [1] => Array
        (
            [1] => Test2
            [2] => Location2
            [3] => Email2
            [4] => Name2
            [5] => Address2
            [6] => Age2
            [7] => Gender2
            [8] => Phone2
            [9] => Website2
            [10] => Cell2
        )

    [2] => Array
        (
            [1] => Test3
            [2] => Location3
            [3] => Email3
            [4] => Name3
            [5] => Address3
            [6] => Age3
            [7] => Gender3
            [8] => Phone3
            [9] => Website3
            [10] => Cell3
        )
)

Now i have to select 1,3,4 and 5 index value from each second array. How can i do this without two loops. I know i will have to use one but i don't want to use two loops

Output Required

Array
(
    [0] => Array
        (
            [1] => Test1
            [3] => Email1
            [4] => Name1
            [5] => Address1

        )

    [1] => Array
        (
            [1] => Test2
            [3] => Email2
            [4] => Name2
            [5] => Address2
        )

    [2] => Array
        (
            [1] => Test3
            [3] => Email3
            [4] => Name3
            [5] => Address3
        )
)
6
  • without loops! I think this can't be done :( Commented Feb 25, 2013 at 20:40
  • 1
    Why do you not want to use more than one loop? Commented Feb 25, 2013 at 20:40
  • using two loops is common. I want to know if there is any php built in function or logic so that i can achieve it Commented Feb 25, 2013 at 20:42
  • Using two loops isn't a bad thing. It won't make it any slower than one loop and some hackish code. On the upside, code readability will be better. Commented Feb 25, 2013 at 20:47
  • 1
    array_map in the rescue. A powerful tool which people often forget. Commented Feb 25, 2013 at 20:52

5 Answers 5

4

Using one loop, array_flip, and array_intersect_key, you can do it like this:

$array_one = array(1, 3, 4, 5);

$array_two = array(
    array(1 => 'Test1', 'Location1', 'Email1', 'Name1', 'Address1', 'Age1', 'Gender1', 'Phone1', 'Website1', 'Cell1'),
    array(1 => 'Test2', 'Location2', 'Email2', 'Name2', 'Address2', 'Age2', 'Gender2', 'Phone2', 'Website2', 'Cell2'),
    array(1 => 'Test3', 'Location3', 'Email3', 'Name3', 'Address3', 'Age3', 'Gender3', 'Phone3', 'Website3', 'Cell3')
); 

$array_one_flip = array_flip($array_one);
foreach($array_two as $k => $v) {
    $result[] = array_intersect_key($v, $array_one_flip);
}

print_r($result);

The result would be:

Array
(
    [0] => Array
        (
            [1] => Test1
            [3] => Email1
            [4] => Name1
            [5] => Address1
        )

    [1] => Array
        (
            [1] => Test2
            [3] => Email2
            [4] => Name2
            [5] => Address2
        )

    [2] => Array
        (
            [1] => Test3
            [3] => Email3
            [4] => Name3
            [5] => Address3
        )

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

3 Comments

array_intersect_key uses the key value's and doesn't achieve OP's desired result.
Yes i have tested it is not working according to my requirement as you can see it is always selecting first 4 columns
Yeah, I overlooked that as well. I've modified my answer to address that issue.
3

You could try it with array_intersect_key:

$keys = array_flip($keys_array);    // flip the keys array for matching with array_intersect_key
$result = array();
foreach ($content_array as $arr)
{
  $result[] = array_intersect_key($arr, $keys);
}

Working example on codepad. Thanks @Michael Irigoyen for the arrays...

3 Comments

it is not selecting the desired result. Please test it
@raheel shan Have you flipped your keys array?
@raheel shan It does work, see my edit / the example on codepad.
1

Just use an array_map function like this:

$keys_to_keep = array(1, 3, 4, 5);
$key_diff_array = array_fill_keys($keys_to_keep, 'not used');

$array; // your main array you want to filter assume value has been set elsewhere

$filtered_array = array_map(function ($value) use $key_diff_array {
    return array_intersect_key($value, $key_diff_array);
}, $array);

1 Comment

I don't see $array in array_map?
0

using one loop:

<?php
 $selection = array(1, 3, 4, 5);
 for($i = 0; $i < count($array); $i++){

 reset($selection);
 $first = current($selection);
 $newarray[$i][$first] = $array[$i][$first];

 $next = next($selection);
 $newarray[$i][$next] = $array[$i][$next];

 $next = next($selection);
 $newarray[$i][$next] = $array[$i][$next];

 $next = next($selection);
 $newarray[$i][$next] = $array[$i][$next];
}
echo '<pre>';
print_r($newarray);

1 Comment

Very inefficient way of doing it.
0

You might also find this Note about array_flip useful.

Ensure that $array1 has values that can be accepted as array keys when flipped.

http://php.net/manual/en/language.types.array.php

The key can either be an integer or a string. The value can be of any type.

Additionally the following key casts will occur:

Strings containing valid integers will be cast to the integer type. E.g. the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer. Floats are also cast to integers, which means that the fractional part will be truncated. E.g. the key 8.7 will actually be stored under 8. Bools are cast to integers, too, i.e. the key true will actually be stored under 1 and the key false under 0. Null will be cast to the empty string, i.e. the key null will actually be stored under "". Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type. If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.

http://www.php.net/manual/en/function.array-flip.php

array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys.

Note that the values of trans need to be valid keys, i.e. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be included in the result.

If a value has several occurrences, the latest key will be used as its value, and all others will be lost.

Comments

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.