I want the alphanumeric array below to return the max element depending on the value in the array which is h5-19: I tried using max($array), but that returns h5-9.
Array
(
[3] => h5-1
[4] => h5-2
[2] => h5-3
[1] => h5-4
[0] => h5-6
[5] => h5-7
[6] => h5-8
[7] => h5-9
[8] => h5-10
[9] => h5-11
[10] => h5-13
[11] => h5-15
[12] => h5-19
)
Thanks in advance.
You can use usort() sort array based on second digit of items. In sort function use explode() to get target digit.
$originArr = [
"h5-01",
"h6-1",
"h5-2",
"h5-3",
"h5-7",
"h5-9",
"h5-11",
"h5-15",
"h5-19"
];
// Copy array to keep order of origin array
$arr = $originArr;
usort($arr, function($a, $b){
return explode('-', $b)[1] - explode('-', $a)[1];
});
echo $arr[0];
// h5-19
Check result in demo
<?php
$set = array();
$data = array(
'3' => 'h5-1',
'4' => 'h5-2',
'2' => 'h5-3',
'1' => 'h5-4',
'0' => 'h5-6',
'5' => 'h5-7',
'6' => 'h5-8',
'7' => 'h5-9',
'8' => 'h5-10',
'9' => 'h5-11',
'10' => 'h5-13',
'11' => 'h5-15',
'12' => 'h5-19',
'789' => 'h1-8',
'123' => 'p-78',
'3000' => 'p-8',
);
foreach($data as $each_element){
$each_val = explode("-",$each_element);
if(!isset($set[$each_val[0]])){
$set[$each_val[0]] = intval($each_val[1]);
}
$set[$each_val[0]] = max(intval($each_val[1]),$set[$each_val[0]]);
}
print_r($set);
Output:
Array
(
[h5] => 19
[h1] => 8
[p] => 78
)
Algorithm:
Create a set of tags which will be an associative array where key will be the tag name and it's value will be the max value of that tag present in your array.
If the key is already set, get the max between the value in the key(tag) and the new value which also belongs to the same tag. This way you have max for each tag.
This sorts the data use rsort() and SORT_NATURAL and then echos the first item...
$data = Array
(
"3" => "h5-1",
"4" => "h5-2",
"2" => "h5-3",
"1" => "h5-4",
"11" => "h5-15",
"12" => "h5-19"
);
rsort($data, SORT_NATURAL);
echo $data[0];
which gives...
h5-19
Related
I have an array with some repeating string values. How to replace these string values (as a whole, because some words are repeated in others strings) with corresponding specific numeric values, as bellow?
deloc = 1
foarte puţin = 2
mediu = 3
mult = 4
foarte mult = 5
This is the array (example):
array = (
"tensionat" => "mediu",
"trist" => "mult",
"melancolic" => "deloc",
"fara_speranta" => "foarte puțin",
"nefolositor"] => "deloc",
"ingrijorat" => "foarte mult",
"amarat" => "deloc",
"anxios" => "mediu"
);
How can this
Try this
$data = array (
"tensionat" => "mediu",
"trist" => "mult",
"melancolic" => "deloc",
"fara_speranta" => "foarte puțin",
"nefolositor" => "deloc",
"ingrijorat" => "foarte mult",
"amarat" => "deloc",
"anxios" => "mediu"
);
$repl = array (
'deloc' => 1,
'foarte puţin' => 2,
'mediu' => 3,
'mult' => 4,
'foarte mult' => 5,
);
$result = array ();
foreach ($data as $key => $value) {
$result[$key] = !empty($repl[$value]) ? $repl[$value] : $value;
}
print_r($result);
Output:
Array
(
[tensionat] => 3
[trist] => 4
[melancolic] => 1
[fara_speranta] => foarte puțin
[nefolositor] => 1
[ingrijorat] => 5
[amarat] => 1
[anxios] => 3
)
I've an array.
Array
(
[initial] => MSS
[hour] => 5.2
[row_checker_1] => 1
[project_name_1] => KGD001
[project_shortcode_1] => KGD001
[5_1] => 23
[6_1] => 3.3
[4_1] => 23.2
[remarks_1] => on going
[task_id] => 76
[row_checker_2] => 2
[project_name_2] => DG001
[project_shortcode_2] => DG001
[5_2] => 1.1
[6_2] => 2.2
[4_2] => 3.1
[remarks_2] => on going
)
Now I want to split all element upper range key is "project_shortcode_1" and lower range key is remarks_1.
So, new array should look like:
array
(
[5_1] => 23
[6_1] => 3.3
[4_1] => 23.2
)
Use array_filter with flag ARRAY_FILTER_USE_KEY for using the array keys, and do the comparison with the logic needed to get the desired keys. It works from PHP 5.6.
$arr = array ( "initial" => "MSS",
"hour" => 5.2,
"row_checker_1" => 1,
"project_name_1" => "KGD001",
"project_shortcode_1" => "KGD001",
"5_1" => 23,
"6_1" => 3.3,
"4_1" => 23.2,
"remarks_1" => "on going",
"task_id" => 76,
"row_checker_2" => 2,
"project_name_2" => "DG001",
"project_shortcode_2" => "DG001",
"5_2" => 1.1,
"6_2" => 2.2,
"4_2" => 3.1,
"remarks_2" => "on going",
);
// PHP > 5.6
$result = array_filter($arr, function($k){
$var = explode('_', $k);
return is_numeric($var[0]) && $var[1]==1;
}, ARRAY_FILTER_USE_KEY);
If what you need is a multidimensional array with all the ranges NUMBER_N, then use something like this (extended from Dmitriy Demir answer):
$myArray = array(
'initial' => 'MSS',
'hour' => '5.2',
'row_checker_1' => '1',
'project_name_1' => 'KGD001',
'project_shortcode_1' => 'KGD001',
'5_1' => '23',
'6_1' => '3.3',
'4_1' => '23.2',
'remarks_1' => 'on going',
'task_id' => '76',
'row_checker_2' => '2',
'project_name_2' => 'DG001',
'project_shortcode_2' => 'DG001',
'5_2' => '1.1',
'6_2' => '2.2',
'4_2' => '3.1',
'remarks_2' => 'on going'
);
function splitRange($a){
$newArray = array();
foreach ($a as $k => $v) {
$rightFormat = preg_match('/^\d+_(\d+)$/', $k, $index);
if ($rightFormat)
$newArray[$index[1]][$k] = $v;
}
return $newArray;
}
print_r(splitRange($myArray));
The result will be something like:
Array
(
[1] => Array
(
[5_1] => 23
[6_1] => 3.3
[4_1] => 23.2
)
[2] => Array
(
[5_2] => 1.1
[6_2] => 2.2
[4_2] => 3.1
)
)
being N from NUMBER_N the index of the array.
Since you mentioned in the comments that you'd prefer to get all values that are in format NUMBER_1 I think you'd need to loop through your array and check the value names with regex, then add the values to a new array if they meet the criteria. Here's how I would do this:
$myArray = array(
'initial' => 'MSS',
'hour' => '5.2',
'row_checker_1' => '1',
'project_name_1' => 'KGD001',
'project_shortcode_1' => 'KGD001',
'5_1' => '23',
'6_1' => '3.3',
'4_1' => '23.2',
'remarks_1' => 'on going',
'task_id' => '76',
'row_checker_2' => '2',
'project_name_2' => 'DG001',
'project_shortcode_2' => 'DG001',
'5_2' => '1.1',
'6_2a' => '2.2',
'4_2' => '3.1',
'remarks_2' => 'on going'
);
$newArray = array();
foreach ($myArray as $k => $v) {
$rightFormat = preg_match('/^\d+_\d+$/', $k);
if ($rightFormat)
$newArray[$k] = $v;
}
print_r($newArray);
The result of print_r in that case would be:
Array ( [5_1] => 23 [6_1] => 3.3 [4_1] => 23.2 [5_2] => 1.1 [6_2] =>
2.2 [4_2] => 3.1 )
If the number after the underscore should always be 1 then change the regex from /^\d+_\d+$/ to /^\d+_1$/.
You can play around and see how regex works here.
PS: I've set all values to strings out of convenience. Feel free to modify that.
A regex-based solution seems fitting for this question.
preg_grep() is a function designed to apply a regex filter upon each value in an array. I little more tweaking is necessary for this case because the keys must be filtered instead.
The One-liner:
$output=array_intersect_key($input,array_flip(preg_grep("/^\d+_1$/",array_keys($input)))));
/* array (
'5_1' => 23,
'6_1' => 3.3,
'4_1' => 23.2,
)*/
Here is the step-by-step array manipulation...
array_keys($input); // create array with input keys as values
/* array (
0 => 'initial',
1 => 'hour',
2 => 'row_checker_1',
3 => 'project_name_1',
4 => 'project_shortcode_1',
5 => '5_1',
6 => '6_1',
7 => '4_1',
8 => 'remarks_1',
9 => 'task_id',
10 => 'row_checker_2',
11 => 'project_name_2',
12 => 'project_shortcode_2',
13 => '5_2',
14 => '6_2',
15 => '4_2',
16 => 'remarks_2',
) */
preg_grep("/^\d+_1$/",array_keys($input)); // filter the input array using regex pattern
/* array (
5 => '5_1',
6 => '6_1',
7 => '4_1',
) */
array_flip(preg_grep("/^\d+_1$/",array_keys($input))); // flip the filtered array
/* array (
'5_1' => 5,
'6_1' => 6,
'4_1' => 7,
)*/
array_intersect_key($input,array_flip(preg_grep("/^\d+_1$/",array_keys($input)))); // filter input by comparing keys against filtered array
/* array (
'5_1' => 23,
'6_1' => 3.3,
'4_1' => 23.2,
)*/
In the following array, which I have stored in $_SESSION['doc_brick_array'], I am trying to find the element with brick0. I want to delete this element, and then re-index the outer array. How can I do this?
Array
(
[0] => Array
(
[brick0] => Array
(
[city_name] => Lahore
[clinic_name] => shifa hospital
[attendant_name] => ali
[drd1_cell1] => 03017666454
[mbv] => 666
[brick_name] => LHR-0002
[clinic_address] => i-8 markaz
[drd1_phone] => 9798797
[drd1_cell2] => 04037777888
[drd1_email] => abc#yahoo.com
[visit_time] => m
)
)
[1] => Array
(
[brick1] => Array
(
[city_name] => Rawalpindi
[clinic_name] => aljanat hospital
[attendant_name] => kanzal
[drd1_cell1] => 03014544567
[mbv] => 6000
[brick_name] =>
[clinic_address] => i-9 markaz
[drd1_phone] => 07337837
[drd1_cell2] => 03017767575
[drd1_email] => abcd#yahoo.com
[visit_time] => m
)
)
)
The code I have tried
for($g=0; $g<=count($_SESSION['doc_brick_array']); $g++){
if (($key = array_search($brick_code, $_SESSION['doc_brick_array'][$g])) !== false) {
unset($_SESSION['doc_brick_array'][$key]);
$_SESSION['doc_brick_array'] = array_values($_SESSION['doc_brick_array']);
}
}
I think what you are after here is array_values() which, from the PHP.net site
array_values() returns all the values from the array and indexes the
array numerically.
As an example:
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
unset($a[2]);
/* will produce an array that would have been defined as
$a = array(1 => 'one', 3 => 'three');
and NOT
$a = array(1 => 'one', 2 =>'three');
*/
$b = array_values($a);
// Now $b is array(0 => 'one', 1 =>'three')
You can use array_filter to remove any elements from $_SESSION['doc_brick_array'] that have a brick0 key.
$x = array_filter($_SESSION['doc_brick_array']), function($y) {
return !isset($y['brick0']);
});
Then use array_values to reindex the result.
$_SESSION['doc_brick_array']) = array_values($x);
I have 2 arrays: Array1 and Array2. As you can see in Array1 I have 2 duplicate values. So what I want to do, is unset one of dublicates (doesn't matter which one) and as a result I need to unset value from Array2 with the same key as already unset value in Array1
Array1
(
[0] => 1331-14-2-45
[1] => 1344-1-4-22
**[2] => 1409-1-1-4**
[4] => 1312-14-1-23
**[5] => 1409-1-1-4**
[6] => 1365-10-3-30
)
AND
Array2
(
[0] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[1] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[2] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
[4] => deviceNotActive#nemodel.GPON.4.6
[5] => deviceNotActive#nemodel.GPON.4.6
[6] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
)
<?php
$array1 = array
(
0 => '1331-14-2-45',
1 => '1344-1-4-22',
2 => '1409-1-1-4',
4 => '1312-14-1-23',
5 => '1409-1-1-4',
6 => '1365-10-3-30',
);
$array1_tmp = $array1;
$array2 = array
(
0 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
1 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
2 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
4 => 'deviceNotActive#nemodel.GPON.4.6',
5 => 'deviceNotActive#nemodel.GPON.4.6',
6 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
);
$array1 = array_unique($array1);
$remove_keys = array_keys(array_diff_key($array1_tmp, $array1));
foreach($remove_keys as $k => $v) {
unset($array2[$v]);
}
You can use the array_unique() function, and maybe take a look at the Array functions list.
Use array_unique function and array_diff
$uniqueValues = array_unique($inputArray); //your first array
$deletedValues = array_diff($inputArray, $uniqueValues);
foreach($deletedValues as $key => $deletedValue){
unset($secondIput[$key]); //you second array
}
I am trying to resolve how to clean up my array and my output should be:
1) all numerical indexed and
2) no duplicate values.
This is a sampling of my current array output:
NOTE: index [3]. I am parsing those values into a new array and do not need either of them. They are merged onto the array below as you can see.
Ultimately I am preparing this for .csv format, but I need my array in some type of sequence in order to keep the data in proper presentation form.
Array // print_r();
(
[0] => 3350
[id] => 3350
[1] => Williamson New England Electric
[company] => Williamson New England Electric
[2] => bob#jojozzz.go
[email] => bob#jojozzz.go
[3] => Pumps & Motors,Electronic Systems,Electrical Supply,Electrical
[industry] => Pumps & Motors,Electronic Systems,Electrical Supply,Electrical
[4] => Pumps & Motors
[5] => Electronic Systems
[6] => Electrical Supply
[7] => Electrical
)
This is what I am trying to achieve:
Array
(
[0] => 3350
[1] => Williamson New England Electric
[2] => bob#jojozzz.go
[4] => Pumps & Motors
[5] => Electronic Systems
[6] => Electrical Supply
[7] => Electrical
)
As I said in my comment, it's probably easier to get the array as numerical array from the beginning, if you get it from a MySQL database:
$row = mysql_fetch_row($result);
Then you can do the following to split the value of the last column:
array_splice($row, -1, 1, explode(',', end($row)));
DEMO
See Felix's comment for a possible way to get your array in the right format at source. If thats not possible, this will do the trick;
$array = Array(
'0' => 3350,
'id' => 3350,
'1' => 'Williamson New England Electric',
'company' => 'Williamson New England Electric',
'2' => 'bob#jojozzz.go',
'email' => 'bob#jojozzz.go',
'3' => 'Pumps & Motors,Electronic Systems,Electrical Supply,Electrical ',
'industry' => 'Pumps & Motors,Electronic Systems,Electrical Supply,Electrical ',
'4' => 'Pumps & Motors',
'5' => 'Electronic Systems',
'6' => 'Electrical Supply',
'7' => 'Electrical'
);
$outputArray = array_values( $array );
$uniqueArray = array_unique( $outputArray );
var_dump( $uniqueArray );
Cannot guarantee anything about performance, but you can use is_numeric to ensure (#1) only numerical indexes, and use array_unique to ensure (#2) no duplicates:
$b = filterNonNumericKeysInArray($a);
$b = array_unique($b);
function filterNonNumericKeysInArray($arr) {
$finalArr = Array();
foreach ($arr as $key => $val) {
if (!is_numeric($key)) { continue; }
$finalArr[$key] = $val;
}
return $finalArr;
}