Implode() vs Foreach() [closed]

我只是一个虾纸丫 提交于 2019-12-06 13:58:14

问题


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.


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/25928108/implode-vs-foreach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!