answersLogoWhite

0


Best Answer

To merge and sort an array in PHP you need to use the array_merge() and sort() functions like shown in the example below:

<?php

$array1 = array(1, 5, 3, 9, 7);

$array2 = array(8, 2, 6, 4, 0);

// merge the arrays

$merge = array_merge($array1, $array2); // 1, 5, 3, 9, 7, 8, 2, 6, 4, 0

// sort the array

sort($merge); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

?>

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you merge and sort an array using PHP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Sorting an array in PHP without using sort function?

plz as soon as possible give me the program for shorting an array in asscending order without using any sort function in c++


How do you sort variable in ascending or descending order both in php?

Assuming that the values are stored in an array, you can use the php function sort($array) to sort ascending, and rsort to sort descending. The following link gives a table that lists all of the built in PHP sort functions: http://php.net/manual/en/array.sorting.php


How do you merge arrays in PHP?

To merge arrays in PHP all you have to do is use the array_merge() function like shown below: &lt;?php $array1 = array("Matt", "Michael", "Justin"); $array2 = array("Janice", "Janet", "Kylie"); $array3 = array_merge($array1, $array2); ?&gt; One thing to remember when merging arrays is you might be creating duplicate results, if you don't want 2 or more of the same result in the array remember to use the function array_unique() to remove the duplicate results from it!


How do you count the number of elements in an array using PHP?

Using the function "count". &lt;?php $foo = array("John", "Jacob", "Jingleheimer", "Schmidt"); echo count($foo); // &lt;-- outputs the number 4 ?&gt;


What is the difference between sort and asort in PHP?

sort() will order the array by its values without preserving the keys. Use it when array is indexed numerically or when you do not care about the keys. asort() will also sort the array by its values, but it will preserve the key -&gt; value association.


How do you empty an array in PHP?

To empty an array in PHP you need to use the unset function like shown below: &lt;?php $array = array('hello', 'hi', 'weee'); unset($array); // empty ?&gt;


How do you separate word in array using php?

To separate a sentence into an array of words, you can use PHP's explode() function. If $text is your sentence, then use the following: $result = explode(" ", $text);


How do you count the vowels in a string using PHP?

build an array of vowels then do a foreach on the array and then explode the string on the array value and the answer is -1 of the result


How do you flip an array without using flip function in PHP?

Use a for-loop starting at the length of the array and go backwards and build up a new array with the values.


What does array combine function do in php?

Creates an array using one value for keys and other for its values http://php.net/manual/en/function.array-combine.php


How can you mix up the order of values in a PHP array?

To shuffle an array in PHP is easy to do, all you need to use is the shuffle() function like shown below: &lt;?php $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); shuffle($array); // array results will be randomly shuffled ?&gt;


How do you find the size of an array in PHP?

To find the size of an array in PHP you can either use the count() function or the sizeof() function as they will produce the same result. &lt;?php $array = array(1, 2, 3, 4, 5, 6, 7); echo count($array); // outputs 7 echo sizeof($array); // outputs 7 ?&gt;