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),
array('product' ='30 year','rate'=6.0 'ProgramID' =>9517),
array('product' ='Pay option','rate'=1.0 'ProgramID' =>9513),
array('product' ='Pay option','rate'=1.0 'ProgramID' =>9513));
How could I iterate through this and removing only values that are
duplicated by ProgramID. In this example the new array should contain only
the 1st, 3rd and 4th values. Any help appreciated.
chuy
--
Posted via a free Usenet account from http://www.teranews.com 4 2705
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Chuy08 wrote:
$records = array(
array('product' ='30 year','rate'=6.0 'ProgramID' =>9514),
array('product' ='30 year','rate'=6.0 'ProgramID' =>9514),
array('product' ='30 year','rate'=6.0 'ProgramID' =>9517),
array('product' ='Pay option','rate'=1.0 'ProgramID' =>9513),
array('product' ='Pay option','rate'=1.0 'ProgramID' =>9513));
How could I iterate through this and removing only values that are
duplicated by ProgramID.
Use a user-defined array sorting function, so the array gets sorted by
ProgramID. See php.net/usort .
- --
- ----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
No hay cosa más satisfactoria que el que alguien te dispare, y falle.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
iD8DBQFF0RG+R56dWuhgxGgRAthkAKCfMp+w261hlzKjCDLG+e n7fTAjqACeM4wa
0rnAPE1m2hwGslxn7OAVoS8=
=EtId
-----END PGP SIGNATURE-----
<?php
$records = array(
array('product' ='30 year','rate'=6.0, 'ProgramID' =>9514),
array('product' ='30 year','rate'=6.0, 'ProgramID' =>9514),
array('product' ='30 year','rate'=6.0, 'ProgramID' =>9517),
array('product' ='Pay option','rate'=1.0, 'ProgramID' =>9513),
array('product' ='Pay option','rate'=1.0, 'ProgramID' =>9513));
function remove_dups($array, $row_element) {
$new_array[0] = $array[0];
foreach ($array as $current) {
$add_flag = 1;
foreach ($new_array as $tmp) {
if ($current[$row_element]==$tmp[$row_element]) {
$add_flag = 0; break;
}
}
if ($add_flag) $new_array[] = $current;
}
return $new_array;
} // end function remove_dups
$a = remove_dups($records, 'ProgramID') ;
print_r ($a);
?>
Original function come from here: http://id2.php.net/manual/tw/
function.array-unique.php
-- http://ascii.mastervb.net - ASCII Art Generator http://anagram.mastervb.net - Anagram Generator http://www.mastervb.net/phpbooks - Best PHP Books
On Feb 13, 8:15 am, "Chuy08" <chu...@yahoo.comwrote:
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),
array('product' ='30 year','rate'=6.0 'ProgramID' =>9517),
array('product' ='Pay option','rate'=1.0 'ProgramID' =>9513),
array('product' ='Pay option','rate'=1.0 'ProgramID' =>9513));
How could I iterate through this and removing only values that are
duplicated by ProgramID. In this example the new array should contain only
the 1st, 3rd and 4th values. Any help appreciated.
chuy
--
Posted via a free Usenet account fromhttp://www.teranews.com
On Feb 13, 1:15 am, "Chuy08" <chu...@yahoo.comwrote:
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),
array('product' ='30 year','rate'=6.0 'ProgramID' =>9517),
array('product' ='Pay option','rate'=1.0 'ProgramID' =>9513),
array('product' ='Pay option','rate'=1.0 'ProgramID' =>9513));
How could I iterate through this and removing only values that are
duplicated by ProgramID. In this example the new array should contain only
the 1st, 3rd and 4th values. Any help appreciated.
chuy
--
Posted via a free Usenet account fromhttp://www.teranews.com
I won't suggest a sollution to what you are asking.
I'll tell you to try and fix the problem in a lower level.
How is this array gennerated ?
The vlaues that are Dups shoul be used as primary keys.
So create something like that $record[$productId][$rate][$programId]
That's how I work... it makes more sense from a Relational point of
view.
Chuy08 wrote:
$records = array(
array('product' ='30 year','rate'=6.0 'ProgramID' =>9514),
array('product' ='30 year','rate'=6.0 'ProgramID' =>9514),
array('product' ='30 year','rate'=6.0 'ProgramID' =>9517),
array('product' ='Pay option','rate'=1.0 'ProgramID' =>9513),
array('product' ='Pay option','rate'=1.0 'ProgramID' =>9513));
for ($i=count($records)-1; $i>=0; $i--)
$tmp[$records[$i]['ProgramID']] = $records[$i]
$records = array();
foreach ($tmp as $r) $records[] = $r;
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux
* = I'm getting there! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: TLOlczyk |
last post by:
I have a brain cramp and I need some help.
I have a chunk of code below which demonstrates
a problem I have with multidimensional arrays.
I want to keep it simple but something specific is...
|
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...
|
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,...
|
by: Stephen Preston |
last post by:
Trying to load an multidimensional array into a MySQL table with columns as
follows,
level1,level2,level3,illust,item,description,partNo,qua,price,remarks,weight
,size,mass
the array first...
|
by: |
last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily
complicated than the other languages I know. I feel this is because there
aren't many good official resources out there to...
|
by: David |
last post by:
Hi All,
Is there a way to remove an array entry completely from a multidimensional
array? I have successfully "emptied" the selected array but it leaves behind
an empty array entry.
For...
|
by: Chuy08 |
last post by:
If I have a multidimensional array like the following:
Array
$records =Array
0 = 30 year, 6.0;
1 = 30 year, 6.0;
2 = Pay Option, 1.0;
3 = Pay Option, 1.0;
How could I flatten this to...
|
by: Mike |
last post by:
Hi,
I have a multidimensional array that I would like to access as a hash
map for performance reasons. I cannot see anything in the STL (I mean
STL/TR1) that supports multi-dimensional hash...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
|
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,...
| |