vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   How do I explode, do something, implode? (https://vborg.vbsupport.ru/showthread.php?t=41277)

JJR512 07-19-2002 11:26 PM

How do I explode, do something, implode?
 
Let's say I have an array:

$array = "50,60,70,80,90";

And I want to take each element of the array and do something with it. The specific something is a mathmatical operation; let's say I want to subtract 10 from each number. Then, after subracting 10 from each number, I want so put all the results back into a new array. The new array would look like "40,50,60,70,80".

How do I write this?

I know to start with something like:

$exploded_array = explode(",",$array)

But where do I go from there? (And in the explode(), does $array need to be enclosed in quotes or not?)

Admin 07-20-2002 06:16 AM

PHP Code:

function subtract(&$value) {
    
$value -= 10;
}

$string '50,60,70,80,90';
$array explode(','$string);
array_walk($array'subtract');
$string implode(','$array); 

You can also use foreach, like this:
PHP Code:

$string '50,60,70,80,90';
$array explode(','$string);
foreach (
$array as $key => $value) {
    
$array[$key] -= 10;
}
$string implode(','$array); 

But I think the first method is more elegant. :)

Neo 07-20-2002 06:24 AM

Chen I forget but why do you add a & to the front of a variable in certain cases?

JJR512 07-20-2002 06:28 AM

Thanks!

Admin 07-20-2002 06:54 AM

Quote:

Originally posted by Neo
Chen I forget but why do you add a & to the front of a variable in certain cases?
I can't explain the whole thing in a post, read this:
http://php.fastmirror.com/manual/en/...references.php

Neo 07-20-2002 08:25 AM

Thanks for the help as always.


All times are GMT. The time now is 04:02 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01889 seconds
  • Memory Usage 1,722KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete