1

I have the following array for tire sizes:

    Array
    (
        [0] => 155
        [1] => 70
        [2] => 13
    )

    Array
    (
        [0] => 155
        [1] => 80
        [2] => 13
    )

    Array
    (
        [0] => 165
        [1] => 65
        [2] => 14
    )

    Array
    (
        [0] => 175
        [1] => 65
        [2] => 14
    )

    Array
    (
        [0] => 175
        [1] => 70
        [2] => 13
    )

    Array
    (
        [0] => 175
        [1] => 70
        [2] => 14
    )

and so on. Now I am creating a drop down so people can select the tire size they are searching for. so here is my PHP code:

include 'database.php';
$result = $mysqli->query('SELECT DISTINCT SKU_SIZE FROM SKU_DATA WHERE SKU_BRANDNAME = "'.$brand.'" ORDER  BY SKU_SIZE');

while( $row = $result->fetch_assoc()){
    $sku_size = $row['SKU_SIZE'];
    $chars = preg_split('/[^\d]/', $sku_size, -1, PREG_SPLIT_NO_EMPTY);

    echo "<option>".$chars[0]."</option>";

}

Now that code is just showing the first number in each array, for the very first drop down they select.

Right now it is showing 155, 155, 165, 175, 175 - and what I want it to do is just show the unique values so it would just show 155, 165, 175.

Update: Thanks! I got that part working. One quick question.. the order is not quite right, not sure what I am doing wrong. Here is a preview:

enter image description here

5 Answers 5

2

Create an array and check to see if each value is in the array before outputting it. If it is not in the array, add it in before outputting.

include 'database.php';
$result = $mysqli->query(
    'SELECT DISTINCT SKU_SIZE 
    FROM SKU_DATA WHERE SKU_BRANDNAME = "'.$brand.'" 
    ORDER  BY SKU_SIZE'
);

$seen = array();

while( $row = $result->fetch_assoc()){
    $sku_size = $row['SKU_SIZE'];
    $chars = preg_split('/[^\d]/', $sku_size, -1, PREG_SPLIT_NO_EMPTY);

    if(in_array($chars[0], $seen))
        continue;

    $seen[] = $chars[0];
    echo "<option>".$chars[0]."</option>";

}
Sign up to request clarification or add additional context in comments.

2 Comments

I like this solution best because there isn't a second loop required.
Depending on what the OP wants to do with the rest of his script, it may make good sense to have two loops, although he did not specify it in his post. This is true especially if he wants to use the results of the query again later on.
2

You can remove any duplicate unique items from an array using the array_unique() function.

EG:

$arrays = array(1,2,3) + array(1,2,3);

print_r(array_unique($arrays));

// Will print just: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )

Comments

1

use this:

while( $row = $result->fetch_assoc()){
    $sku_size = $row['SKU_SIZE'];
    $chars = preg_split('/[^\d]/', $sku_size, -1, PREG_SPLIT_NO_EMPTY);

    $sizes[$chars[0]] = true;
}

ksort($sizes, SORT_NUMERIC);

foreach ($sizes as $size => $tmp){
    echo "<option value=\"$size\">$size</option>";
}

3 Comments

Perfect, thank you! If you don't mind, I just updated my answer can you see what's going on with the ordering?
To make the order correct, change the line to foreach (sort($sizes, SORT_NUMERIC) as $size => $tmp){
added sorting to code. p.s. you have to use ksort in my case, as sizes are keys.
1

Use a temporary array to store the numbers that have been echoed then.

Comments

1

Make a new array with just the first value

$diameter = array();
foreach ($tires as $tire) {
    $diameter[] = $tire[0];
}

Then, use array_unique() to remove the duplicates, or only add them to $diameter if they are not already in there.

Then use that $diameter array to create the dropdown.

This has the advantage that you can also sort the $diameter array.

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.