RE: [PHP-DB] Some data manipulation
Write a function to do whatever you want to each element, then use array_walk(). ---John Holmes... > -Original Message- > From: Hatem Ben [mailto:[EMAIL PROTECTED]] > Sent: Saturday, October 05, 2002 6:49 PM > To: [EMAIL PROTECTED] > Subject: [PHP-DB] Some data manipulation > > hey all > > i have a database of arrays like this : > Array > ( > [0] => 2- > [1] => 8b2 > [2] => 8#c3 > [3] => 8b2 > [4] => 8#f2 > [5] => 4a2 > [6] => 8a1 > [7] => 8a2 > [8] => 4a2 > [9] => 4a2 > [10] => 4a2 > > ) > > i just need to change in every string the last value when it is equal to > 1,2 > or 3 > ex :: > [0] => 2-won't be changed [0] => 2- > [1] => 8b2 will be changed to [1] => 8b6 > [2] => 8#c3 will be changed to[2] => 8#c7 > [6] => 8a1 will be changed to[6] => 8a > > and so on > > Thanks for any help > Hatem > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Some data manipulation
i was trying to do it with preg_replace, but this is much better thanks a lot, Hatem - Original Message - From: "Rasmus Lerdorf" <[EMAIL PROTECTED]> To: "Hatem Ben" <[EMAIL PROTECTED]> Cc: <[EMAIL PROTECTED]> Sent: Sunday, October 06, 2002 3:09 AM Subject: Re: [PHP-DB] Some data manipulation > So loop through and check the last char. Am I missing something that > makes this harder than the obvious: > > foreach($arr as $key=>$val) { > $end = $val[strlen($val)-1]; > switch($end) { >case '1': $arr[$key] = substr($val,0,-1); break; >case '2': >case '3': $arr[$key] = substr($val,0,-1) . ((int)$end + 4); break; > } > } > > Not sure if you always want to add 4 to the last char when it ends in 2 or > 3, but that was the only info you gave. > > -Rasmus > > On Sun, 6 Oct 2002, Hatem Ben wrote: > > > hey all > > > > i have a database of arrays like this : > > Array > > ( > > [0] => 2- > > [1] => 8b2 > > [2] => 8#c3 > > [3] => 8b2 > > [4] => 8#f2 > > [5] => 4a2 > > [6] => 8a1 > > [7] => 8a2 > > [8] => 4a2 > > [9] => 4a2 > > [10] => 4a2 > > > > ) > > > > i just need to change in every string the last value when it is equal to 1,2 > > or 3 > > ex :: > > [0] => 2-won't be changed [0] => 2- > > [1] => 8b2 will be changed to [1] => 8b6 > > [2] => 8#c3 will be changed to[2] => 8#c7 > > [6] => 8a1 will be changed to[6] => 8a > > > > and so on > > > > Thanks for any help > > Hatem > > > > > > -- > > PHP Database Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Some data manipulation
So loop through and check the last char. Am I missing something that makes this harder than the obvious: foreach($arr as $key=>$val) { $end = $val[strlen($val)-1]; switch($end) { case '1': $arr[$key] = substr($val,0,-1); break; case '2': case '3': $arr[$key] = substr($val,0,-1) . ((int)$end + 4); break; } } Not sure if you always want to add 4 to the last char when it ends in 2 or 3, but that was the only info you gave. -Rasmus On Sun, 6 Oct 2002, Hatem Ben wrote: > hey all > > i have a database of arrays like this : > Array > ( > [0] => 2- > [1] => 8b2 > [2] => 8#c3 > [3] => 8b2 > [4] => 8#f2 > [5] => 4a2 > [6] => 8a1 > [7] => 8a2 > [8] => 4a2 > [9] => 4a2 > [10] => 4a2 > > ) > > i just need to change in every string the last value when it is equal to 1,2 > or 3 > ex :: > [0] => 2-won't be changed [0] => 2- > [1] => 8b2 will be changed to [1] => 8b6 > [2] => 8#c3 will be changed to[2] => 8#c7 > [6] => 8a1 will be changed to[6] => 8a > > and so on > > Thanks for any help > Hatem > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php