?
Solved

Multidimensional Array with Duplicate Data

Posted on 2011-09-02
7
Medium Priority
?
324 Views
Last Modified: 2012-05-12
I have a multidimensional array which is generated using mysqli_fetch_assoc.

It looks like this:
myArray[x]['width']
myArray[x]['height']
myArray[x]['material']
myArray[x]['productId']

There are duplicates of both the width and height values in the array because each height/width are available for each material.

I need help displaying only unique width and height values.
0
Comment
Question by:mmoore500
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36476119
Please post the CREATE TABLE statement and the SELECT query, and post the PHP code that generates myArray.  Please tell us how many rows are selected by the query.  There may be more than one way to achieve your goals.  You might want to adjust the query with DISTINCT, or you might want to sanitize the array after you have created it.  Standing by, ~Ray
0
 
LVL 2

Author Comment

by:mmoore500
ID: 36476228
I didn't use a CREATE TABLE

Here is the SELECT statement:

SELECT products.productId, dimensions.width, dimensions.height, materials.material FROM products, dimensions, materials WHERE products.productId = dimensions.productId AND products.productId = materials.productId AND products.productId = '$productId'

Create array:
while($recordsData = mysqli_fetch_assoc($recordsResult))
{
      $myArray[] = $recordsData;
}

The amount of rows changes based on each product because there can be variations in available widths, heights, and materials. So I count the rows using mysqli_num_rows.

Distinct will still produce the same query result because the variations in the width, height, and materials columns.

I'm not sure what you mean by sanitize the array. I have been trying to work through this and have managed to create a new 2D array with only the height and width combos. Now I just need to remove the duplicates and it will be solved. Unless, you recommend another way.

I'd also like to say thank you for your help in advance. You have been helping me when I post questions for about 2 years.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36476329
I didn't use a CREATE TABLE - you can use the mysql_query SHOW CREATE TABLE to get the CREATE TABLE statement.  Somewhere, someone used it, believe me!

If you want to remove duplicates from an array, PHP can help.  Check this out:
http://us3.php.net/manual/en/function.array-unique.php
0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 2

Author Comment

by:mmoore500
ID: 36476374
I had already attempted to use the array_unique function, but it is removing all but one row. Here is what I did:

for($j = 0; $j < $recordsRows; $j++)
{
      $dimensionArray[$j] = array
      (
            "width" => $recordsArray[$j]['width'],
            "height" => $recordsArray[$j]['height'],
      );
}
                                          
$newArray = array_unique($dimensionArray);

The $newArray has only one record in it. It should have several unique records. Can you shed some light on what I am doing wrong?
0
 
LVL 2

Assisted Solution

by:mmoore500
mmoore500 earned 0 total points
ID: 36476433
I got it all figured out. Apparently array_unique does not work on multidimensional arrays.

Here is what I did:
$allDimensions = array();
foreach ($dimensionArray as &$v)
{
      if (!isset($allDimensions[$v['width']]))
             $allDimensions[$v['width']] =& $v;
}
                                          
foreach ($allDimensions as $o)
{
      echo '<br/>' . $o['width'] . ' x ' . $o['height'];
}
0
 
LVL 2

Author Closing Comment

by:mmoore500
ID: 36494099
Ray,

Thank you again for all your help over the past couple years. I pick this all up as I go. I attempt to do things properly, but it is pretty difficult for me to grasp it all. Thanks.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36476624
Thank you for your kind words.  Sorry it was in such a rush - I have to prepare for grandma's 80th birthday celebration tomorrow and could not stay at the computer.  All the best, ~Ray
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question