This question already has answers here:
Get and remove first element of an array in PHP
(2 answers)
Closed 8 years ago.
I have a range of fourier transform values in php as shown below.
[8974,398.22605659378,340.40931712583,224.61805748557,224.21476160103,531.02102311671,348.09311299013,74.373164045484,440.15451832283,379.54095616825,801.05398080895,184.15175862698,539.59590498835,114.82864261836,595.84662567888,488.12623039438,370,488.12623039438,595.84662567888,114.82864261836,539.59590498835,184.15175862698,801.05398080895,379.54095616825,440.15451832283,74.373164045484,348.09311299013,531.02102311671,224.21476160103,224.61805748557,340.40931712582,398.22605659378]
How do I actually ignore/delete away the first value in this case "8974" in php language?
You can use array_shift();
Like
$array=array(8974,398.22605659378,340.40931712583,224.61805748557,224.21476160103,531.02102311671,348.09311299013,74.373164045484,440.15451832283,379.54095616825,801.05398080895,184.15175862698,539.59590498835,114.82864261836,595.84662567888,488.12623039438,370,488.12623039438,595.84662567888,114.82864261836,539.59590498835,184.15175862698,801.05398080895,379.54095616825,440.15451832283,74.373164045484,348.09311299013,531.02102311671,224.21476160103,224.61805748557,340.40931712582,398.22605659378);
array_shift($array);
print_r($array);
You could use array_shift
ie:
$myArray = array(8974,398.22605659378,340.40931712583,224.61805748557,224.21476160103,531.02102311671,348.09311299013,74.373164045484,440.15451832283,379.54095616825,801.05398080895,184.15175862698,539.59590498835,114.82864261836,595.84662567888,488.12623039438,370,488.12623039438,595.84662567888,114.82864261836,539.59590498835,184.15175862698,801.05398080895,379.54095616825,440.15451832283,74.373164045484,348.09311299013,531.02102311671,224.21476160103,224.61805748557,340.40931712582,398.22605659378);
array_shift($myArray)
You can easily unset first element to Delete it:
unset($array[0]);
Ignore it
Create a temporary array from the first by skipping the first element:
foreach (array_slice($arr, 1) as $xyz) {
/// ...
}
Or, without creating a temporary array:
reset($arr); next($arr); // reset and skip first element
while (list($key, $value) = each($arr)) {
echo $value;
}
Modify it
Modify the array in place by shifting the first item off:
array_shift($arr);
// do whatever you want
Related
This question already has answers here:
Selecting every nth item from an array
(9 answers)
Remove every 3rd and 4th element from array in php
(1 answer)
How to access N-th element of an array in PHP
(3 answers)
Remove every nth item from array
(6 answers)
foreach step 5 steps forward in an array
(3 answers)
Closed 4 months ago.
i have a array like this
$names = [a,b,c,d,e,f,g,h,i,j,k,l,m];
what i wan to remove
$remove = "b,c,e,f,h,i,k,l";
then i need a new array from the remaining elements like below
$new_arr = [a,d,g,j,m];
Use array chunk to split by 3 and take out first element.
<?php
$names = [a,b,c,d,e,f,g,h,i,j,k,l,m];
$chunked =array_chunk($names, 3);
$filtered = [];
foreach($chunked as $chunk){
$filtered[] = $chunk[0];
}
var_dump($filtered);
?>
Instead of removing number 2 and number 3 from each 3 elements, the task is simply written like this:
Keep the first of every 3 items.
This can be determined using the index and the modulo operator %.
Using array_filter saves the foreach loop. This allows the solution to be implemented as a one-liner.
$names = ['a','b','c','d','e','f','g','h','i','j','k','l','m'];
$result = array_filter($names,fn($k) => $k%3 == 0, ARRAY_FILTER_USE_KEY );
var_dump($result);
Demo: https://3v4l.org/sKTSQ
The $names array needs to be noted as in the code above. A notation without quotes like in the question produces error messages.
This question already has answers here:
PHP get previous array element knowing current array key
(10 answers)
Closed 4 years ago.
So I tried doing this :
$array = array('first'=>'111', 'second'=>'222', 'third'=>'333');
var_dump(prev($array["second"]));
Hoping to get 111 but I get NULL. Why?
$array["second"] returns 222. Shouldn't we get 111 when we use PHP's prev() function?
How to get the previous value of an array if it exists using the key?
Your current value from $array["second"] is not an array and prev takes an array as a parameter.
You have to move the internal pointer of the $array and then get the previous value.
$array = array('first'=>'111', 'second'=>'222', 'third'=>'333');
while (key($array) !== "second") next($array);
var_dump(prev($array));
prev function expects an array as an argument but you're passing a string.
$array["second"] evaluates to '222'
In this case, you point directly the previous value, without iterating the array.
$array = array('first'=>'111', 'second'=>'222', 'third'=>'333');
$keys = array_flip(array_keys($array));
$values = array_values($array);
var_dump($values[$keys['second']-1]);
This question already has answers here:
How to use array values as keys without loops? [duplicate]
(6 answers)
Closed 6 years ago.
Please check attached images. These image contains $complex_arr print and $simple_arr print results. I want to convert $complex_arr to $simple_arr output.
How is that possible? What is actually doing each of $complex_arr inside value will be converted as associative simple array like $simple_arr
$arr1 = array("asso1"=>"a", "asso2"=>"1");
$arr2 = array("asso1"=>"b", "asso2"=>"2");
$arr3 = array("asso1"=>"c", "asso2"=>"3");
$complex_arr = array($arr1,$arr2,$arr3);
$simple_arr = array("a"=>"1", "b"=>"2", "c"=>"3");
// print_r($complex_arr);
print_r($simple_arr);
Input:
Output:
You have to write it on our own... here is my idea:
public function makeItSimpler($arr){
$newarr = array();
foreach($arr as $complex){
$newarr[$complex['asso1']]=$complex['asso2'];
}
return $newarr;
}
Perhaps you can do it better... take look at "array-map" http://php.net/manual/de/function.array-map.php
Good Luck
foreach ($complex_arr as $key => $value) {
$simple_arr[$value['asso1']]=$simple_arr[$value['asso2']];
}
With php5.5 (where array_column becomes available) it is:
$simple_arr = array_combine(
array_column($complex_arr, 'asso1'),
array_column($complex_arr, 'asso2')
);
And even simplier (after I reread function manual):
$simple_arr = array_column($complex_arr, 'asso2', 'asso1');
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Have those syntaxes:
Foreach:
$array=array('v1','v2','v3');
foreach( $array as $value ){
echo $value;
}
Output:
v1v2v3
Implode:
$array=array('v1','v2','v3');
$value=implode(" ",$array);
echo $value;
Output:
v1 v2 v3
I need some help understanding the difference between implode(),foreach() used in the situation above.Are they the same? Or what is the difference? Which i should use and when?
For the record,i know small differences,and things like that.I just want to know your opinion and if there is something i didn't know about those functions.
Generally loops may be used for making any action you want.
You can for example concatenate string with other string depending or array element:
$array=array('v1','v2','v3');
foreach ($array as $value) {
if ($value == 'v1') {
echo $value.' something';
}
else {
echo $value.' something2';
}
}
and if you have numbers in your array you can do math operations:
$array=array(1,2,3);
foreach ($array as $value) {
echo ($value + 5).' ';
}
You can also change array elements:
$array=array(1,2,3);
foreach ($array as &$value) {
$value += 3;
}
unset($value);
foreach ($array as $value ){
echo $value.' ';
}
// result: 4 5 6
Implode is just a function that merge array elements and put a splitter between them. Nothing more. It doesn't change array elements. It simple returns output string. It's usually used for preparing data to display.
Also implode is much more convenient for putting splitter between elements. In loop if you want to put space between all elements but you don't need to put space after last element you need to make extra checks for last element and using implode you don't need to care because implode just does it for you.
And in your case, output wouldn't be the same because your loop would output:
v1v2v3 and implode would output v1 v2 v3 because you used space separator
foreach is a loop statement. implode is a function. That's the difference. You are supposed to use foreach for any kind of operation for each element in an array. But implode is a helper method that binds the elements of the given array by using the given string as binder.
foreach() : it's a looping concept means you get values one by one and print you will found each result/value seperate while you not concat that.
implode() : convert an array to string with passsing glue. and you will get all array values in one string.
implode() also iterating array internally, and converting array into string
loop is also doing same thing here,
loop advantage here
1) when you want to conditional part, for instance
you want to exclude v2 from string or other operation depend on requirement
$array=array(1,2,3);
foreach ($array as $value ){
if($value == 2) continue;
echo $value." ";
}
these things can't apply in implode function
2) In loop you can direct buffer output or append into string.. [this is denpend on requirement],
import is only function that is combine array values into string.
how can it be same, both r used in different places. for example
list($y,$m,$d)=explode("-",$query); //separating and storing in variable `y-m-d`
$arr=array($m,$d);
$cat=implode('-',$arr); //u got that format again with only two variables
implode expects array.
where foreach is a loop which iterates through given array() until and unless it meets the false condition and gets out of the loop
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to create comma separated list from array in PHP?
I have an array as follows;
$array = array(1,2,3,4,5);
I want to print or echo this variable as 1,2,3,4,5. What is the simplest method for this?? I printed array[0] first and then skipped first value and used foreach function to echo all remaining ",".$value.
Try following
echo implode(",", $array);
You can use the implode function.
In the example you showed, it'd be written like this:
implode(',', $array);
$array = array(1,2,3,4,5);
$result = implode(',', $array);
echo $result;