How to join arrays in ascending order with Unique array

Asked

Viewed 82 times

1

Good is the following I have the following code:

$cars = array("Volvo", "BMW", "Toyota", "BMW", "Ferrari");
$cars2 = array_unique($cars);

echo $cars2[0];
echo "<br><br>";
echo $cars2[1];
echo "<br><br>";
echo $cars2[2];
echo "<br><br>";
echo $cars2[3];

Me showing the $cars2[3], show me a mistake, but in theory I wanted you to show me the Ferrari, or how can I join the arrays, excluding the repeated, and arrays have always worked.

How can I do that?

1 answer

2


$cars  = array("Volvo", "BMW", "Toyota", "BMW", "Ferrari");
$cars2 = array_unique($cars);
$cars2 = array_values($cars2);

print_r($cars2);
  • I think that’s it, but the order gets all changed? How can I do it in a way that gets the right order?

  • I edited, look now.

  • Perfect! Thank you so much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.