sort merged object values in codeigniter [duplicate] - php

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 2 months ago.
I have three object values to merge like
$videos = $this->Blogmodel>all_video_posts_ByCategory($cat_id);
$texts = $this->Blogmodel->all_text_posts_ByCategory($cat_id);
$audio = $this->Blogmodel->all_audio_posts_ByCategory($cat_id);
$data['array'] = array_merge($videos, $texts, $audio);
But the problem was values are sorted by one by one .. How to sort by date while merging ??

You can first merge the arrays and then sort the final array.
You are probably looking for a multi-sort function. See below code:
$data = array_qsort2($data['array'], 'posted_date', "ASC");
function array_qsort2 (&$array, $column=0, $order="ASC") {
$oper = ($order == "ASC")?">":"<";
if(!is_array($array)) return;
usort($array, create_function('$a,$b',"return (\$a['$column'] $oper \$b['$column']);"));
return ($array);
}

Related

Get an array column based on the values of another column in PHP [duplicate]

This question already has answers here:
How to filter an array by a condition
(9 answers)
get specific column with specific condition from multidimensional array using array function only in php
(4 answers)
Closed 3 years ago.
I have a multi-column PHP array as
Array (
Array ('col1', 'col2', 'col3'),
);
How can I get an array of col2 if col1 is greater than a specific value?
I can do it by a foreach loop, but I think it should be possible with a native function like array_filter.
Simply put, you're better with a foreach loop, as you're not going to have to make multiple iterations of the array. You want to both filter and modify the array, which requires using both array_filter() and array_map() in order to get what you want:
<?php
$arr = [
[5, 'col2-1', 'col3'],
[10, 'col2-2', 'col3']
];
$res = array_filter($arr, function($item) {
return $item[0] > 7;
});
$res = array_map(function($item) {
return $item[1];
}, $res);
var_dump($res);
Test here: https://3v4l.org/HRTOJ

Grab value from complex array and output as simple associative array [duplicate]

This question already has answers here:
How to use array values as keys without loops? [duplicate]
(6 answers)
Closed 6 years ago.
Please check attached images. These image contains $complex_arr print and $simple_arr print results. I want to convert $complex_arr to $simple_arr output.
How is that possible? What is actually doing each of $complex_arr inside value will be converted as associative simple array like $simple_arr
$arr1 = array("asso1"=>"a", "asso2"=>"1");
$arr2 = array("asso1"=>"b", "asso2"=>"2");
$arr3 = array("asso1"=>"c", "asso2"=>"3");
$complex_arr = array($arr1,$arr2,$arr3);
$simple_arr = array("a"=>"1", "b"=>"2", "c"=>"3");
// print_r($complex_arr);
print_r($simple_arr);
Input:
Output:
You have to write it on our own... here is my idea:
public function makeItSimpler($arr){
$newarr = array();
foreach($arr as $complex){
$newarr[$complex['asso1']]=$complex['asso2'];
}
return $newarr;
}
Perhaps you can do it better... take look at "array-map" http://php.net/manual/de/function.array-map.php
Good Luck
foreach ($complex_arr as $key => $value) {
$simple_arr[$value['asso1']]=$simple_arr[$value['asso2']];
}
With php5.5 (where array_column becomes available) it is:
$simple_arr = array_combine(
array_column($complex_arr, 'asso1'),
array_column($complex_arr, 'asso2')
);
And even simplier (after I reread function manual):
$simple_arr = array_column($complex_arr, 'asso2', 'asso1');

Sort multidimensional array in PHP [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
I have a multidimensional array, fetched from an SQL query. It looks like this:
$CIKK[$j]["vonalkod"]
$CIKK[$j]["kiadva"]
$CIKK[$j]["nev"]
$CIKK[$j]["hatarido"]
Can I sort $CIKK based on the value of its ["vonalkod"]?
you use this function
function sort_multi($array, $column, $method) {
foreach ($array as $key => $row) {
$narray[$key] = $row[$column];
}
array_multisort($narray, $method, $array);
return $array;
}
$myarray = sort_multi($CIKK , 'vonalkod' , SORT_DESC)
i'm using array_multisort in http://php.net/manual/en/function.array-multisort.php
the $method will take SORT_DESC or SORT_ASC

PHP - multidimensional array from arrays [duplicate]

This question already has answers here:
Transposing multidimensional arrays in PHP
(12 answers)
Closed 1 year ago.
I've got 6 arrays - 1 with name and 5 with some properties - which should be assigned to that name. All values are of course in order. I'd like to make a 2-dimensional array with will be later put into CSV and the result should be as on the table here:
I guess that i have to do 2 loops here, but I can't make them work. How to construct such array?
Solution found
I've connected all arrays:
$final_array = array($nazwa_array,$new_ilosc_array,$new_koszt_array,$new_cena_lifo_array,$new_cena_fifo_array,$new_rodzaj_array);
I've found a matrix transposition function, which returns array in correct order:
function transpose($array) {
array_unshift($array, null);
return call_user_func_array('array_map', $array);
}
$a = array();
foreach ( $names AS $key => $value ) {
$a[$key]['name'] = $value;
$a[$key]['property1'] = $value.'->'.$property1_array[$key];
$a[$key]['property2'] = $value.'->'.$property2_array[$key];
$a[$key]['property3'] = $value.'->'.$property3_array[$key];
$a[$key]['property4'] = $value.'->'.$property4_array[$key];
$a[$key]['property5'] = $value.'->'.$property5_array[$key];
}

How can I use PHP to sort a two dimensional array? [duplicate]

This question already has answers here:
How do I sort a multi-dimensional array by value?
(3 answers)
PHP: sorting a multidimentional array ($arr[$i]['v'])
(1 answer)
Closed 9 years ago.
I want to sort the array below by 'name'. I have tried several things but I can't figure it out. Any suggestions?
$data = array();
$data[] = array('name'=>'Bill','phone'=>'555-5555');
$data[] = array('name'=>'Joe','phone'=>'555-5554');
...
You can use usort() to sort an array using a custom criteria.
For instance:
function my_sort_by_name($a, $b) {
return strcmp($a['name'], $b['name']);
}
$data = array();
$data[] = array('name'=>'Bill','phone'=>'555-5555');
$data[] = array('name'=>'Joe','phone'=>'555-5554');
usort($data, 'my_sort_by_name');

Categories