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
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 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 7 years ago.
Improve this question
I have arraylist like : [1, 2, 3, 4, 5];
How can I get each of these values in php?
Is there any function ? I tried array() but it did not return anything.I really appreciate any help.
Let say your $array = [1,2,3,4,5];
To get the values:
foreach($array as $values):
echo $values.'<br/>';
endforeach;
Or if you want based on elements, you can get by this way:
//To get value 1
echo $array[0];
// 0 being here the index of your array
// so echo $array[0] will output 1 since it's the first key of your array
If you have an array like this
$numbers = array(1,2,3,4,5);
with five values (i.e indexed 0 - 4) you can print out all values in the array using the print_r() function like this:
print_r($numbers);
Or you can loop through every element in the array with any PHP looping statement like FOR STATEMENTS, FOREACH STATEMENTS, WHILE STATEMENTS..
e.g
foreach($numbers as $num){
echo $num;
echo '<br/>';
}
You can also get a single value from the array using the index for example my array has 5 values (indexed from 0 - 4) if i want to print out the number '3' i would write
echo $numbers[2];
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
Right now i am working on a simple PHP script.
I have four variables:
$test_a = 25;
$test_b = 24;
$test_c = 22;
$test_d = 35;
I want to display not the numbers from the variables. I need to extract only the two variables with the highest number.
So i need result something like this:
<?PHP echo "The two highest variables are: $test_a and $test_d";?>
So how i can extract only the two highest variables?
Add the values to array, sort it in descending order, then take two first elements:
$a = array($test_a, $test_b, $test_c, $test_d);
arsort($a);
echo 'Two highest values:'.$a[0].' and '.$a[1];
Put your values in an array: $array = array(25, 24, 22, 35); and get the first highest value using php max function:
$highest[] = max($array); //store it in an array so you can compare using array_diff
Remove that value from your array w/ array_diff:
$array = array_diff($array, $highest); //remove highest from original array
And then repeat finding highest with max:
$second_highest = max($array);
echo "The two highest variables are: $highest[0] and $second_highest";
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);
?>