Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP Arrays Issue

Hello,

I'm trying to make a list of filter options for products on a website using php arrays, but I have an issue I can't seem to work out. Lets say on the products listing page there is a option to only show products which have a certain weight, for example:

<ul class="filter">
<li><span>Weight</span></li>
<li><a>10</a></li>
<li><a>12</a></li>
<li><a>14</a></li>
</ul>

And so on, and the products array looking something like this:

$products = array(
    "Product1" => array(
        "name" => "Product1",
        "weight" => 10
    ),
    "Product2" => array(
        "name" => "Product2",
        "weight" => 10
    ),
    "Product3" => array(
        "name" => "Product3",
        "weight" => 12
    ),
    "Product3" => array(
        "name" => "Product3",
        "weight" => 14
    )
);

I can loop through the array and return the weights of each product to use in the list, like:

foreach($products as $product_id => $product) {
  if(isset($product["weight"])) {
    echo $product["weight"];
  }
}

And that works okay, except if there is two products in the array which have the same weight, it will echo it out, so it may end up with the same filter option twice in the list if two different products have the same weight.

I was wondering if anyone knew of a way I could limit it to one weight echoed out if there is two duplicate ones or if there is a string function I can use to accomplish this?

Thanks, Adam

4 Answers

Try taking a look at `array_unique(). http://php.net/manual/en/function.array-unique.php

What I might try doing is loop through with your foreach like you are, but instead I would run the results through the array_unique function, and than echo the results of that.

I had to use array_unique on a Wordpress site, where I was looping through some custom taxonomies trying to get the post ID. Problem was the various taxonomies I was looping could return the same post ID multiple times, which would output duplicate post titles, so I had to use this function to trim out the duplicate post ID's and it worked like a charm.

Hello,

Thanks for the reply,

Can you please advise where I actually write array_unique? Do I put it in the foreach loop or?

If I do:

foreach($products as $id => $value) {
    if (isset($value["weight"])) {
        echo array_unique($value["weight"]);
    }
}   

I get an error, where exactly do I put it in the code?

Thanks

I'm new to PHP, so I don't know if a language-specific way to do this. However, from a general programming standpoint, I think you could use a flag to stop echoing once one is found:

$found = false;

foreach($products as $product_id => $product) {
  if(isset($product["weight"]) AND !$found) {
    echo $product["weight"];
    $found = true;
  }
}

Thanks for the reply,

I can only get the first weight echoed out, but I think i've figured it out by looping through the products and pushing them into a temp array and checking that no duplicate values are added to the array

Sorry I think I fundamentally misunderstood your question haha.

The problem you have is the way that you are writing the array.....

Your Current Code looks like this:

$products = array(
    "Product1" => array(
        "name" => "Product1",
        "weight" => 10
    ),
    "Product2" => array(
        "name" => "Product2",
        "weight" => 10
    ),
    "Product3" => array(
        "name" => "Product3",
        "weight" => 12
    ),
    "Product3" => array(
        "name" => "Product3",
        "weight" => 14
    )
);

However, it will be easier if you do it like this:

$products[] = array(
  "name" => "Product1",
  "weight" => 10
),
$products[] = array(
  "name" => "Product2",
  "weight" => 10
),
$products[] = array(
  "name" => "Product3",
  "weight" => 12
);

By doing it the way that I have shown you above, you can now use the foreach loop like this:

foreach($products as $product){
  echo $product["weight"];
}

Hello,

Thanks for the advice, seems a lot easier :)

no problem, Hope i was able to answer your question :)