Solved

Multidimensional Array with Duplicate Data

Posted on 2011-09-02
7
318 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
  • 4
  • 3
7 Comments
 
LVL 109

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 109

Accepted Solution

by:
Ray Paseur earned 500 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 109

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 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