Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php函数以避免重复代码_Php - Fatal编程技术网

Php函数以避免重复代码

Php函数以避免重复代码,php,Php,我正在使用下面的代码拆分字符串。我必须为每个输入重复代码$ProductsURL[x]和输出$productx[] <?php $url = "$ProductsURL[0]"; $urls = split("http://", $url); $product0 = array(); foreach($urls as $val){ if(!$val) continue; $product0[] = "http://$val"; } ?> <?php $url =

我正在使用下面的代码拆分字符串。我必须为每个输入重复代码
$ProductsURL[x]
和输出
$productx[]

<?php
$url = "$ProductsURL[0]";
$urls = split("http://", $url);
$product0 = array();
foreach($urls as $val){
    if(!$val) continue;
    $product0[] = "http://$val";
}
?>
<?php
$url = "$ProductsURL[1]";
$urls = split("http://", $url);
$product1 = array();
foreach($urls as $val){
    if(!$val) continue;
    $product1[] = "http://$val";
}
?>
..................

..................

是否有一种方法可以避免为每个需要的输入/输出重复代码。我需要代码~100次

使用简单循环。并使用数组变量作为散列来存储数据

$product = array();    
for ($i = 0; ; $i++) {
        if ($i > 1) {
            break;
        }
       $url = "$ProductsURL[$i]";
    $urls = split("http://", $url);

    foreach($urls as $val){
        if(!$val) continue;
        $product[$i][] = "http://$val";
    }
    }
另外,我认为您在以下行中有错误:
$url=“$ProductsURL[$I];”

最后应该是这样的:

$product = array();
for ($i = 0; $i < 100; $i++) {
  $url = $ProductsURL[$i];
  $urls = split("http://", $url);
  $product[$i] = array();
  foreach($urls as $val){
    if(!$val) continue;
    $product[$i][] = "http://$val";
  }
}
$product=array();
对于($i=0;$i<100;$i++){
$url=$ProductsURL[$i];
$url=split(“http://”,$url);
$product[$i]=array();
foreach($url作为$val){
如果(!$val)继续;
$product[$i][]=“http://$val”;
}
}

不需要函数。
您只需要一个旧的普通
foreach
循环。
也许还有一些关于如何使用变量的知识


您可以使用变量作为变量名,使用$$

<?php

foreach ($productURL as $key => $value) {
$newName = 'product' . $key;
$$newName[] = $value;
}
?>

相反,我会选择将所有产品存储在一个数组中,而不是单独的变量。您需要的代码应该是:

<?php
$products = array();
foreach ($ProductsURL as $productUrl) {
    $product = array();
    $urls = split("http://", $url);
    foreach($urls as $val){
        if(!$val) continue;
        $product[] = "http://$val";
    }
    $products[] = $product;
}
?>


你能解释一下你的代码是做什么的吗?我的数据库中存储了一个字符串中的产品URL:。我将它们与上面的代码分开,以获得$product0[1]以从一个品牌获得fisr产品,等等……我会引导它们朝着
$product[0]
而不是
$product0
。我也是。但这正是OP的要求。有时他们要求一些东西,因为他们不知道其他的方式。
<?php

foreach ($productURL as $key => $value) {
$newName = 'product' . $key;
$$newName[] = $value;
}
?>
<?php
$products = array();
foreach ($ProductsURL as $productUrl) {
    $product = array();
    $urls = split("http://", $url);
    foreach($urls as $val){
        if(!$val) continue;
        $product[] = "http://$val";
    }
    $products[] = $product;
}
?>