Home > OS >  Unable to assign value from Array 1 to Array 2 due to Undefined index
Unable to assign value from Array 1 to Array 2 due to Undefined index

Time:08-29

$new_stock_array has the value:

$new_stock_array = Array
(
    "VRX1600/380" => 0,
    "NEFOM" => 1,
    "PZ1000-840" => 4,
    "CU1000TNG" => 1,
    "SST-91" => 0,
    "BEER3" => 12,
    "L09125N" => 1,
    "B15GFA" => 0,
    "IBM20" => 8,
    "FGDG1500LS-3" => 0,
    "BD550S" => 2,
    "GNX1400TNG" => 1,
    "SH3000-800" => 6,
    "SLX3100TN" => 1,
    "FGOR1000LC" => 7,
    "ITR50" => 2,
    "SH2000-800" => 1,
    "FGOR1200LC" => 1,
    "TRAYS66LGLASS" => 1,
    "B215G" => 1,
    "SD520Q" => 7,
    "ST1200-NG" => 1,
    "BD650S" => 6,
    "GFC400-LPG" => 1,
    "CH300-LPG" => 1,
    "B318G" => 5,
    "CST400-LPG" => 1,
    "TC12" => 2,
    "STAND6LTRAYS" => 2,
    "SLX2100TN" => 0,
    "STGR300" => 4,
    "SH2000-700" => 5,
    "SLX4100TN" => 0,
    "BTS120L" => 16,
    "HX-1S" => 1,
    "PAX2100TNG" => 2,
    "BTS160L" => 19,
    "GNX1400TN" => 1,
    "VRX1200/380" => 2,
    "GFC400-NG" => 1,
    "GN400TNV" => 2,
    "CST400-NG" => 1,
    "AGGF-3-NG" => 14,
    "CGR400-NG" => 1,
    "GNX2100TNG" => 2,
    "GNX2100TN" => 2,
    "BC05" => 2,
    "ST1200-LPG" => 2,
    "GR300-LPG" => 2,
    "AGGF-3-LPG" => 0,
    "CGR400-LPG" => 2,
    "TRAYS6GLASS" => 3,
    "BEER3SS" => 3,
    "GNX650TN" => 3,
    "GNX4100TNG" => 3,
    "PAX3100TNG" => 2,
    "SH3000-700" => 3,
    "STAND6TRAYS" => 3,
    "TRAYS6LGLASS" => 3,
    "B1400TNV" => 2,
    "PZ2000-840" => 3,
    "4OVST-LPG" => 3,
    "AGGF-5-LPG" => 3,
    "ST600-NG" => 3,
    "CH600-NG" => 3,
    "WS540BTG" => 3,
    "BASE300" => 3,
    "SP18G" => 4,
    "CH900-NG" => 4,
    "WS1900BTG" => 4,
    "BASE1200" => 4,
    "BD700S" => 4,
    "GNX2100BT" => 5,
    "IBM30" => 5,
    "PZ3000-840" => 6,
    "CH600-LPG" => 6,
    "6OVST-LPG" => 5,
    "GNX3100TN" => 6,
    "GNX3100BT" => 6,
    "GNX3100TNG" => 5,
    "GNX4100TN" => 8,
    "DSA420TGO" => 9,
    "AGGF-4-LPG" => 7,
    "EASY50" => 9,
    "AGGF-4-NG" => 9
);

Here is a minimal excerpt of $all_rows:

$all_rows = array(
0 => Array
    (
        "web-scraper-order" => "web-scraper-order",
        "web-scraper-start-url" => "web-scraper-start-url",
        "top_category_links" => "top_category_links",
        "top_category_links-href" => "top_category_links-href",
        "product_links" => "product_links",
        "product_links-href" => "product_links-href",
        "title" => "title",
        "mpn" => "mpn",
        "rrp_price_inc" => "rrp_price_inc",
        "stock" => "stock",
        "desc" => "desc",
        "primary_img_large" => "primary_img_large",
        "breadcrumb_categories" => "breadcrumb_categories",
        "stock_commentary" => "stock_commentary",
        "next_page" => "next_page",
        "next_page-href" => "next_page-href",
        "rrp_price_ex" => "rrp_price_ex",
        "full_img" => "full_img",
        "grouped_src_3_loaded_only" => "grouped_src_3_loaded_only",
        "grouped_src_3_loaded_only-src" => "grouped_src_3_loaded_only-src",
        "ETA_label" => "ETA_label",
        "pagination_gallery" => "pagination_gallery",
        "div_href_img" => "div_href_img",
        "manual" => "manual",
        "collected_images" => "collected_images"
    ),

1 => Array
    (
        "web-scraper-order" => "1661506781-1480",
        "web-scraper-start-url" => "https://www.agequipment.com.au/",
        "top_category_links" => "",
        "top_category_links-href" => "https://www.agequipment.com.au/food-processing/",
        "product_links" => "12 Inch (300mm) Meat Slicer",
        "product_links-href" => "https://www.agequipment.com.au/12inch-300mm-meat-slicer/",
        "title" => "12 Inch (300mm) Meat Slicer",
        "mpn" => "VRX1600/380",
        "rrp_price_inc" => "999.00",
        "stock" => "PRE ORDER",
        "desc" => "",
        "primary_img_large" => "",
        "breadcrumb_categories" => "",
        "stock_commentary" => "Pre-Order now for next shipment 9/9/2022",
        "next_page" => "",
        "next_page-href" => "",
        "rrp_price_ex" => "$908.18",
        "full_img" => "https://www.agequipment.com.au/pub/media/catalog/product/cache/10fe8d9df2d769f1f80c003a45c2fe8b/1/0/10x.jpg",
        "grouped_src_3_loaded_only" => "",
        "grouped_src_3_loaded_only-src" => "",
        "ETA_label" => "ETA 9/9/2022",
        "pagination_gallery" => "",
        "div_href_img" => "",
        "manual" => "",
        "collected_images" => ""
    ),

2 => Array
    (
        "web-scraper-order" => "1661576256-1882",
        "web-scraper-start-url" => "https://www.agequipment.com.au/",
        "top_category_links" => "",
        "top_category_links-href" => "https://www.agequipment.com.au/gas/",
        "product_links" => "Commercial Gas Fryer - 3 Burner (LPG)",
        "product_links-href" => "https://www.agequipment.com.au/commercial-gas-fryer-3-burner-lpg./",
        "title" => "Commercial Gas Fryer - 3 Burner (LPG)",
        "mpn" => "AGGF-3-LPG",
        "rrp_price_inc" => "1,599.00",
        "stock" => "Out Of Stock",
        "desc" => "",
        "primary_img_large" => "",
        "breadcrumb_categories" => "",
        "stock_commentary" => "Pre-Order now for next shipment, arriving in several weeks",
        "next_page" => "",
        "next_page-href" => "",
        "rrp_price_ex" => "$1,453.64",
        "full_img" => "https://www.agequipment.com.au/pub/media/catalog/product/cache/10fe8d9df2d769f1f80c003a45c2fe8b/a/g/aggf-3-6_1.jpg",
        "grouped_src_3_loaded_only" => "",
        "grouped_src_3_loaded_only-src" => "",
        "ETA_label" => "",
        "pagination_gallery" => "",
        "div_href_img" => "",
        "manual" => "",
        "collected_images" => ""
    )
);

Here is the loop I'm trying to run to take stock values $val1 from the first array and assign them to $row['mpn'] in the second array:

foreach ($new_stock_array as $key1 => $val1){
foreach ($all_rows as &$row) {

  $row['stock_count'] = '';

   if ($key1 == $row['mpn'] ){
     $row['stock_count'] = $val1;
   }
}
}

The result is that $row['stock_count'] = ''; is added to the $all_rows array, but the stock value $val1 from the $new_stock_array is not able to be assigned to $row['stock_count'] when such a value exists.

Of relevance or not - it is unclear - but I am also getting the notice:

Notice: Undefined index: mpn in /opt/lampp/htdocs/agequipment/reconsolidate_img_arrays.php on line 322

CodePudding user response:

I was getting

Notice: Undefined index: mpn in /opt/lampp/htdocs/agequipment/reconsolidate_img_arrays.php on line 322

because in the real data (as opposed to the minimal data excerpt given here) some data were not set as the data had blank data cells.

CodePudding user response:

A foreach by reference is known to cause issues, best to avoid.
Your nested loop sets $row['stock_count'] = ''; on every row in each iteration of the outer loop. This will result in only the row with the last mpn in $new_stock_array having a different value. You only need to loop through $all_rows once and use the mpn of that row to look up the value $new_stock_array[$row['mpn']]. Check to make sure the array keys exist before assigning values.

foreach ($all_rows as $r_key => $row) {
  $all_rows[$r_key]['stock_count'] = (array_key_exists('mpn', $row) && array_key_exists($row['mpn'], $new_stock_array)) ?
    $new_stock_array[$row['mpn']] : '';
}

See it in action: https://onlinephp.io/c/20ebc

  • Related