Jump to content

soyaslim

Members
  • Posts

    16
  • Joined

  • Last visited

Everything posted by soyaslim

  1. I am currently working on a simple task yet a bit tricky. My task is to read all the images from the directory and write them into CSV file. So, when I read the directory I get the array list of images filename data will look like below: array ( '13763', '13763-1', '13763-2', '13764', '13765', '13765-1', ); I wanted this data to be exported into csv file which looks like below: So, I am going through the foreach loop as $image which gives me each element and each loop I am checking the string has '-' character or not. If it has no '-' character than I assign $currentId = $image but if it has '-' then I append $additionalImage .= ', '.$image.'.jpg'. But I have an issue when accessing the $additionalImage. Below is my code what I have done so far. $currentId = ''; $prevId = ''; $additionalImages = ''; $addId = ''; $counter = 0; $file = fopen('./images/M2_import_image.csv', 'w'); foreach ($images as $image) { if (strpos($image, '-') == FALSE) { if ($currentId != $image) { $mainImage = $image.'.jpg'; // print_r($additionalImages); fputcsv($file, array( $mainImage, $mainImage, $mainImage, $mainImage, $mainImage, $additionalImages, )); $additionalImages = ''; } $currentId = $image; $additionalImages = $image.'.jpg'; } if (strpos($image, '-') != FALSE) { $addId = substr($image, 0, strpos($image, '-')); $additionalImages .= ', '.$image.'.jpg'; } } fclose($file);
  2. Could u please shed a light on me? I really appreciate your help and the time u had already spent. So, I manage to change the images filenames array that I got from the directory, I first replace the extension '.jpg' and directory name '/images' and sorted the array as below: array ( '13763', '13763-1', '13763-2', '13764', '13765', '13765-1', ); So, I am going through the foreach loop as $image which gives me each element and each loop I am checking the string has '-' character or not. If it has no '-' character than I assign $currentId = $image but if it has '-' then I assign $additionalImage .= ', '.$image.'.jpg'. But the problem is I only want to insert one row if it has additional images '13763.jpg, 13763-1.jpg, 13763-2.jpg'. Below is my code what I have done so far. $currentId = ''; $additionalImages = ''; $addId = ''; foreach ($images as $image) { if (strpos($image, '-') == FALSE) { $currentId = $image; $additionalImages = $image.'.jpg'; } if (strpos($image, '-') != FALSE) { $addId = substr($image, 0, strpos($image, '-')); $additionalImages .= ', '.$image.'.jpg'; } // insert into csv file }
  3. Is there any way I can achieve this? I checked if the array data has "-" and matches the id but did not able to do it.
  4. I tried to check if the next array data has '-' or rolanId in the string but still could not achieve the array. foreach ($images as $image) { $filename = pathinfo($image); $filename = $filename['filename']; // 13763 $rolanId = substr($filename, 0, strpos($filename, '-')); // 13763 $additionalImages[] = $rolanId.'.jpg'; $csvData[$rolanId] = array( $rolanId.'.jpg', (strpos($filename, "-") != FALSE)? $filename : '', ); } print_r($csvData);
  5. You are exactly right! but how do I achieve this array
  6. Hello guys, I have been working on this simple project, however, seems a little bit tricky. So, I have an array which I want to write the data into csv file. The array below: Array ( [0] => 13763-1.jpg [1] => 13763.jpg [2] => 13763-2.jpg [3] => 13764-2.jpg [4] => 13764.jpg [5] => 13764-1.jpg [6] => 13765.jpg ) I sorted the array using rsort($array) so it looks like below: Array ( [0] => 13765.jpg [1] => 13764.jpg [2] => 13764-2.jpg [3] => 13764-1.jpg [4] => 13763.jpg [5] => 13763-2.jpg [6] => 13763-1.jpg ) So, I want to output this data into csv file like below: However, I could not able to achieve the additional images value in the CSV data. My code below: $file = fopen($directory."M2_import_image.csv", 'w'); // insert header to import file $header= array("product_id", "base_image", "small_image", "thumbnail_image", "swatch_image", "additional_images"); fputcsv($file, $header, ','); $images = glob($directory.'*.{jpg,png,gif,jpeg,JPG}', GLOB_BRACE); rsort($images); // natcasesort($images); $csvData = []; $counter = 0; foreach ($images as $image) { $filename = pathinfo($image); $filename = $filename['filename']; $rolanId = substr($filename, 0, strpos($filename, '-')); $additionalImages[] = $rolanId.'.jpg'; if (strstr($filename, $rolanId) != FALSE) { $counter++; } else if (strstr($filename, $rolanId) == FALSE) $counter = 0; if ($counter > 0) { $additionalImages[] = $filename.'.jpg'; } } print_r($additionalImages); fclose($file);
  7. Hey Kicken, actually the array 'supplier' value has a single quote like below Array ( [0] => Array ( [supplier] => 'ROLAN' [distance] => 3.8 ) so, the it does not match the following code as ROLAN is not equal to 'ROLAN' $supplierAvailable[$row['supplier']]['distance'] = $distanceMap[$row['supplier']]
  8. Hello guys, I have this array that I want the 'distance' key value to another array if this array 'supplier' matches the other array 'supplier' value. Array ( [0] => Array ( [supplier] => 'ROLAN' [distance] => 3.8 ) [1] => Array ( [supplier] => 'COOLDRIVE' [distance] => 6.2 ) [2] => Array ( [supplier] => 'ENGINEERING.' [distance] => 14.6 ) [3] => Array ( [supplier] => 'TEST DEPO' [distance] => 9.0 ) ) The other array is So, I just want to add 'distance' from the top array to below which I did already with the foreach loop. Array ( [0] => Array ( [supplier] => ENGINEERING [rolanID] => Array ( [0] => 51154 ) ) [1] => Array ( [supplier] => COOLDRIVE [rolanID] => Array ( [0] => 2338117 ) ) [2] => Array ( [supplier] => ROLAN [rolanID] => Array ( [0] => 2338117 [1] => 51154 ) ) ) So, Is there any best way than this below my code: Thank you foreach ($results as $row) { // this is second array loop $supplierAvailable[$row['supplier']]['supplier'] = $row['supplier']; $supplierAvailable[$row['supplier']]['rolanID'][] = $row['rolan_part_id']; foreach ($supplierDetails as $index => $depo) { // this is the first array loop to add distance to second array if ($depo['supplier'] == "'".$row['supplier']."'") { $supplierAvailable[$row['supplier']]['distance'] = $depo['distance']; unset($depo[$index]); break; } } }
  9. Thanks a tons Brand, after I reviewed my code I found that I skip '&' in the loop initialization. It's working perfectly and is awesome. If there is anything I can contribute as my appreciation please let me know. Also, would u recommend me php guidelines?
  10. Sorry Barand, I actually sorted by 'itemCount' before this so I sorted the array first by following code: usort($test, function ($a, $b) { if ($a['itemCount'] > $b['itemCount']) { return -1; } else return 1; });
  11. Hi Barand, Could u shed a light on me? I used ur code with another case of data but there was a duplicate rolanID in one of the array. $test = array( array( "supplier" => "ROLAN", "rolanID" => array(456), "itemCount" => 1, ), array( "supplier" => "ROLAN", "rolanID" => array(123, 234), "itemCount" => 2, ), array( "supplier" => "TEST DEPO", "rolanID" => array(123, 234, 456), "itemCount" => 3, ), array( "supplier" => "DIFFERENT DEPO", "rolanID" => array(897, 487, 100), "itemCount" => 3, ), array( "supplier" => "TEST2 DEPO", "rolanID" => array(456, 188, 200, 123), "itemCount" => 4, ) ); So, I tried using this data and the output was below using ur code. Array ( [0] => Array ( [supplier] => TEST2 DEPO [rolanID] => Array ( [0] => 456 [1] => 188 [2] => 200 [3] => 123 ) [itemCount] => 4 ) [1] => Array ( [supplier] => DIFFERENT DEPO [rolanID] => Array ( [0] => 897 [1] => 487 [2] => 100 ) [itemCount] => 3 ) [2] => Array ( [supplier] => TEST DEPO [rolanID] => Array ( [0] => 123 [1] => 234 [2] => 456 ) [itemCount] => 3 ) ) Since the supplier 'TEST2 DEPO' has already rolanID '123' so, is it possible to remove the duplication in any other supplier? For example in this case, the rolan ID '123' is duplicated in last element of array supplier 'TEST DEPO'.
  12. $finalArray[] = $test[0]; for ($i=1; $i<count($test); $i++) { $id = array_diff($test[$i]['rolanID'], $test[0]['rolanID']); if (!empty($id)) { array_push($finalArray, array( 'supplier' => $test[$i]['supplier'], 'rolanID' => $id, 'address' => $test[$i]['address'], 'itemCount' => count($id), )); } } This is how I tried but it does not work in some cases. For example, it only checks the first element of array with others. So, there will be multiple arrays with same rolanID that doesnot exists in first element of array.
  13. Hello guys, I have a multidimensional array and I want to change it to my final array. Below is the array I have. $test = array( array( "supplier" => "TEST DEPO", "rolanID" => array(123, 234, 456), "itemCount" => 3 ), array( "supplier" => "ANOTHER DEPO", "rolanID" => array(123, 786, 345), "itemCount" => 3 ), array( "supplier" => "ROLAN", "rolanID" => array(123, 234), "itemCount" => 2 ), ); So, with this array I want to output like the below array. As we can see, if all rolanID is equal to one of the supplier then I don't want to add that array into the final array. For, an example the supplier "ROLAN" array is removed completely in the final array as its all rolanID is in the supplier "TEST DEPO" but the supplier "ANOTHER DEPO" has some rolanID that is not in the supplier "TEST DEPO" so, it is included that in the final array. Array ( [0] => Array ( [supplier] => TEST DEPO [rolanID] => Array ( [0] => 123 [1] => 234 [2] => 456 ) [itemCount] => 3 ) [1] => Array ( [supplier] => ANOTHER DEPO [rolanID] => Array ( [0] => 786 [1] => 345 ) [itemCount] => 2 ) )
  14. Seriously, it has to be this simple I have been spending my whole day in this loop. I really appreciate this and thank u for saving my day. God bless u!
  15. for ($i=0; $i<count($results)-1; $i++) { $productID = ($results[0]['supplier'] == $results[$i+1]['supplier']) ? array($results[$i]['rolan_part_id'], $results[$i+1]['rolan_part_id']) : array($results[$i+1]['rolan_part_id']); $finalArray[$results[$i]['supplier']] = array( "productID" => $productID, ); } print_r($finalArray); so, the $results contain the array I want to change, which is three nested arrays. The above output is: Array ( [COOLDRIVE DISTRIBUTION] => Array ( [productID] => Array ( [0] => 2338117 ) ) [ROLAN] => Array ( [productID] => Array ( [0] => 51154 ) ) ) I couldn't get the other productID in 'ROLAN' array.
  16. Hello guys. I want the array to be processed into my final array how am I supposed to achieve this. Below is the array I want to process. Array ( Array ( [part_id] => 2338117 [supplier] => COOLDRIVE DISTRIBUTION [quantity] => 12 ) Array ( [part_id] => 2338117 [supplier] => ROLAN [quantity] => 20 ) Array ( [part_id] => 51154 [supplier] => ROLAN [quantity] => 20 ) ) This is the final array I want. Array ( [COOLDRIVE DISTRIBUTION] => Array ( [proudctID] => Array ( [0] => 2338117 ) ) [ROLAN] => Array ( [productID] => Array ( [0] => 2338117 [1] => 51154 ) ) )
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.