Solved

Multidimensional Array with Duplicate Data

Posted on 2011-09-02
7
313 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 108

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 108

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 108

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
This video teaches users how to migrate an existing Wordpress website to a new domain.

760 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now