Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 547839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:00:58+00:00 2026-05-13T11:00:58+00:00

Scenario/Problem Isolation : Lets suppose my program uses MULTIPLE variables. At the program beginning

  • 0

Scenario/Problem Isolation: Lets suppose my program uses MULTIPLE variables. At the program beginning I want to manipulate MANY of the variables AT ONCE through a general function with LITTLE CODE, before then later in the process using only distinctive few variables in specific functions.

Question: How do I pass multiple variables by reference to a foreach loop? Or is there a better/alternative method for looping through multiple determined variables?

Post(s) related to topic, but didn’t solve my issue:

PHP foreach loop on multiple objects?

Background (for those concerned): I have a command line program which uses getopts http://hash-bang.net/2008/12/missing-php-functions-getopts/ to get various arguments, thus I get about 20 variables. I want to run all variables, which contain filepath(s) (about 10) through the “general” function reduceHierarchyDots() at ONCE (instead of calling the function 10 times).

<?php

/// The "general" function:

function reduceHierarchyDots ($file) {
    while (preg_match('|./\.{2}/|', $file)) { $file = preg_replace('|/([^/]+)/\.{2}/|', '/', $file, 1); }
    $file = preg_replace('|(/(\./)+)|', '/', $file);
    $file = preg_replace('|^(\./)+|', '', $file);
    return $file;
}

function reduceHierarchyDotsRef (&$file) {
    while (preg_match('|./\.{2}/|', $file)) { $file = preg_replace('|/([^/]+)/\.{2}/|', '/', $file, 1); }
    $file = preg_replace('|(/(\./)+)|', '/', $file);
    $file = preg_replace('|^(\./)+|', '', $file);
}

/// The "many" variables:

$x = "something";
$y = 123;
$y = array ("a", "B", 3);
$a = "/Users/jondoe/Desktop/source/0.txt";
$b = "/Users/jondoe/Desktop/source/../1.txt";
$c = "/Users/jondoe/Desktop/source/../../2.txt";
$arrOne = array (
    "v1" => "/some/thing/../1.pdf",
    "v2" => "/some/thing/../../2.pdf",
    "v3" => "/some/thing/../../../3.pdf"
);
$arrTwo = array (
    "./1.doc",
    "/so.me/.thing/ends./././2.doc",
    "./././3.doc"
);

/// At the beginning I want to run multiple determined variables through a "general" function:

/// Debugging: Variables BEFORE the manipulation:
echo("BEFORE:\n"); var_dump($b, $arrOne["v2"], $arrTwo[2]); echo("\n");

/// Method works, but is long! (1 line/statement per function call)
reduceHierarchyDotsRef($b);
reduceHierarchyDotsRef($arrOne["v2"]);
reduceHierarchyDotsRef($arrTwo[2]);

/// Hence, I'd like to pass all variables by reference at once to a foreach loop:
//// These cause: Parse error: syntax error, unexpected '&':
// foreach ( array($b, $arrOne["v2"], $arrTwo[2] ) as &$file) { $file = reduceHierarchyDots($file); }
// foreach (array(&$b, &$arrOne["v2"], &$arrTwo[2] ) as &$file) { $file = reduceHierarchyDotsRef($file); }
//// These have no effect on the intended variables:
// foreach (array(&$b, &$arrOne["v2"], &$arrTwo[2] ) as $file) { $file = reduceHierarchyDots($file); }
// foreach (array(&$b, &$arrOne["v2"], &$arrTwo[2] ) as $file) { $file = reduceHierarchyDotsRef($file); }

/// Debugging: Variables AFTER the manipulation:
echo("AFTER:\n"); var_dump($b, $arrOne["v2"], $arrTwo[2]);

/// After the "general" function ran over various variables, the more specific actions happen: ...

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:00:58+00:00Added an answer on May 13, 2026 at 11:00 am

    You could try generating an array of the variable names, then using variable variables:

    $x = '/bees/../ham';
    $y = 'some/other/path';
    
    $arr = array('x', 'y');
    
    foreach($arr as $item) {
        reduceHierarchyDotsRef($$item);
    }
    

    not sure if this works with passing by reference, but I see not reason for it not to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think you can do something like that. $(subPathFiles.Split(';'))… May 14, 2026 at 10:38 pm
  • Editorial Team
    Editorial Team added an answer <[^<]+?> This Regex will match "< >" pair. Add logic… May 14, 2026 at 10:38 pm
  • Editorial Team
    Editorial Team added an answer Remove the third parameter from the helper: <%= Html.RadioButton("Sex", "Male")… May 14, 2026 at 10:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.