I have an array with files. And I want to sort them by their min value (I'll explain below).
This is an example of array:
$files = array(
array(
'name' => "DevTools/tools/files/design/js/custom/http_build_query.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "DevTools/tools/files/design/js/custom/Form.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "DevTools/tools/files/design/js/custom/VanillaJS.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/jquery.js"
),
array(
'name' => "resources/js/plugins/jquery-ui.js"
),
array(
'name' => "resources/js/plugins/popper.js"
),
array(
'name' => "resources/js/plugins/bootstrap-v4/index.js",
'range' => array(
'min' => 800
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/util.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/dropdown.js",
'range' => array(
'min' => 200
)
),
array(
'name' => "resources/js/plugins/bootstrap-v4/collapse.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/custom/global.js"
),
array(
'name' => "layouts/cms/.js/+0.js",
'range' => array(
'min' => 0
)
),
array(
'name' => "resources/js/plugins/module/delete.js"
),
array(
'name' => "resources/js/plugins/module/form.submit.js"
),
array(
'name' => "resources/js/plugins/module/piece.image.js"
),
array(
'name' => "resources/js/plugins/select2.js"
)
);
It's easy to understand. We have names for all files. And some of them have [range][min].
What I need to do is keeping their order, but sorting only files from same folder which have the [range][min], so we reorder these ones by [min].
My code
So I use this function:
usort($files, function (array $a, array $b) {
if (!isset($a['range']) || !isset($b['range'])
|| dirname($a['name']) != dirname($b['name'])
|| $a['range']['min'] == $b['range']['min']) {
return 0;
}
return $a['range']['min'] <=> $b['range']['min'];
});
Which does exactly what I said above. So only those ones are correcly reordered, as you can see here:
[6] => Array
(
[name] => resources/js/plugins/bootstrap-v4/util.js
[range] => Array
(
[min] => 0
)
)
[7] => Array
(
[name] => resources/js/plugins/bootstrap-v4/collapse.js
[range] => Array
(
[min] => 0
)
)
[8] => Array
(
[name] => resources/js/plugins/bootstrap-v4/dropdown.js
[range] => Array
(
[min] => 200
)
)
[9] => Array
(
[name] => resources/js/plugins/bootstrap-v4/index.js
[range] => Array
(
[min] => 800
)
)
Which is great.
The problem
Now I add in my $files array this one:
array(
'name' => "resources/js/plugins/module/piece.images.js"
)
Append it anywhere you want in $files, the usort will reorder entire array with no logic:
Array
(
[0] => Array
(
[name] => DevTools/tools/files/design/js/custom/http_build_query.js
[range] => Array
(
[min] => 0
)
)
[1] => Array
(
[name] => resources/js/plugins/bootstrap-v4/collapse.js
[range] => Array
(
[min] => 0
)
)
[2] => Array
(
[name] => resources/js/plugins/select2.js
)
[3] => Array
(
[name] => resources/js/plugins/module/piece.image.js
)
[4] => Array
(
[name] => resources/js/plugins/module/form.submit.js
)
[5] => Array
(
[name] => resources/js/plugins/module/delete.js
)
[6] => Array
(
[name] => layouts/cms/.js/+0.js
[range] => Array
(
[min] => 0
)
)
[7] => Array
(
[name] => resources/js/plugins/bootstrap-v4/util.js
[range] => Array
(
[min] => 0
)
)
[8] => Array
(
[name] => resources/js/custom/global.js
)
[9] => Array
(
[name] => resources/js/plugins/bootstrap-v4/dropdown.js
[range] => Array
(
[min] => 200
)
)
[10] => Array
(
[name] => DevTools/tools/files/design/js/custom/Form.js
[range] => Array
(
[min] => 0
)
)
[11] => Array
(
[name] => resources/js/plugins/bootstrap-v4/index.js
[range] => Array
(
[min] => 800
)
)
[12] => Array
(
[name] => resources/js/plugins/popper.js
)
[13] => Array
(
[name] => resources/js/plugins/jquery-ui.js
)
[14] => Array
(
[name] => resources/js/plugins/jquery.js
)
[15] => Array
(
[name] => DevTools/tools/files/design/js/custom/VanillaJS.js
[range] => Array
(
[min] => 0
)
)
[16] => Array
(
[name] => resources/js/plugins/module/piece.images.js
)
)
I have no idea why is that happening. If you can find out why, can you please see if there is a solution please?
If I didn't explain well, please ask in comments for clarification.
So, what I need to do is keeping their order, but sorting only files from same folder which have the [range][min], so we reorder these ones by [min].
Related
Array ( [0] => Array (
[date] => 01-06-2018
[nav] => 30.65100 )
[1] => Array (
[date] => 31-05-2018
[nav] => 30.84900 )
[2] => Array (
[date] => 30-05-2018
[nav] => 30.73200 )
[3] => Array (
[date] => 29-05-2018
[nav] => 30.81500 )
The above code is the Multi-array, we have added a common id like id_code = 0089 to every array in it without using any loops in PHP. Can anyone helps me and it is possible or not .....?
If you mean not manually loop through the array then yes, it is possible using array_walk:
$array = [
0 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
1 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
2 => [
"date" => "01-06-2018",
"nav" => "30.65100"],
3 => [
"date" =>"01-06-2018",
"nav" => "30.65100"]
];
array_walk($array, function(&$item1) {
$item1['id_code'] = "0089";
});
print_r($array);
Output:
Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[1] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[2] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
[3] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
[id_code] => 0089
)
)
Demo https://3v4l.org/lCGIO
An effective solution will be to use array_map function as:
$keyValue = 'some value';
$data = array_map(function($d) use ($keyValue){
return $d + ['keyName' => $keyValue];
}, $data);
I think this is the closer you can get, but it is probably not your expected result. The exact result you need cannot be done without using a loop as far as I know.
$t = array( 0 => array( 'date' => '01-06-2018', 'nav' => '30.65100' ), 1 => array( 'date' => '31-05-2018', 'nav' => '30.84900' ), 2 => array( 'date' => '30-05-2018', 'nav' => '30.73200' ), 3 => array( 'date' => '29-05-2018', 'nav' => '30.81500' ));
$tt = array( 0 => array( 'id' => '648'), 1 => array( 'id' => '332'), 2 => array( 'id' => '889'), 3 => array( 'id' => '285') );
$final = array_map(null, $t, $tt);
print_r($final);
The output will look like
Array
(
[0] => Array
(
[0] => Array
(
[date] => 01-06-2018
[nav] => 30.65100
)
[1] => Array
(
[id] => 648
)
)
[1] => Array
(
[0] => Array
(
[date] => 31-05-2018
[nav] => 30.84900
)
[1] => Array
(
[id] => 332
)
)
[2] => Array
(
[0] => Array
(
[date] => 30-05-2018
[nav] => 30.73200
)
[1] => Array
(
[id] => 889
)
)
[3] => Array
(
[0] => Array
(
[date] => 29-05-2018
[nav] => 30.81500
)
[1] => Array
(
[id] => 285
)
)
)
I would like remove all keys [Name] but the main problem is the number in the list key ([List1],[List2] etc.). The numbers at key [List] may be more but for example I gave only two.
I would like to change this because it is an old json file and in the new one it doesn't have a key, like a converter
Is there a way to go across the entire array and remove all [Name] keys?
Array(
[Values] => 1
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
[Name] => Nm1
)
)
[1]=> Array(
[Properties] => Array(
[Id] => 1
[Name] => Nm1
)
)
)
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
[Name] => Nm1
)
)
)
)
[List2] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
[Name] => 0
)
)
)
)
)
[List2] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
[Name] => Nm1
)
)
)
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
[Name] => Nm1
)
)
)
)
)
)
My goal is:
Array(
[Values] => 1
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
)
)
[1]=> Array(
[Properties] => Array(
[Id] => 1
)
)
)
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
)
)
)
)
[List2] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
)
)
)
)
)
[List2] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
)
)
)
[List1] => Array(
[Product1] => Array(
[0] => Array(
[Properties] => Array(
[Id] => 1
)
)
)
)
)
)
I tried:
$ProductCount= count($array['List1']['Product1']);
for($i = 0;$i<$ProductCount;$i++){
unset($array['List1']['Product1'][$i][Properties][Name]);
}
But I also have a key[List2] and can be [List3] etc.
You can do it like this:
function remove_key($array, $key)
{
foreach($array as $k => $v) {
if(is_array($v)) {
$array[$k] = remove_key($v, $key);
} elseif($k == $key) {
unset($array[$k]);
}
}
return $array;
}
$array = remove_key($array, 'Name');
$array is your multidimensional array and $key is the key name that you want to remove.
NOTE: If the key represents an array (not a value) this method will ignore it but I assume that this is what you need. If you want to remove arrays if the key matches you need to switch the conditions order in the foreach loop.
I have array of photo with filename and time. I want to find the photos which are taken in hour. For example, I have photos taken from 10.00, 10.15, 10.59. So when I search. I want these photo to be listed under 10.00.
I think it's something to do with in_array or array_search. But I still don't have an idea how.
Here is my array: $pixArr[]
Array
(
[0] => Array
(
[file] => IMG_7519.JPG
[time] => 13:02
)
[1] => Array
(
[file] => IMG_7518.JPG
[time] => 13:01
)
[2] => Array
(
[file] => IMG_7517.JPG
[time] => 13:00
)
[3] => Array
(
[file] => IMG_7516.JPG
[time] => 11:39
)
[4] => Array
(
[file] => IMG_7515.JPG
[time] => 11:39
)
[5] => Array
(
[file] => IMG_7514.JPG
[time] => 11:38
)
[6] => Array
(
[file] => IMG_7513.JPG
[time] => 11:29
)
[7] => Array
(
[file] => IMG_7512.JPG
[time] => 11:26
)
[8] => Array
(
[file] => IMG_7511.JPG
[time] => 11:26
)
)
I expect it coming out something like this:
10:00
listPhoto.php
<?
//now how to list files by hour???
?>
you may use array_filter As #u_mulder suggested:
<?php
$pixArr = array
(
0 => array
(
'file' => 'IMG_7519.JPG',
'time' => '13:02'
),
1 => array
(
'file' => 'IMG_7518.JPG',
'time' => '13:01'
),
2 => array
(
'file' => 'IMG_7517.JPG',
'time' => '13:00'
),
3 => array
(
'file' => 'IMG_7516.JPG',
'time' => '11:39'
),
4 => array
(
'file' => 'IMG_7515.JPG',
'time' => '11:39'
),
5 => array
(
'file' => 'IMG_7514.JPG',
'time' => '11:38'
),
6 => array
(
'file' => 'IMG_7513.JPG',
'time' => '11:29'
),
7 => array
(
'file' => 'IMG_7512.JPG',
'time' => '11:26'
),
8 => array
(
'file' => 'IMG_7511.JPG',
'time' => '11:26'
),
);
print_r(array_filter($pixArr, function($value){
if($value['time'] >= '11:00' && $value['time'] <= '12:00')
return $value;
}));
See Demo: https://eval.in/513466
you can using assort This function sorts an array such that array indices maintain their correlation with the array elements they are associated with.
I have been tried this problem.
$arr = [
[
"file" => "IMG_7519.JPG",
"time" => "13:02"
],
[
"file" => "IMG_7519.JPG",
"time" => "12:02"
],
[
"file" => "IMG_7519.JPG",
"time" => "15:02"
]
];
asort($arr); //sorting array
echo "<pre>".print_r($arr)."</pre>";
and this is the result.
Array ( [1] => Array ( [file] => IMG_7519.JPG [time] => 12:02 ) [0] => Array ( [file] => IMG_7519.JPG [time] => 13:02 ) [2] => Array ( [file] => IMG_7519.JPG [time] => 15:02 ) )
I've made a custom search form, a query using a custom post type with categories, custom taxonomy and meta fields and a search template for displaying them but cannot get the results to show. I'm using Wordpress. I've changed the way to make the query using pre_get_posts but have trouble now how to display the result. All variables are passed into query_vars.
Below is my code:
function my_get_posts( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if (is_home() || is_search() || is_archive() || is_page( 'search' ) )
{
$query->set('post_type', 'yacht');
$query->set('posts_per_page', 1000);
$query->set('s', '' );
$meta_query = array();
$meta_query[] = array(
array(
'key' => 'yachts_length',
'value' => array($_GET['min_length'], $_GET['max_length']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_price',
'value' => array($_GET['min_price'], $_GET['max_price']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_year',
'value' => array($_GET['min_year'], $_GET['max_year']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
)
);
$meta_query['relation'] = 'AND';
$query->set('meta_query',$meta_query);
}
return $query;
}
add_action( 'pre_get_posts', 'my_get_posts');
And the query_vars output using the form:
Array
(
[cat] => 0
[page] => 0
[pagename] => search
[manufacturer] => 0
[min_length] => 3
[max_length] => 100
[min_price] => 500
[max_price] => 999999
[min_year] => 1970
[max_year] => 2015
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] => search
[static] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[menu_order] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[orderby] => menu_order
[order] => ASC
[post_type] => yacht
[posts_per_page] => 1000
[meta_query] => Array
(
[0] => Array
(
[0] => Array
(
[key] => yachts_length
[value] => Array
(
[0] => 3
[1] => 100
)
[compare] => BETWEEN
[type] => NUMERIC
)
[1] => Array
(
[key] => yachts_price
[value] => Array
(
[0] => 500
[1] => 999999
)
[compare] => BETWEEN
[type] => NUMERIC
)
[2] => Array
(
[key] => yachts_year
[value] => Array
(
[0] => 1970
[1] => 2015
)
[compare] => BETWEEN
[type] => NUMERIC
)
)
[relation] => AND
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
)
I have to compare two 7-dimensional Arrays and for every match i have to count +1 to a new array.
This is the array with user-input, which i want to compare with the "right values"
Array([task] => Array(
[OF] => Array(
[nr] => Array(
[1] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[2] => Array(
[pos] => Array(
[0] => Array(
[value] => +
)
)
)
[3] => Array(
[pos] => Array(
[1] => Array(
[value] => -
)
[2] => Array(
[value] => +
)
)
)
[4] => Array(
[pos] => Array(
[0] => Array(
[value] => -
)
)
)
[5] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
[6] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[7] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
)
)
),
[TA] => Array(
[nr] => Array(
[1] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[2] => Array(
[pos] => Array(
[0] => Array(
[value] => +
)
)
)
[3] => Array(
[pos] => Array(
[1] => Array(
[value] => -
)
[2] => Array(
[value] => +
)
)
)
[4] => Array(
[pos] => Array(
[0] => Array(
[value] => -
)
)
)
[5] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
[6] => Array(
[pos] => Array(
[0] => Array(
[value] => :
)
)
)
[7] => Array(
[pos] => Array(
[0] => Array(
[value] => *
)
)
)
)
)
)
The right values are in the same structure with different values.
Now i have to loop through the arrays and check if the user-input is == the "right value".
Sorry if it's hard to understand. It's slightly hard to explain.
Any and all help is appreciated, thank you all in advance.
EDIT:// i want to know the amount of right values of the 'task' arrays e.g. OF => 12 , TA => 3
If I understood well, what you need is a recursive comparision. Here is my adhoc code, I didn't test it:
function compareArrays($array1, $array2) {
$retval = 0;
for($i = 0; $i < count($array1); $i++) {
// The values are arrays, we need a recursive comparation again
if (is_array($array1[$i]) && is_array($array2[$i])) {
$retval += compareArrays($array1[$i], $array2[$i]);
// The values are equal, we can increase the counter
} else if ($array1[$i] === $array2[$i]) {
$retval++;
// The values are different
} else {
// Is there anything to do?
}
}
return $retval;
}