Solved

Multidimensional Array with Duplicate Data

Posted on 2011-09-02
7
321 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 110

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 110

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 110

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building a glossary into your website 9 54
How do I allow for non standard CSV file 9 41
Can't Find the Homepage on my site 4 42
sitemap.xml 3 23
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

738 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