PHP sort array using another array no keys - php

Hello i have an array in php
Array
(
[0] => red
[1] => blue
[2] => green
[3] => yellow
[4] => purple
[5] => white
)
and i want to sort it using that array
Array
(
[0] =>
[1] => 0
[2] => -1
[3] => -5
[4] => -5
[5] => 9
)
so i want the element with the greatest value on the second array to come first on the first array not its value from the second array but the element it self from the first array to move in the first position on the first array! The second bigger to the second place etc.. elements with the same value don't care me how they will be arranged!
the output i want to get is
Array
(
[0] => white
[1] => blue
[2] => green
[3] => yellow
[4] => purple
[5] => red
)

You can use array_multisort() :
$ar1 = array(/* your SO links */);
$ar2 = array(/* your numbers */);
array_multisort($ar2, SORT_DESC, $ar1);
Documentation here

Use array_multisort.
see http://www.php.net/manual/fr/function.array-multisort.php, follow the "Exemple #1 Trier plusieurs tableaux"
Cordially

Lets call your arrays are $dataArray, and $sortArray respectively
asort($sortArray);
foreach ( $sortArray as $key=>$val ) {
$newArray[] = $dataArray[$key];
}
If you need it reversed, just add in a call to array_reverse() at the end.

I think what the OP wants is to sort the first array by the second array's values.
IE- the second array is the vote count for the first array's web pages.
This could be solved more simply if in the first array you have each element as an array with two elements in it, webpage & vote count
$pages = Array
(
array(
'page' => 'http://stackoverflow.com/questions/640805/open-source-ios-components-reusable-views-controllers-buttons-table-cells-e',
'count' => 0
)
array(
'page' => 'http://stackoverflow.com/questions/3889634/fast-and-lean-pdf-viewer-for-iphone-ipad-ios-tips-and-hints',
'count' => -1
)
// etc for the rest...
)
But since the question asked how to do it in the current data structure:
$sorted_values = asort($pages);
$new_pages = array();
$sorted_keys = array_keys($sorted_values);
foreach($sorted_keys as $page_key)
array_push($new_pages, $pages[$page_key]);

Related

count the title character in php while grouping array results in alphabetical order?

I have a php code as shown below.
php code:
$beta_lists = array_flip($flip);
print_r($beta_lists); // Line A
foreach ($beta_lists as $title => $permalink) {
$title_char = substr(transliterator_transliterate('Any-Latin;Latin-ASCII;', $title), 0, 1);
}
Line A prints the following arrary:
Array
(
[Apple] => http://www.abc.mno/apple/
[Ball] => http://www.abc.mno/ball/
[Builders] => http://www.abc.mno/builders/
[Bowling] => http://www.abc.mno/bowling/
[Correct] => http://www.abc.mno/correct/
[Campaign] => http://www.abc.mno/compain/
[Direct] => http://www.abc.mno/direct/
[Degree] => http://www.abc.mno/degree/
)
What I am trying to achieve through the php code above is I want to count (which is 4 in the following case) the title character while grouping array results in alphabetical order. I will be grouping array results in alphabetical order later.
A C
Apple Correct
B Compaingn
Ball D
Builders Direct
Bowling Degree
Problem Statement:
I am wondering what changes I should make in the php code above so that it counts the total number of title characters (which is 4) used while grouping array results in alphabetical order.
One option would be to sort the titles into a new array, grouping on the first character of the title. If you ksort the array first, that sorting will be maintained in the new array. Then to get the number of title characters you can simply count the result array. This structure also has the benefit of making it easy to count titles beginning with e.g. 'C' using count($groups['C']).
ksort($beta_lists);
$groups = array();
foreach ($beta_lists as $title => $value) {
$groups[$title[0]][$title] = $value;
}
print_r($groups);
echo count($groups);
Output:
Array
(
[A] => Array
(
[Apple] => http://www.abc.mno/apple/
)
[B] => Array
(
[Ball] => http://www.abc.mno/ball/
[Bowling] => http://www.abc.mno/bowling/
[Builders] => http://www.abc.mno/builders/
)
[C] => Array
(
[Campaign] => http://www.abc.mno/compain/
[Correct] => http://www.abc.mno/correct/
)
[D] => Array
(
[Degree] => http://www.abc.mno/degree/
[Direct] => http://www.abc.mno/direct/
)
)
4
Demo on 3v4l.org

Moving array element to beginning of array?

If I have an array like the one below how would I go about moving key [2] and its associated value to the beginning of the array ? (making it key [0] and increasing the other keys by 1)
Current:
[0] => Array
(
[name] => Vanilla Coke cans 355ml x 24
)
[1] => Array
(
[name] => Big Red Soda x 24
)
[2] => Array
(
[name] => Reeses White PB Cups - 24 CT
)
Desired outcome:
[0] => Array
(
[name] => Reeses White PB Cups - 24 CT
)
[1] => Array
(
[name] => Vanilla Coke cans 355ml x 24
)
[2] => Array
(
[name] => Big Red Soda x 24
)
EDIT
To clarify I do will always want to move an element to the begining of the array but it wont necessarily be the last element it can sometimes be the 3rd 4th e.c.t. it varies each time.
array_splice removes (and optionally replaces / inserts) values from an array returning an array with the removed items. In conjunction with the simple array_unshift function the job could be done.
$arr = [1,2,3,4,5];
function array_move_item_as_first(array $array, int $idx) : array
{
array_unshift( $array, array_splice($array, $idx, 1)[0] );
return $array;
}
print_r(array_move_item_as_first($arr, 2));
output:
Array
(
[0] => 3
[1] => 1
[2] => 2
[3] => 4
[4] => 5
)
Why don't you use array_unshift and array_pop together?
array_unshift($someArray, array_pop($someArray));
array_pop removes the last element, and array_shift prepends the entry to the array.

Handling PHP Array post values and merging - PHP Arrays

I know this won't take much time for experts here. But still please help me out
My Array output is like this
Array ( [0] => 1 [1] => 37 [2] => 1035 ) 1
Array ( [0] => 1 [1] => 37 [2] => 1035 ) mystatusmessage1
Array ( [0] => 4 [1] => 37 [2] => 2925 ) 2
Array ( [0] => 4 [1] => 37 [2] => 2925 ) mystatusmessage2
What I would like to get it is in a single string value like this so that I can insert into database.
1,37,1035,1,mystatusmessage1
4,37,2925,2,mystatusmessage2
How can I achieve that. I'm trying to do with foreach but still I'm not able to do it.
Thanks,
Kimz
use implode function to make string from array for example
if you have array like Array('a','b','c');
implode(',',array('a','b','c') )
will return a,b,c as string
here first argument is your glue by which you want to join string
Here you go.
// Original array
$array = array(0 => 1, 1 => 37, 2 => 1035);
// $_POST array
$_POST = array(1,'mystatusmessage1');
// Jump to the end of array
end($array);
// Merge the post with original array
$newArr = array_merge($array,$_POST);
// Impode
echo implode(",",$newArr);
Repeat with other array.

Sort array based on value ascending or descending php

my array is
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
)
start value is = 3
Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 0
[5] => 1
[6] => 2
)
any idea friends..
try
$arr = array(0,1,2,3,4,5,6);
foreach($arr as $k=>$v) {
if($v >= 3)
$a[] = $v;
else
$b[] = $v;
}
$c = array_merge($a, $b);
print_r($c); //Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 0 [5] => 1 [6] => 2 )
Step 0 (optional if needed):
order the array (heap or some other sort algorithm)
Step 1:
create an output array
Step 2:
loop trough the array and find your start point move the value and the ones after to new array and set the value in the original array to null
Step 3:
loop trough the array and move the remaining values over
DONE
Just do this:
Call asort on the array.
Call array_search to find the index you want to slice.
Call array_slice to cut off the part you want.
Call array_merge to merge them in the right order.
I don't know PHP that well but a quick look at php.net and I suspect this will work.
Seeing how your array is already sorted and 0-indexed, you can just slice and reorganise it:
array_values(array_slice($arr, 3, null, true) + array_slice($arr, 0, 3, true));

How to sort an array with one exception in PHP

Let's say I have an array called $array that looks like this once I run asort on it:
Array
(
[1] => Apples
[2] => Bananas
[3] => Cherries
[4] => Donuts
[5] => Eclairs
[6] => Fried_Chicken
)
What is the simplest way to make it so that, after sorting alphabetically, the key that has the value "Donuts" is removed and then put at the end?
I would simply remove the donut element, perform your asort, and then add the donut item back on.
I came up with this. Tested it and confirmed it works. Reordered your array so I could actually see the sorting.
$arr = Array(
1 => "Fried_Chicken",
2 => "Donuts",
3 => "Bananas",
4 => "Apples",
5 => "Eclairs",
6 => "Cherries"
);
// Get donut and key
$donut_key = array_search("Donuts", $arr);
$donut = $arr[$donut_key]; // If you don't need to keep the value, skip this line
// Remove donut
unset($arr[$donut_key]);
// Sort
asort($arr);
// Append Donut
$arr += array($donut_key => $donut);
Array Search
http://php.net/manual/en/function.array-search.php
Key preserving append
http://www.vancelucas.com/blog/php-array_merge-preserving-numeric-keys/

Categories