Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset array keys in PHP [duplicate]

Tags:

arrays

php

I have two arrays and would like to find the first matching element and return the value without the key being preserved. I tried array_intersect but it preserves the key.

For example:

  $a = ['three', 'two', 'one'];
  $b = ['five', 'one'];

  $output = array_intersect($a, $b);

The resultant array returns [2] => ['one']

I just want this to be returned [0] => ['one'] Any workaround ?

like image 273
sn n Avatar asked Oct 28 '25 03:10

sn n


1 Answers

Just use array_values:

<?php
$a = ['three', 'two', 'one'];
$b = ['five', 'one'];

$output = array_values(array_intersect($a, $b));

print_r($output);

Example here

like image 79
Slava Rozhnev Avatar answered Oct 29 '25 18:10

Slava Rozhnev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!