0

I am using the below code to split a string. And i have to repeat code for each input $ProductsURL[x] and output $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";
}
?>
..................

Is there a way to avoid repeating the code for each input/output needed. I need the code ~100 times

2
  • Could you explain what your code does? Commented Jan 20, 2015 at 22:49
  • I have store in my data base the products URL`s in one string : example.com/p2http://example.com/p1 . and i split them with the above code to get $product0[1] to get the fisr product from a brand and so on... Commented Jan 20, 2015 at 22:54

5 Answers 5

1

Use simple loop. And use array variables as hashes to store data.

Also I think that you have error in the following line: $url = "$ProductsURL[$i];".

Finally it should be something like that:

$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";
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

No need for a function.
All you need is an old plain foreach loop.
And maybe some knowledge how work with variable variables (http://php.net/manual/en/language.variables.variable.php)

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

Then, to check the outcome:

print_r($product0);
print_r($product1);
print_r($product2);
....

3 Comments

I would steer them towards $product[0] instead of $product0.
Me too. But this is what the OP asked for.
Sometimes they ask for something because they don't know of any other way.
0
$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";
    }
    }

Comments

0

You can use variables as variable names, using $$

<?php

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

Comments

0

Instead of that, I would choose to store all the products in an array instead of separate variables. The code you'd need should be something like:

<?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;
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.