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 8373137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:36:18+00:00 2026-06-09T14:36:18+00:00

I have the following function which works fine. function ($objects, $items = array()) {

  • 0

I have the following function which works fine.

function ($objects, $items = array())
{
  $result = array();
  foreach ($objects as $object) {
    $result[$object->id] = $object->first_name . ' ' . $object->last_name; 
  }
  return $result;
}

However, I would like to pass in an array to $items, and have that exploded, so that I dont have to specify first_name and last_name manually.

If $item was only a single value (and not an array), then it would be simple:

$result[$object->id] = $object->$item; 

But I have no idea how to make this work if $items contains multiple values and I want to join them with a space. Something like, the following, but I need to get the $object in there

$items = array('first_name', 'last_name');
$result[$object->id] = implode(' ', $items);
  • 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-09T14:36:19+00:00Added an answer on June 9, 2026 at 2:36 pm

    Do I get you right that you`d like to use the strings in $item as property-names of $object?

    function ($objects, $items = array())
    {
      $result = array();
      foreach ($objects as $object) {
        $valuesToAssign = array();
        foreach ($items as $property) {
            $valuesToAssign[] = $object->$property;
        }
        $result[$object->id] = implode(' ', $valuesToAssign);
      }
      return $result;
    }
    

    I have no idea to avoid the second foreach, but that gives you the desired result.

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

Sidebar

Related Questions

I have the following function which works very well within a $(document).ready(function(){ $('.threadWrapper >
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
I have added the following fine function to my status bar to show which
All, I have the following array and function, which will create a multidimensional array
I have the following function which accepts text and a word count and if
I have the following function which sets up the GUI. public void go() {
I have the following function which creates a std::vector of iterators into another container:
I have the following function in a PHP script, which returns and API call:
I have the following C++ function definition, which I am trying to call through
I have a for-loop which performs the following function: Take a M by 8

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.