473,465 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Flattening Multidimensional array to unique values

If I have a multidimensional array like the following:

Array
$records =Array
0 =[Product] 30 year, [Rate]6.0;
1 =[Product] 30 year, [Rate]6.0;
2 =[Product] Pay Option, [Rate]1.0;
3 =[Product] Pay Option, [Rate]1.0;

How could I flatten this to achieve an array that only has unique Product
values, basically removing $records['1] and $records['3'] in this example?

--
Posted via a free Usenet account from http://www.teranews.com

Feb 12 '07 #1
1 9749
Rik
On Mon, 12 Feb 2007 18:43:26 +0100, Chuy08 <ch****@yahoo.comwrote:
If I have a multidimensional array like the following:

Array
$records =Array
0 =[Product] 30 year, [Rate]6.0;
1 =[Product] 30 year, [Rate]6.0;
2 =[Product] Pay Option, [Rate]1.0;
3 =[Product] Pay Option, [Rate]1.0;

How could I flatten this to achieve an array that only has unique Product
values, basically removing $records['1] and $records['3'] in this
example?
Why does you example not contain a multidimensional array? This one is
handled just fine by array_unique()...

You probably mean:
$records = array(
array('product' ='30 year','rate'=6.0),
array('product' ='30 year','rate'=6.0),
array('product' ='Pay option','rate'=1.0),
array('product' ='Pay option','rate'=1.0));

Quite some overhead, but workable (even preserves keys):
<?php
function array_unique_multi($array){
$copy = $array;
array_walk($array,create_function('&$v,$k','$v = serialize($v);'));
$array = array_unique($array);
return array_intersect_key($copy,$array);
}
?>

Be carefull not to have unserializable content in the array.
--
Rik Wasmus
Feb 12 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rob Tweed | last post by:
Probably a simple question but I can't find the answer anyway. Specifically, is it possible to copy a multidimensional array into the $_SESSION array - ie a deep clone of all keys and data? I...
5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
0
by: David Gersic | last post by:
I'm working with an HR system and trying to deal with an XML document that contains a bunch of personal data (unique to the person) and one or more sets of job data (a person can be hired more than...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
3
by: Claire | last post by:
I have a multidimensional array defined as private double myArray = new double; The first column of the array contains X values, the other contains Y values I have a charting function defined as...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
4
by: Chuy08 | last post by:
If I have a multdimensional array like so: $records = array( array('product' ='30 year','rate'=6.0 'ProgramID' =>9514), array('product' ='30 year','rate'=6.0 'ProgramID' =>9514),...
1
by: Chuy08 | last post by:
Currently I have the following XML document: <Programs> <Program> <ProductTypeName>30 Yr Fixed</ProductTypeName> <Rate>6.250</Rate> <APR>6.274</APR> <InvestorName>Helos</InvestorName>...
2
by: Snaggy | last post by:
I have a multidimensional array with a few entries I need to delete. Key and values are not unique in different sub arrays. As far as I know the filter function is not recursive and only deletes...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.