Error in explode folder separator?

5

In the following code

<?php
$urlproducto = $_GET['url'];
$carpetas = $_GET['cat'];
$categoria = explode('/', $carpetas);
echo $categoria;
?>

shows me the error of: notice: array to string conversion line 4

I need to unify the folders or categories with the value of the product that is in the URL.

so carpeta/1222/hola-mundo

    
asked by Tania 28.07.2018 в 07:34
source

1 answer

7

Once you make explode becomes an array, and the error it gives you is trying to show an array with echo , you have to go through it with a loop:

for ($i=0; $i<count($categoria); $i++) {
  echo $categoria[$i];
}

If you are looking for a specific parameter, for example 1222 in 'folder / 1222 / hi-world', you do the echo of that position.

echo $categoria[1];

To show the first two elements of the previous example:

$url = $categoria[0]."/".$categoria[1];
echo $url;

Show URL joining two variables , edit to show comments.

After explode unes the elements you need from the array and the product. Check that the positions are correct, and keep in mind that if you change to a different environment than the tests can change.

 <?php
    $urlproducto = $_GET['url'];
    $carpetas = $_GET['cat'];
    $categoria = explode('/', $carpetas);
    $url = $categoria[7]."/".$categoria[8]."/".$categoria[9]."/".$urlproducto;
    echo $url;
    ?>
    
answered by 28.07.2018 / 09:02
source