Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep only specific array keys/values in array?

I have a multidimensional array that I am searching trough for specific values. If those values are found I need to extract the indexes with those values ( make new array ) and remove all others.

array_intersect worked fine on php 5.3 , now on 5.4 it complains Notice: Array to string conversion.

I found that array_intersect has an issue with multidimensional array on 5.4. https://bugs.php.net/bug.php?id=60198

This is $options array I am searching trough

Array (

    [index1] => html
    [index2] => html
    [index3] => slide
    [index4] => tab
    [index5] => Array
        (
            [0] => 123
        )

)

code that works on php 5.3.x

$lookfor   = array('slide', 'tab');
$found     = array_intersect($options, $lookfor);


print_r($found);


Array
(
    [index3] => slide
    [index4] => tab
)

but in 5.4.x this trows the error mentioned above.

What would be another way to do this without a loop please. and without suppressing the error.

Thank you!

like image 902
Benn Avatar asked Apr 18 '14 16:04

Benn


People also ask

How do you filter an array key?

Filtering a PHP array by keys To use the PHP array_filter() function to filter array elements by key instead of value, you can pass the ARRAY_FILTER_USE_KEY flag as the third argument to the function. This would pass the key as the only argument to the provided callback function.

Can array key be an array?

This is not possible - array keys must be strings or integers.

What is Array_keys () used for?

The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.

What function is used to sort the values in array and keep the keys intact?

The ksort() function sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.


1 Answers

array_intersect() isn't recursive. The function assumes the array is just one level deep and expects all the array elements to be scalars. When it finds a non-scalar value, i.e. a sub-array, it throws a Notice.

This is vaguely mentioned in the documentation for array_intersect():

Note: Two elements are considered equal if and only if: (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

One solution I could think of is to use array_filter():

$lookfor = array('html', 'slide');
$found   = array_filter($options, function($item) use ($lookfor) {
    return in_array($item, $lookfor);
});

Note: This still performs a looping and isn't any better than a simple foreach. In fact, it might be slower than a foreach if the array is large. I have no idea why you're trying to avoid loops — I personally think it'd be more cleaner if you just use a loop.

Demo

Another solution I could think of is to remove the sub-arrays before using array_intersect():

<?php

$options = array(
    'index1' => 'html',
    'index2' => 'html',
    'index3' => 'slide',
    'index4' => 'tab',
    'index5' => array(123),
);

$lookfor = array('html', 'slide');
$scalars = array_filter($options,function ($item) { return !is_array($item); });
$found = array_intersect ($scalars, $lookfor);

print_r($found);

Demo

like image 122
Amal Murali Avatar answered Oct 18 '22 02:10

Amal Murali