how to flip flip STRING and decimal/ float values in array PHP? - php

I am trying to make a app that allow me to rank and sort decimal number according to it's value , but array_flip function can't flip string and decimal number ,
<?php
$myarray = array(1,0.334,-0.334,-1);
//create a copy and sort
$myarray_copy = $myarray;
rsort($myarray_copy);
//reverses key and values
$myarray_copy = array_flip($myarray_copy);
//create result by using keys from sorted values + 1
foreach($myarray as $val)
$myarray2[] = ($myarray_copy[$val]+1);
//print final array
print_r($myarray2);
print_r($myarray);
?>
and there is a warning about array_flip
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in C:\xampp\htdocs\ranking.php on line 9
, do you guys know how to deal with these ? is there any solution ?

After sort use array_walk to convert each item from decimal to string like ---
function test_alter(&$item1, $key)
{
$item1 = (string)$item1;
}
array_walk($fruits, 'test_alter', 'fruit');
And then flip it. Hope this helps.

Related

computation on array-search in php

I have a multi-D array like so:
array ( 'JD'=>2457002.50, 67.618536),
array ( 'JD'=>2457003.50, 67.619705),
array ( 'JD'=>2457004.50, 67.620938)....
I have a value say:
$MyJD = 2457003.9553;
I would like to find the value in the array, and if not, match the closest number to the array in question and return the the next index (which i'm assuming is [1])
I was thinking to do an array_search, but it's not going to find the exact number, I want the closest number to $MyValue?
This won't return the index but will return the proper array:
array_multisort(array_map(function($v) use($MyJD) {
return abs($v['JD'] - $MyJD);
}, $array), $array);
$result = reset($array);
Calculate the difference between each JD value and $MyJD
Sort on the difference (sorting the original) and get the lowest (first) one
Alternately, you could combine using the difference as the key and then sort on the keys:
$array = array_combine(array_map(function($v) use($MyJD) {
return abs($v['JD'] - $MyJD);
}, $array), $array);
ksort($array);
$result = reset($array);
Maybe someone will post a good array_reduce answer.
Do bucle put new array with key difference between
In this way you will have array ordered by difference
In each iteration
$myarray[myvalue - yourvalueinbucle] if -1 this key then ×-1 and how value your enter iterator of bucle, then your value for this key = your line multiarrayvalue in for each iterator

How to get key of an associative array by searching for offset value?

Using this as an example and being aware of key,
$arr = array(
'product1'=>array('color'=>'blue','size'=>'medium'),
'product2'=>array('color'=>'green','size'=>'large'),
'product3'=>array('color'=>'yellow','size'=>'small'),
);
Is there a method for getting any key in multidimensional array by its incremented value?
For example, I'd like to get the key of the third array value in $arr above. $arr[2] would return the value (an array containing yellow/small).
Is there a way to leverage the key function to get any key by its numeric iterator, rather than the key from the "current position"?
Or, is there another built-in function that I am obviously overlooking which would return the key of $arr[2] instead of it's value?
echo getkey($arr[2]);
# returns product3
Just use array_keys function :
$arr = array(
'product1'=>array('color'=>'blue','size'=>'medium'),
'product2'=>array('color'=>'green','size'=>'large'),
'product3'=>array('color'=>'yellow','size'=>'small'),
);
$keys = array_keys($arr);
echo $keys[2];
// shorter version
echo array_keys($arr)[2];
More infos : http://php.net/manual/en/function.array-keys.php
It doesn't seem logical/efficient to generate a new/full array of keys just to select one from it. The other answers are "working too hard".
array_slice() specifically extracts portions of an array based on position rather than key name. This makes it the perfect function for this case.
Better practice would be to only slice away the subarray that you want, then call for its key, like this:
Code: (Demo)
$arr = array(
'product1'=>array('color'=>'blue','size'=>'medium'),
'product2'=>array('color'=>'green','size'=>'large'),
'product3'=>array('color'=>'yellow','size'=>'small'),
);
$key=2;
echo key(array_slice($arr,$key,1)); // [input],[0-indexed position],[number of subarrays]
Output:
product3
You can use array_keys() function:
function getKey($arr, $i) {
if (empty($arr[array_keys($arr)[$i]])) {
return null;
}
// array_keys($arr)[$i] returns original array's key at i position
// if i = 2, array_keys($arr)[$i] = 'product3'
return array_keys($arr)[$i];
}
// echo getKey($arr, 2);
// returns product3

how to find the largest and smallest value in a string

Hi I have a string and I want to find about the smallest and the largest value from this string and store in a variable.
Array
(
[0] => 21,50
)
You should use max and min function.
$arr = ['21,50'];
echo max(explode(',',$arr[0])); // 50
echo min(explode(',',$arr[0])); // 21
Hope it will help you :)
If you have a string $a, then you can explode it, to get an array and then you can use the build in function min() and max()
$a = "21,50";
$arr = explode($a);
$maxValue = max($arr);
$minValue = min($arr);
You can also explode an array key to achieve this.
$myArr = array("21,50");
$tmpArr = explode($myArr[0]);
$maxVal = max($tmpArr);
$minVal = min($tmpArr);

PHP Arrays : How to Find the Lowest/Highest Value

I have two array like this :
$array1 = array(1,1,2,3,3,4,5); //remember that i have two '1' value in this array
$array2 = array($url1, $url2, $url3, $url4, $url5, $url6);
I wish to find the lowest/highest value in $array1 then link to $url1/$url5 like this :
1 or 5
How I can make this happen using PHP? Any help would be greatly appreciated
Thanks
Use the max() & min() function
max — Find highest value
min — Find lowest value
Example code:
$max = max($array);
$min = min($array);
If the array is already sorted, use $array2[0] and $array2[count($array2)-1].
If it's not already sorted, you can use this to sort the arrays.
array_multisort($array1, SORT_NUMERIC, $array2);
$lowest = $array2[0];
$highest = $array2[count($array2)-1];
As far as I understand you need this 2 functions: max, min
try this
$maxValueKeys = array_keys($array1, max($array1)); // Your min value indexes
$minValueKeys = array_keys($array1, min($array1)); // Your max value indexes
But it isn't an associative array

change starting index when assigning one array to another in php

I am using str_split() to split a long strings into an array of length 16 each. And I'm assigning the returned array to one in my function. Like this:
$myarray = str_split($string, 16);
The problem is that I want the indexing of $myarray to start from a number other than 0, say 50. Currently I'm doing this:
foreach($myarray as $id => $value)
{
$myarray[$id + 50] = $value;
unset($myarray[$id]);
}
Is there a better solution? Because the arrays and strings I'm dealing with are very long. Thanks
You can use array_pad().
$myarray = str_split($string, 16);
$myarray = array_pad($myarray, -(size($myarray)+50), null);
It will fill the first 50 elements with nulls and push the rest of the array forward by 50 elements.

Categories