I'm trying to loop through a CSV with PHP, I've already done that, but now I need to keep the URLs of the images as they are from a reseller catalog so I can upload them to woocommerce.
The thing is that I can't find the way to find the path of the image inside an array, which I have created to store all the CSV info, I have used some PHP functions but without result. I attach code and image of what I need.
I have an HTML form that allows me to select the CSV to process it and I'm interested in doing it in a generic way because this script can be used in more situations.
My current php script is like this:
<?php
$fila = 1;
$array = array();
$buscar = 'https://';
$resultadoBusqueda;
if (($gestor = fopen("Productos-Euromoto-20-09-2018.csv", "r")) !== FALSE) {
while (($datos = fgetcsv($gestor, 1000, ",")) !== FALSE) {
$numero = count($datos);
echo "<p> $numero de campos en la línea $fila: <br /></p>\n";
$fila++;
for ($i=0; $i < $numero; $i++) {
//echo $datos[$i] . "<br />\n";
$array = $datos[$i];
echo 'contenido array: ' . $array;
echo "<br>";
//echo 'foto: ' . strstr($array[$i], 'https://', true);
}
}
$resultadoBusqueda = array_filter($array, function($var) use ($buscar) { return stristr($var, $buscar) } );
if($resultadoBusqueda){
echo 'se ha encontrado el termino "' . $buscar .'" en la posicion <br>';
foreach($resultados as $resultadoBusqueda){
echo $resultados . "<br>";
}
}else{
echo 'El termino "' . $buscar . '" no se ha encontrado en el array ';
}
}
But it returns me that an argument is missing. Anyway, I don't know if it will be the best way to do this. What I need is that, within the array, look for the string that begins with "http://" and ends with ".jpg"
I appreciate all help.
All the best
When using
and then in each loop:
what you get from the latter is an array where the fields are already separated, you wouldn't need to explode. fgetcsv returns an array itself:
The bad thing is that in your third parameter you are telling it that your CSV is separated by commas, but as seen in the photo it is separated by semicolons , so you must use semicolons as the third parameter and not commas, as you have in that moment:
Assuming your CSV had the form:
Your procedure is equivalent to iterating over an array that looks like:
(Of course, fgetcsv will be removing one row at a time, so as not to store everything in memory)
For the purposes of the example, your iteration would be like doing:
(sprintf saves me from interpolating ugly variables)
To get the images you use a regular expression that says:
That is:
that prints
In each iteration you can add the images to an array and from this obtain a list of SKU => image tuples:
And that prints:
COMPLETE EXAMPLE
One way to find the field containing 'https://' is like this:
explode
.You go through the newly created array and look for the string you are interested in, with a
IF
.Another way can be...
Knowing the position of the field that contains the images can also be done like this...
string
to aarray
withexplode
.foreach
.IF
to filter the position.Assuming that the field in position 11 (if it is an array it will be 10)...
Hope this can help you.