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

  • SEARCH
  • Home
  • 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 7713591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:54:41+00:00 2026-06-01T01:54:41+00:00

This is probably very easy to do but for some reason I can’t seem

  • 0

This is probably very easy to do but for some reason I can’t seem to figure this out. Let’s say I have code like this:

$elements = array('a', 'b', 'c', 'd');

$myValues = array(
    'values' => array(
        'a' => array(
            'xx' => 3,
            'yy' => ''
        ),
        'b' => array(
            'xx' => '',
            'yy' => ''
        ),
        'c' => array(
            'xx' => 8.4,
            'yy' => ''
        ),
        'd' => array(
            'xx' => 18.4,
            'yy' => ''
            )
        )
);

foreach($elements as $elem)
{
    if($myValues['values'][$elem]['xx'] != '')
    {
        if($myValues['values'][$elem]['xx'] < 6)
        {
            $myValues['values'][$elem]['yy'] =  'less than 6';
        }
        elseif($myValues['values'][$elem]['xx'] >= 6 && $myValues['values'][$elem]['xx'] < 15)
        {
            $myValues['values'][$elem]['yy'] =  'between 6 and 16';
        }
        else
        {
            $myValues['values'][$elem]['yy'] = 'greater than 15';
        }

            testFunc($myValues['values'][$elem]['xx']); // This is how I would call my function once I replace the code above
    }
}

As you can see here what I’m trying to do is to change the value of $myValues['values'][$elem]['yy'] based on some conditions. What I want to do is to replace the if elseif else section of codes by a function that performs the same action.

I tried something like:

function testFunc($xx)
{
    if($xx < 6)
    {
        $yy = 'less than 6';
    }
    elseif($xx >= 6 && $xx < 15)
    {
        $yy =  'between 6 and 16';
    }
    else
    {
        $yy = 'greater than 15';
    }

    return $yy;
}

But obviously this won’t work because I’m not changing the value of $myValues['values'][$elem]['yy'] inside my function.

NOTE I really want to pass ONLY the value of $myValues['values'][$elem]['xx'] inside my function and return the changed value of $myValues['values'][$elem]['yy'].

Can anybody help me with this?

Thanks in advance

  • 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-06-01T01:54:43+00:00Added an answer on June 1, 2026 at 1:54 am

    EDIT: re-reading the question, I seem to have misunderstood it at first glance. There doesn’t seem to be a need for references here. Would this not work?

    foreach ($elements as $element) {
        $myValues['values'][$element]['yy'] = testFunc($myValues['values'][$element]['xx']);
    }
    

    Equally, if $elements is always the same as array_keys($myValues['values']), you could use references:

    foreach ($myValues['values'] as &$value) {
        $value['yy'] = testFunc($value['xx']);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so this i probably very easy but i can't seem to figure it
k this is probably something very easy but for some reason i am getting
This will probably be very easy to get a solution for, but I can't
This is a very basic question with probably an easy answer. Let's say I
I'm sure this is possible -- and probably very easy -- but I can't
This is probably very easy but im also very new to php. Im looking
This problem is probably very easy to solve but somehow I cannot find a
This probably very easy when you know how, but I don't :) I'm trying
This is probably very easy but I cannot get it to work in php.
I'm afraid this is probably a very embarrassingly easy question - but my mind

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.