2

I am working a form where i have to adopt associative array technique. the user will select a product size and enter the corresponding stock. once post the corresponding stock will be associated to the corresponding size value. Here is the form:

   <form action="test1.php" method="post">
    <p>Choose a Size or more and enter its Stock</p>
    Size :<select name="articleSize[]" id="articleSize[]">
        <option value="">Select a size</option>
        <option value="xl">XL</option>
        <option value="32">32</option>
        <option value="unique">Unique</option>
        <option value="xxl">XXL</option>
    </select>
    Stock : <input type="text" name="articleStock[]" id="articleStock[]" size="8" id="stock" value="">

    <p>Choose a Size or more and enter its Stock (optional)</p>
    Size :<select name="articleSize[]" id="articleSize[]">
        <option value="">Select a size</option>
        <option value="xl">XL</option>
        <option value="32">32</option>
        <option value="unique">Unique</option>
        <option value="xxl">XXL</option>
    </select>
    Stock : <input type="text" name="articleStock[]" id="articleStock[]" size="8" id="stock" value="">
    <p>
    <input type="submit" name="submit" id="submit" value="submit">
    </p>
    </form>

I have tried myself based on what i know and here is my code:

if (isset($_POST['submit'])) {
$arrSize = $_POST['articleSize'];
$arrStock = $_POST['articleStock'];

foreach ($arrSize as $size_key => $sizeValue) {
    foreach ($arrStock as $stock_Key => $stockVal) {
        if ($size_key == $stock_Key) {
            $newArray[$sizeValue] = $stockVal;
        }
    }

 }
 if (count($newArray) >=1) { 
     foreach ($newArray as $key => $value) {
        echo "Size is ".$key. " and stock is ".$value;
     }
  }

}

My code is working as excepted but is't there better way to do it?

The result shoud be like.

$stock_size = array(the size=> its corresponding stock);
0

2 Answers 2

1

You can eliminate inner foreach like this:

foreach ($arrSize as $size_key => $sizeValue) {
    if (!empty($arrStock[$size_key])) {
        $newArray[$sizeValue] = $arrStock[$size_key];
    }
}

If there's a need to count 0 values too you can:

foreach ($arrSize as $size_key => $sizeValue) {
    if (!empty($arrStock[$size_key]) && (int)$arrStock[$size_key] >=0 ) {
        $newArray[$sizeValue] = $arrStock[$size_key];
    }
}

And if you're sure that all values have pairs:

foreach ($arrSize as $size_key => $sizeValue) {
    $newArray[$sizeValue] = $arrStock[$size_key];
}

which is the same as

$newArray = array_combine($arrSize, $arrStock);

Update with function:

function makePairs($arrSize, $arrStock)
{
    $newArray = [];

    foreach ($arrSize as $size_key => $sizeValue) {
        if (!empty($arrStock[$size_key])) {
            $newArray[$sizeValue] = $arrStock[$size_key];
        }
    }

    return $newArray;
}

// Call like
print_r(makePairs($_POST['articleSize'], $_POST['articleStock']));
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your advise...it working like i wanted...i did not want to return an array with empty values included..
how can i translate the above as a function ? because the return value as to be a function?
Please ask another question, I don't understand what it means translate as a function.
i was saying that how can i successfully take above code and make it as a function. so the function will return false if no value has been enter or return array if values has been entered.
0

You can use array_combine which will create key-value pair array

$arrSize = $_POST['articleSize'];
$arrStock = $_POST['articleStock'];

$stock_size = array_combine ( $arrSize ,$arrStock );

Comments

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.