Sorting Multi Dimensional Array with PHP [duplicate] - php

This question already has answers here:
Sort multidimensional array by multiple columns
(8 answers)
Closed 4 years ago.
I have below array with me:
Array(
[0] => Array(
['sort'] => 1
['ques'] => 'Zing order'
)
[1] => Array(
['sort'] => 1
['ques'] => 'How stackoverflow works?'
)
[2] => Array(
['sort'] => 2
['ques'] => 'What is PHP'
)
)
What I want is:
Array(
[0] => Array(
['sort'] => 1
['ques'] => 'How stackoverflow works?'
)
[1] => Array(
['sort'] => 1
['ques'] => 'Zing order'
)
[2] => Array(
['sort'] => 2
['ques'] => 'What is PHP'
)
)
Ideally, it should sort first with sort key and then alphabetically with ques key.

You can use array_multisort() with the help of array_column(). So you have the power to set Sort Order now :)
<?php
$array = array(
array(
'sort' => 1,
'ques' => 'Zing order'
),
array(
'sort' => 1,
'ques' => 'How stackoverflow works?'
),
array(
'sort' => 2,
'ques' => 'What is PHP'
)
);
array_multisort(array_column($array, 'sort'), SORT_ASC,
array_column($array, 'ques'), SORT_ASC,
$array);
print_r($array);
?>
DEMO: https://3v4l.org/ofeZG

Related

Reorder multidimensional arrays manually by array key name

I have this multidimensional PHP array:
array (
0 =>
array (
'name_lower' => 'apples',
'name' => 'Apples',
),
1 =>
array (
'name_lower' => 'pears',
'name' => 'Pears',
),
2 =>
array (
'name_lower' => 'avocados',
'name' => 'Avocados',
),
3 =>
array (
'name_lower' => 'bananas',
'name' => 'Bananas',
),
)
What I'm trying to manually reorder the arrays inside the multidimensional array and list them in exact the following order:
array (
0 =>
array (
'name_lower' => 'bananas',
'name' => 'Bananas',
),
1 =>
array (
'name_lower' => 'avocados',
'name' => 'Avocados',
),
2 =>
array (
'name_lower' => 'pears',
'name' => 'Pears',
),
3 =>
array (
'name_lower' => 'apples',
'name' => 'Apples',
),
)
It does not follow a pattern to automatically sort the arrays. It needs to be rearranged manually by name. Any ideas?
If you index the array on something unique and set an array with the sort order with those unique values, then you can map the sort order array and extract from the main array:
$sort = array('bananas', 'avocados', 'pears', 'apples');
$array = array_column($array, null, 'name_lower');
$array = array_map(function($v) use($array) { return $array[$v]; }, $sort);

How to get random array from multidimensional array in php?

I am trying to get random array:
for example:
$navItems= array(
array(
slug => "top10.php",
title => "Top 10 geriausi"
),
array(
slug => "index.php",
title => "Pagrindinis"
),
array(
slug => "top-prasciausi.php",
title => "Top 10 prasciausi"
),
array(
slug => "visi-politikai.php",
title => "Visi politiki"
),
);
and after that use this random array inside foreach loop like this:
foreach ($navItems as $item) {
echo "<li class=\"hov\">$item[title]<li>";
}
Instead of $navItems variable i want to use that random arrays from $navItems array
You can use shuffle to randomize the array
Note: If you are trying to pick one from the array, you can use shuffle and select the first element of the array.
$navItems = array(
array(
'slug' => "top10.php",
'title' => "Top 10 geriausi"
),
array(
'slug' => "index.php",
'title' => "Pagrindinis"
),
array(
'slug' => "top-prasciausi.php",
'title' => "Top 10 prasciausi"
),
array(
'slug' => "visi-politikai.php",
'title' => "Visi politiki"
),
);
shuffle( $navItems );
echo "<pre>";
print_r( $navItems );
echo "</pre>";
This will result to something like:
Array
(
[0] => Array
(
[slug] => index.php
[title] => Pagrindinis
)
[1] => Array
(
[slug] => visi-politikai.php
[title] => Visi politiki
)
[2] => Array
(
[slug] => top10.php
[title] => Top 10 geriausi
)
[3] => Array
(
[slug] => top-prasciausi.php
[title] => Top 10 prasciausi
)
)
Doc: shuffle

Combining multidimensional array to single array [duplicate]

This question already has answers here:
Is there a function to extract a 'column' from an array in PHP?
(15 answers)
Closed 6 years ago.
I have the following array structure
array (
0 =>
array (
'ID' => '1',
'post_title' => 'Hello world!',
),
1 =>
array (
'ID' => '79',
'post_title' => 'ffffffffffff',
),
2 =>
array (
'ID' => '1720',
'post_title' => 'Git primer',
),
)
I will love to convert it to a structure similar to the one below. Is there any php function that can do this? I am trying to avoid repetitive foreach loop.
array (
'1' => 'Hello world!',
'79' => 'ffffffffffff',
'1720' => 'Git primer',
)
Use array_column()to get this.
Array_column() function return all column name you have specify in parameter.
$array=array (
0 =>
array (
'ID' => '1',
'post_title' => 'Hello world!',
),
1 =>
array (
'ID' => '79',
'post_title' => 'ffffffffffff',
),
2 =>
array (
'ID' => '1720',
'post_title' => 'Git primer',
),
)
$new_array = array_column($array, 'post_title', 'ID');
print_r($new_array);
Output:
Array
(
[1] => Hello world!
[79] => ffffffffffff
[1720] => Git primer
)
Here it is:
//Your array
$test = array (
0 =>
array (
'ID' => '1',
'post_title' => 'Hello world!',
),
1 =>
array (
'ID' => '79',
'post_title' => 'ffffffffffff',
),
2 =>
array (
'ID' => '1720',
'post_title' => 'Git primer',
),
);
//Solution:
foreach ($test as $t){
$new_array[$t['ID']] = $t['post_title'];
}
echo "<pre>";
echo print_r($new_array);
die;
You can accomplish this using following
array_column($array,'post_title','ID');
Output
Array
(
[1] => Hello world!
[79] => ffffffffffff
[1720] => Git primer
)

How to sort an assocoative array in php Laravel [duplicate]

This question already has answers here:
Sort an array of associative arrays by column value
(23 answers)
How to Sort a Multi-dimensional Array by Value
(16 answers)
Closed 6 years ago.
I want to sort this array based on count in descending order. here is my array
array(
46 =>
array (
'name' => 'HSR Layout',
'url' => 'hsr-layout',
'count' => 2,
),
37 =>
array (
'name' => 'Electronic City',
'url' => 'electronic-city',
'count' => 3,
)
)
If you are using Laravel, which your tag suggests, you can use collections to manipulate arrays like this. For example:
$array = collect($array)->sortBy('count')->reverse()->toArray();
Using array_multisort().
$array = array(
46 =>
array (
'name' => 'HSR Layout',
'url' => 'hsr-layout',
'count' => 2,
),
37 =>
array (
'name' => 'Electronic City',
'url' => 'electronic-city',
'count' => 3,
)
);
$price = array();
foreach ($array as $key => $row)
{
$count[$key] = $row['count'];
}
array_multisort($count, SORT_DESC, $array);
print_r($array);
Program Output
Array
(
[0] => Array
(
[name] => Electronic City
[url] => electronic-city
[count] => 3
)
[1] => Array
(
[name] => HSR Layout
[url] => hsr-layout
[count] => 2
)
)
Live demo : Click Here

php: how to get indexed array of key values [duplicate]

This question already has answers here:
PHP Store Key Value from Associative Array into Simple Array
(5 answers)
Closed 8 years ago.
Assuming I have an array like
$arr = array (
array( 'foo' => 'Lorem' ),
array( 'foo' => 'ipsum' ),
array( 'foo' => 'dolor' ),
array( 'foo' => 'sit' )
);
How can I quickly convert this array into an indexed array for the key "foo"? So that the result is
Array
(
[0] => 'Lorem'
[1] => 'ipsum'
[2] => 'dolor'
[3] => 'sit'
)
Are there any quick ways with PHP functions? Or do I simply have to create a new array, iterate over the other one and insert the values manually.
You could make use of array_column which is available from PHP 5.5
print_r(array_column($arr,'foo'));
The code...
<?php
$arr = array (
array( 'foo' => 'Lorem' ),
array( 'foo' => 'ipsum' ),
array( 'foo' => 'dolor' ),
array( 'foo' => 'sit' )
);
print_r(array_column($arr,'foo'));
Demo
You can use array_map(). This works -
$new_arr = array_map(function($v){return $v['foo'];}, $arr);
var_dump($new_arr);
// OUTPUT
array
0 => string 'Lorem' (length=5)
1 => string 'ipsum' (length=5)
2 => string 'dolor' (length=5)
3 => string 'sit' (length=3)
.. Or using array_map(..):
<?php
$arr = array (
array( 'foo' => 'Lorem' ),
array( 'foo' => 'ipsum' ),
array( 'foo' => 'dolor' ),
array( 'foo' => 'sit' )
);
print_r(array_map(function($x){return $x["foo"];}, $arr));
Output:
Array
(
[0] => Lorem
[1] => ipsum
[2] => dolor
[3] => sit
)

Categories