Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let´s say I´ve got an array like [3,2,8,4] (just an example, it can have more or less values).
I want the numbers to be in the same order but instead use numbers 1-4 (if there are 4 values as in this example), ie. [2,1,4,3].
How can I accomplish this?
$data = [3,2,8,4];
$keys = array_keys($data);
array_multisort($data, SORT_ASC, $keys);
array_walk($keys, function(&$value) { ++$value; });
var_dump($keys);
,You could go with ArrayReplace.
<?php
$base = array(3 2, 8, 4);
$replacements = array(0 => 2, 1 => 1, 2 => 4, 3 => 3);
$store = array_replace($base, $replacements);
?>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array that looks like this:
Array ( [0] => 1 [1] => 1 [2] => 3 )
I'd like to get the sum of the values excluding the last value.
So in this example I'm looking to find the answer to 1 + 1.
The array could be varying lengths.
You can do like this
$myArray= [1,1,3];
array_pop($myArray); // Remove the last element from array
echo array_sum($myArray); // Sum the values of the array
You can do something like this
<?php $array = array(1, 1, 3);
$sliced = array_slice($array, 0, -1); ?>
This will return you 1 & 1 it won't give you 3
<?php $value = array(1,1,3);
$removed = array_pop($value);
echo array_sum($value);
?>
Another way to do using closure
$array = [1,1,3];
echo array_sum(array_filter($array, function($key) use ($array) {
return count($array) - 1 !== $key;
}, ARRAY_FILTER_USE_KEY));
try this its very simple & fastest way
// initialize array by 'short syntax'
$a = [
1,1,3
];
//remove last element of array no matter length
// This function will reset() the array pointer of the input array after use.
array_pop($a);
// then you can sum of rest array values by array_sum
echo array_sum($a);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How can I find out if an array has values for specific positions AND everything else is empty?
Let's say I want to check indexes 2,3,4
If I have this array:
10,3,56,78,89,89 returns false
if I have ,,45,56,67,, returns true
How can I do this in PHP?
Try this out (considering we're inside of function)
$allowed = [2, 3, 4];
$arr = [NULL, NULL, 45, 56, 67, NULL, NULL];
foreach($arr as $k => $v)
if(!empty($v) && !in_array($k, $allowed)) return false;
return true;
This works well for you. array compare, array_keys(), array_filter()
return [2,3,4] == array_keys(array_filter($array)) ? true : false;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
An array has negative and positive numbers. Separate the numbers such that negative numbers are at the beginning and positive numbers at the end without changing the order.
Example:
Array = {1, -3, -5, 9 , -8}
O/P = {-3, -5, -8, 1, 9}
I found many answer in c , c++ , java but not in PHP , so can any one please let me know how or better way to achieve this?
However this question put on hold , i tried with some of solutions and
find my own answer added below , hope it might be useful for someone.
function part($arr){
$j = 0;
for($i=0;$i<count($arr);$i++){
$val = $arr[$i];
$k = $i;
while($k>$j && $val < 0){
$arr[$k] = $arr[$k-1];
$k = $k-1;
if($j==$k){
$j=$j+1;
}
$arr[$k] = $val;
}
}
return $arr;
}
$arr = array(1, -3, -5, 9 , -8);
print_r(part($arr));
Without doing all the work for you.
Look into array_filter you could filter the negative values into 1 array, then filter the positive values into another array.
Then use array_merge to merge the 2.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
In my array I have these values:
[10d, 10e] => 4
The values are retrieved from another website and the important values will be put in this array.
Now I want to have two single values from these like:
[10d] => 2 [10e] => 2
How can I do this?
Now I'm guessing a little because of the description that is not that clear.
$arr = array('10d, 10e' => 4);
$newArr = array();
foreach($arr as $key => $value) {
$newKeys = explode(',', $key);
foreach($newKeys as $item) {
$newArr[trim($item)] = $value / count($newKeys);
}
}
print_r($newArr);
Result
[10d] => 2 [10e] => 2
Some functions you can use:
foreach() to loop the input array
explode() to split the key
trim() to remove whitespace
count() to, well, count array items
And, of course, / to divide ;-)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I add new item to array? For example into middle of an array ?Should we use array splice or an array merge ?
Could you explain me the difference between both function ?
Say I have
$a1=array("a"=>"Horse","b"=>"Dog","c"=>"Cow",);
$a2=array("d"=>"Cat");
Now I need to add $a2 in 2 position .
Which one should I use ?
You can use array_splice, except that won't keep your keys.
$a1 = array("a"=>"Horse", "b"=>"Dog", "c"=>"Cow");
$a2 = array("d"=>"Cat");
array_splice($a1, 2, 0, $a2);
// $a1 is now: array("a"=>"Horse", "b"=>"Dog", 0=>"Cat", "c"=>"Cow");
If you want Cat to have a key of d, you can use a mix of array_slice and the array union operator (+):
$a1 = array_slice($a1, 0, 2) + $a2 + array_slice($a1, 2);
// $a1 is now: array("a"=>"Horse", "b"=>"Dog", "d"=>"Cat", "c"=>"Cow");
you can use array_push to add array at any position. ..array_splice can also be used. .
example: array_splice