[thelist] Array element replacements...
jsWalter
jsWalter at torres.ws
Fri Aug 13 15:30:24 CDT 2004
I have 2 arrays (see below), $data and $report.
I would like to loop through $report and find each matching string (key)
in $data and replace the string in $report with the value of $data.
Now, $data has many "records", so this loop much run through the entire
length of $data.
I image that this will create a third array (see below for example),
$results.
I've spent the last few days on this.
I have a method that will look for a particular value in an array and
replace that value, in fact it works in multi-dim arrays as well (see
farther below).
I just can't seem to get my head around this.
Anyone already solve this?
Thanks
Walter
==============
$report = Array
(
[0] => Array
(
[0] => Description
[1] => Shares
[2] => NAV
[3] => Amount
)
[1] => Array
(
[0] => Beginning Net Asset Value
[1] => PTD BB Shares
[2] => PTD BB NAV
[3] => PTD BB Amount
)
[2] => Array
(
[0] => Additions
[1] => PTD Cont Shares
[2] => PTD Cont NAV
[3] => PTD Cont Amount
)
)
==============
$data = Array
(
[100] => Array
(
[Investor #] => 100
[PTD BB Amount] => 1,060,861.00
[PTD BB NAV] => 250
[PTD BB Shares] => 4,243.44
[PTD Cont Amount] => -
[PTD Cont NAV] => -
[PTD Cont Shares] => -
[PTD NI Amount] => -23,073.47
[PTD NI NAV] => -5.44
[PTD WD Amount] => -
[PTD WD NAV] => -
[PTD WD Shares] => -
[PTD EB Amount] => 1,037,787.53
[PTD EB NAV] => 244.56
[PTD EB Shares] => 4,243.44
[PTD RoR] => -217.50%
[YTD BB Amount] => -
[YTD BB NAV] => -
[YTD BB Shares] => -
[YTD Cont Amount] => 1,060,861.00
[YTD Cont NAV] => 250
[YTD Cont Shares] => 4,243.44
[YTD NI Amount] => -23,073.47
[YTD NI NAV] => -5.44
[YTD WD Amount] => -
[YTD WD NAV] => -
[YTD WD Shares] => -
[YTD EB Amount] => 1,037,787.53
[YTD EB NAV] => 244.56
[YTD EB Shares] => 4,243.44
[YTD RoR] => -217.50%
)
[101] => Array
(
[Investor #] => 101
[PTD BB Amount] => 200,000.00
[PTD BB NAV] => 250
[PTD BB Shares] => 800
[PTD Cont Amount] => -
[PTD Cont NAV] => -
[PTD Cont Shares] => -
[PTD NI Amount] => -4,349.95
[PTD NI NAV] => -5.44
[PTD WD Amount] => -
[PTD WD NAV] => -
[PTD WD Shares] => -
[PTD EB Amount] => 195,650.05
[PTD EB NAV] => 244.56
[PTD EB Shares] => 800
[PTD RoR] => -217.50%
[YTD BB Amount] => -
[YTD BB NAV] => -
[YTD BB Shares] => -
[YTD Cont Amount] => 200,000.00
[YTD Cont NAV] => 250
[YTD Cont Shares] => 800
[YTD NI Amount] => -4,349.95
[YTD NI NAV] => -5.44
[YTD WD Amount] => -
[YTD WD NAV] => -
[YTD WD Shares] => -
[YTD EB Amount] => 195,650.05
[YTD EB NAV] => 244.56
[YTD EB Shares] => 800
[YTD RoR] => -217.50%
)
)
==============
$results = Array
(
[100] => Array
(
[0] => Array
(
[0] => Description
[1] => Shares
[2] => NAV
[3] => Amount
)
[1] => Array
(
[0] => Beginning Net Asset Value
[1] => 4,243.44
[2] => 250
[3] => 1,060,861.00
)
[2] => Array
(
[0] => Additions
[1] => -
[2] => -
[3] => -
)
)
)
[101] => Array
(
[0] => Array
(
[0] => Description
[1] => Shares
[2] => NAV
[3] => Amount
)
[1] => Array
(
[0] => Beginning Net Asset Value
[1] => 800
[2] => 250
[3] => 200,000.00
)
[2] => Array
(
[0] => Additions
[1] => -
[2] => -
[3] => -
)
)
)
==============
==============
/**
* Search/replace in an array recursively
*
* This function will search an array recursively
* till it finds what it is looking for, then it will
* replace that item with the given string. An array
* within an array within an array within array.
* This will replace ALL occurrences, not just the first
*
* @author Walter Torres <phpWalter at torres.ws>
* @based_on Richard Sumilang <richard at richard-sumilang.com>
* @param string $needle What are you searching for?
* @param array $haystack by reference - What you want to
search in
* @param array $newValue new value to replace with
* @return boolean $match was anything found/changed
* @access public
*/
function array_search_replace($needle, &$haystack, $change)
{
// Default value
$match = false;
foreach($haystack as $key => $value)
{
// Drop in here again if this is an array
if(is_array($value))
$match = array_search_replace($needle,
$haystack[$key], $change);
// Otherwise, see if this value matches what were
looking for
else if($value == $needle)
{
// Remember, this changes the *original* array!
$haystack[$key] = $change;
$match = true;
}
}
// Did we succeed for not
return $match;
}
==============
More information about the thelist
mailing list