Jump to content

soyaslim

Members
  • Posts

    19
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

soyaslim's Achievements

Member

Member (2/5)

0

Reputation

1

Community Answers

  1. Hi @requinix , Thank you for your explanation and for taking the time to solve this problem. The whole recursive approach I really do not follow that method because I find it really confusing and hard, however, I am able to break it through my existing solution. Below I have simply break the loop if the `vehicle_info` `key` does not match $commonArray = []; $total = count($products) - 1; $vehicles = $products[0]; array_shift($products); foreach ($vehicles as $key => $vehicle) { foreach ($vehicle['vehicle_info'] as $ktype => $fitments) { $countKey = 0; // check all the products foreach ($products as $index => $product) { if (!isset($product[$key]['vehicle_info'])) { // key not found in this product, move on to next key break 2; } foreach ($product[$key]['vehicle_info'] as $kt => $fits) { if ($ktype == $kt) { $countKey++; if ($countKey == $total) { $commonArray[$key] = $product[$key]; } } } } } } print_r($commonArray);
  2. Hi @requinix Thanks for your feedback and suggestion and clean code. However, I need to check the vehicle info`key` which is `123`, `234` so on as well before adding to commonArray. I added the condition to your code like below. But it only check the `key` but not the vehicle info `key`. $commonArray = []; $first = array_shift($products); foreach ($first as $key => $vehicle) { $commonVehicle = []; foreach ($products as $product) { foreach ($product as $k => $pro) { // check if $key exists in all the other $products if ($key == $k) { // if so, build up the contents of $commonVehicle... $commonVehicle[$key] = $pro; } else { // if not, use continue (likely a continue 2; or continue 3;) to skip ahead of this loop here and go to the next $key continue 2; } } } if ($commonVehicle) { // only add this $key if there is something to add $commonArray[$key] = $vehicle; } } print_r($commonArray);
  3. Hello guys, How r we? I am trying to manipulate multi-dimension array to the desired array. So, I have this array $products that have three `elements`. Lets say the element as`product` so, three products. $products = [ [ 'test' => [ 'make' => 'test make', 'vehicle_info' => [ '123' => [ 'brake' => 'brake 123 value' ], '234' => [ 'brake' => 'brake 234 value' ] ] ], 'test2' => [ 'vehicle_info' => [ '456' => [ 'brake' => 'brake 456 value' ], '678' => [ 'brake' => 'brake 678 value' ] ] ] ], [ 'test' => [ 'make' => 'test make', 'vehicle_info' => [ '234' => [ 'brake' => 'brake 234 value' ] ] ], 'test2' => [ 'vehicle_info' => [ '456' => [ 'brake' => 'brake 456 value' ] ] ], 'test3' => [ 'vehicle_info' => [ '789' => [ 'brake' => 'brake 789 value' ] ] ] ], [ 'test' => [ 'make' => 'test make', 'vehicle_info' => [ '234' => [ 'brake' => 'brake 234 value' ] ] ], 'test3' => [ 'vehicle_info' => [ '789' => [ 'brake' => 'brake 789 value' ] ] ] ] ]; So, here is the desired output of the common array between 3 products. Since, the key `test` is common in all three products and `234` is common within `test` array in all three products. Array ( [test] => Array ( [make] => test make [vehicle_info] => Array ( [234] => Array ( [brake] => brake 234 value ) ) ) ) So, the solution I have works fine but I want to optimize my code or to improve it as much as possible. Please let me know if the steps are not clear. $commonArray = []; $total = count($products) - 1; $vehicles = $products[0]; foreach ($vehicles as $key => $vehicle) { foreach ($vehicle['vehicle_info'] as $ktype => $fitments) { $countKey = 0; // keep track of key exists in all products // loop through all the products foreach ($products as $index => $product) { if ($index == 0) // skip the first product continue; foreach ($product as $k => $pro) { if ($key == $k) { foreach ($pro['vehicle_info'] as $kt => $fits) { if ($ktype == $kt) { $countKey++; if ($countKey == $total) { // check if the key exists in all products $commonArray[$key] = $pro; } } } } } } } } print_r($commonArray);
  4. 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);
  5. 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 }
  6. 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.
  7. 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);
  8. You are exactly right! but how do I achieve this array
  9. 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);
  10. 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']]
  11. 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; } } }
  12. 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?
  13. 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; });
  14. 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'.
  15. $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.
×
×
  • 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.