Jump to content


Photo

Repeating a process on a list of vars, like array_walk


  • Please log in to reply
1 reply to this topic

#1 killerb

killerb
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 06 August 2006 - 02:48 AM

Does anyone know a function that does this? I have just found a use for it, while making a simple program to calculate timber lengths for making a box.

I take L,w,h, thickness and timberWidth, then do the various equations, then with the resultVars, I want to send them through number_format() before displaying them.

My function uses global vars. If I had used an array, I could do array_walk, but I have vars.

I tried a few things:
<?php

$arr=array($tl,$tw,$ew,$eh,$sl,$sh);//etc
array_walk($arr,'number_format');
// doesnt work

$arr=array(&$tl,&$tw,&$ew,&$eh,&$sl,&$sh);//etc
array_walk($arr,'number_format');
// Nope.

array_walk(array($tl,$tw,$ew,$eh,$sl,$sh),'number_format');
// E_ERROR

array_walk(($tl,$tw,$ew,$eh,$sl,$sh),'number_format');
// E_ERROR

?>

Is there actually a way to do it so the array_walk loop updates all referenced vars?

No results from searching.

Cheers.
Experience is something you get just after you need it.

#2 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 06 August 2006 - 09:31 AM

This sample (using array_map) will work for you:
<?php
$tl="1"; $tw="2"; $ew="3";
$arr=array($tl,$tw,$ew);
$b=array_map("addit", $arr);
print_r($b);
function addit($val) { return 'X'.$val; }
?>

RTFM is an almost extinct art form, it should be subsidized.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users