php通过数组

时间:2016-08-23 12:06:08

标签: php arrays

我有两个数组

$x = array(1 => 'test1', 2 => 'test', 3 => 'test3', 4 => 'test4');
$y = array(1,4);

我想知道如何通过$x(键数组)过滤$y。所以我想要的结果是(在过滤后)

$z = array(2 => 'test', 3 => 'test3');

这些数组很大。所以一个接一个地设置在这里效率不高。我想要任何数组函数来做到这一点。有可能??

5 个答案:

答案 0 :(得分:3)

试试这个:

Post

答案 1 :(得分:1)

您正在寻找array_diff_key

 private GestureDetectorCompat gestureDetector;

    // in your adapter constructor
    gestureDetector = new GestureDetector(context, new SingleTapConfirm());

    public View getView(final int position, View recycled, ViewGroup parent) {

        ViewHolder holder;
        if (recycled == null) {
            .....find items by id
        } else {
            holder = (ViewHolder) recycled.getTag();
        }

        // Set listener to item image
        holder.image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // Use lazy initialization for the gestureDetector
                gestureDetector.onTouchEvent(event);
                // At least the onTouch-callback gets called with the correct position
                return true;
            }
        });

        // Use glide library to load images into the image views
        Glide.with(mContext)....into(holder.image);
        return recycled;
    }

    private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            Log.e(TAG, "onSingleTapConfirmed"); // never called..
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.e(TAG, "onDoubleTap"); // never called..
            return super.onDoubleTap(e);
        }
    }

答案 2 :(得分:0)

只检查密钥是否存在,如果没有,则将其分配给新阵列。由于您需要在新数组中生成结果,因此可以这样做:

'===>>> [FINISHED] Building sources: bla                                 (01h:20m:30s)'

答案 3 :(得分:0)

试试这个:

$x = array(1 => 'test1', 2 => 'test', 3 => 'test3', 4 => 'test4');
$y = array(1,4);
$newArray = array();
foreach ($x as $key => $val)
{
    if(!in_array($key, $y))
        $newArray[$key] = $val;
}
print_r($newArray)

Check Demo Here

答案 4 :(得分:0)

要过滤掉数组元素,可以使用php函数array_filter,如此

$z = array_filter($x, function($element) use ($y) {
    return !in_array($element, $y);
}, ARRAY_FILTER_USE_KEY);

use ($y)使得该函数可以访问$yARRAY_FILTER_USE_KEY,因为我们希望按键(例如1)而不是值(例如" TEST1&#34)