0

I am trying to read each column from the third one to the 12th from a csv file and store all the data in an array. At this point I only get the last column: Here is my file:

enter image description here

Here is my code:

<?php

$in_file = 'gabriel-images-urls.csv';

$fd = fopen($in_file, "r");

$new_array[]= array();

$start_row = 2;

$i = 2;//define row count flag


while (($row = fgetcsv($fd)) !== FALSE) {
    for ($col=3; $col<=11; $col++) {         
        $row[] = $row[$col];
        if($i >= $start_row){
           $url = $row[$col];
           $url_name = basename($url);
           $new_array[] = [$row[0], $row[1],$url_name];
      }
   $i++;
 }
?>

So the idea is the variable $url_name to contain all the links from the above file with their basename and if it is posssible to be seperated with a comma in the array. Expected output:

     Array

    (
        [0] => G5651407J
        [1] => G5651407J
        [2] => https://assets.suredone.com/683987/media-photos/g5651407j-gabriel-g56514-front-right-ultra-premium-twin-tube-strut-for-lexus-toyota-models.jpg, https://assets.suredone.com/683987/media-photos/cfc869-bendix-premium-copper-cfc869-front-brake-pad-set-for-chrysler-sebring-models-2.jpeg, https://assets.suredone.com/683987/media-photos/cfc869-bendix-premium-copper-cfc869-front-brake-pad-set-for-chrysler-sebring-models-3.jpeg
    )

I want all of the links to be at the third element of the array, seperated with the comma.

4
  • please add your expected output ! Commented Oct 22, 2019 at 6:56
  • I added it in the post! Commented Oct 22, 2019 at 7:01
  • What have you tried so far to debug the given code? Small hint: are you sure you want to append everything to $new_array within that for loop? That might end in eight new elements per CSV line Commented Oct 22, 2019 at 7:35
  • Additionally, is that single array element the whole result for the given input? If yes, what's the rule behind that? Commented Oct 22, 2019 at 7:36

1 Answer 1

1
<?php

$in_file = 'gabriel-images-urls.csv';
$fd = fopen($in_file, "r");
$new_array= array();
$i=0;
while (($row = fgetcsv($fd)) !== FALSE)
{
    if($i!=0)
    {
        $url_array = array();
        for ($col=2; $col<=11; $col++) 
        {
            if(isset($row[$col]) && trim($row[$col])!=null)
            {
                $url = $row[$col];
                $url_array[] =$url;
            }

        }

        $new_array[] = array($row[0],$row[1],implode($url_array,','));
    }
    else if($i==0)
    {
        $i++;
    }
}

echo '<pre>';
print_r($new_array);
echo '</pre>';

?>

I have also optimized your code to stop incrementing row flag every time. Check if it works fine for you.

Sign up to request clarification or add additional context in comments.

12 Comments

Actually for the third element I only get commas, not the actual links.
can you share your csv
It is shared in the post.
[0] => 142386 [1] => 142386 [2] => ,,,,,,,,
This is my result.
|

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.