[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Multidimensional Array with Duplicate Data

Posted on 2011-09-02
7
Medium Priority
?
328 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

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…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Suggested Courses
Course of the Month19 days, 16 hours left to enroll

873 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