Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7518083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:39:14+00:00 2026-05-30T01:39:14+00:00

I currently have two arrays that look like this: Swatches: Array ( [0] =>

  • 0

I currently have two arrays that look like this:

Swatches:

Array
(
    [0] => Array
        (
            [swatch_id] => 8
            [swatch_file] => orange_swatch.jpg
        )

    [1] => Array
        (
            [swatch_id] => 9
            [swatch_file] => pink_swatch.jpg
        )

    [2] => Array
        (
            [swatch_id] => 10
            [swatch_file] => green_swtach.jpg
        )

)

Selected Swatches:

Array
(
    [0] => Array
        (
            [swatches_has_products_id] => 18
            [swatches_swatch_id] => 8
            [products_product_id] => 19
        )

    [1] => Array
        (
            [swatches_has_products_id] => 19
            [swatches_swatch_id] => 10
            [products_product_id] => 19
        )

)

I am trying to check a check box if $swatch['swatch_id'] is equal to $selected_swatches['swatches_swatch_id']. I am doing this with the following code:

<?php foreach ($swatches as $k => $swatch): ?>
    <li>
        <img src="<?php echo base_url(); ?>media/images/swatches/<?php echo $swatch['swatch_file']; ?>" height=""/>
        <input type="checkbox" name="product_has_swatch[]" value="<?php echo $swatch['swatch_id']; ?>" <?php    if($swatch['swatch_id'] == $selected_swatches[$k]['swatches_swatch_id']) : ?> checked="checked" <?php endif; ?> />
    </li>
<?php endforeach; ?>

However I get the following error, if there are no matches:

A PHP Error was encountered
   Severity: Notice
    Message: Undefined offset: 2
   Filename: products/create.php
Line Number: 137

Line 137 is the if to check if I have matches; Where am I going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:39:16+00:00Added an answer on May 30, 2026 at 1:39 am

    One solution is:

    // make a new array of selected ids
    $newArr = array();
    foreach($selected_swatches as $val) {
        array_push($newArr, $val['swatches_swatch_id']);
    }
    
    // then check with in_array, like:
    <?php foreach ($swatches as $k => $swatch): ?>
        <li>
            <img src="<?php echo base_url(); ?>media/images/swatches/<?php echo $swatch['swatch_file']; ?>" height=""/>
            <input type="checkbox" name="product_has_swatch[]" value="<?php echo $swatch['swatch_id']; ?>" <?php if(in_array($swatch['swatch_id'], $newArr)) : ?> checked="checked" <?php endif; ?> />
        </li>
    <?php endforeach; >
    

    Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have two arrays one the stores a file name and another that
I (currently) have two forms, one that needs to call the other. In other
All, I currently have two projects that are under SourceSafe that I am unable
I currently have an asp.net website hosted on two web servers that sit behind
I have two arrays of x - y coordinates, and I would like to
I am currently using markercluster plugin with jquery ui maps. I have two arrays
I'm having trouble understanding how to solve this issue, essentially I have two arrays
I have two arrays, The $first has 5000 arrays inside it and looks like:
I have a site that dynamically creates two arrays of Lat/Long values based on
I currently have a C function that I'd like to use in .NET (C#).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.