get all values from arraylist in php [closed] - php

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];

Related

PHP Add all Values in Array Excluding Last [closed]

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);

divide every single value in an array on the next value to return a number [closed]

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 4 years ago.
Improve this question
how can i using php divide every single value in an array on the next value to return a number like a have an array
i want a function that divide 1/2/3/4/5 and it should return this value (0.0083333333333333)
i have tried this
<?php
function vo(){
$newArray=array(1,2,3,4,5);
$resulte=1;
foreach($newArray as $value){
$resulte=$value/$resulte;
}}
vo();
?>
output for this code, resulte=1.875
i think im bad at math not sure tho
$your_array = [1,2,3,4,5];
$result = $your_array[0]; //result defaulted to first element in array;
for($i=1; $i<sizeof($your_array); $i++){ //loop starts from 2nd //element in array
$result /= $your_array[$i];
}
code explanation: set initial value of $result to first element of array, as this is the first thing that you said you want to divide;
start looping through your array from 2nd element, and set the value of $result to it's current value divided by the current element in the loop (this is why /= is used)
the final value of $result, becomes the accumulated result of the division of each element of the array divided by the element next to it.
if you output $result, you will get 0.0083333333333333
You got your division backwards. You want to divide by the next number in the list, therefore your $resulte = $value / $resulte should be $resulte = $resulte / $value. Also remember to add a print($resulte) after the loop.

PHP return the Two highest variables from four [closed]

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";

How to split an array value? [closed]

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 ;-)

insert array of new item in any position in another array [closed]

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

Categories