By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

Multidimensional array remove values

P: n/a
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

Feb 13 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
-----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-----
Feb 13 '07 #2

P: n/a
<?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

Feb 13 '07 #3

P: n/a
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.

Feb 13 '07 #4

P: n/a
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!
Feb 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.