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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:15:31+00:00 2026-05-25T00:15:31+00:00

i have an array which looks like this: Array ( [0] => Array (

  • 0

i have an array which looks like this:

Array ( 
    [0] => Array ( [unit_id] => 1 [unit_name] => Clown Fish) 
    [1] => Array ( [unit_id] => L [unit_name] => Liter ) 
    [2] => Array ( [unit_id] => 2 [unit_name] => Elephant  ) 
    [3] => Array ( [unit_id] => 3 [unit_name] => Water Bottle ) 
    [4] => Array ( [unit_id] => 4 [unit_name] => Office Seating ) 
    [5] => Array ( [unit_id] => 5 [unit_name] => Green Green Grass ) 
)

then, i wrote a function

function array_to_list($arr_data, $str_key, $str_value) {
    $arr_list = array();
    if (is_array($arr_data)) {
        foreach($arr_data as $arr_value) {
            if (isset($arr_value[$str_key]) && isset($arr_value[$str_value])) {
                $arr_list = array_merge($arr_list, array($arr_value[$str_key] => $arr_value[$str_value]));
            }
        }
    }
    return $arr_list;
}

to convert the array to look like this

Array ( 
    [1] => Clown Fish ) 
    [L] => Liter ) 
    [2] => Elephant ) 
    [3] => Water Bottle ) 
    [4] => Office Seating ) 
    [5] => Green Green Grass ) 
)

but the output, instead, is

Array ( 
    [0] => Clown Fish ) 
    [L] => Liter ) 
    [1] => Elephant ) 
    [2] => Water Bottle ) 
    [3] => Office Seating ) 
    [4] => Green Green Grass ) 
)

i assume this has something to do with the nature of array_merge itself, which according to php manual “Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.”

is there a way to so i can produce the intended result with or without array_merge?

  • 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-25T00:15:32+00:00Added an answer on May 25, 2026 at 12:15 am

    Why don’t you just try a much simpler approach:

    function array_to_list($arr_data, $str_key, $str_value) {
        $arr_list = array();
        if (is_array($arr_data)) {
            foreach($arr_data as $arr_value) {
                if (isset($arr_value[$str_key]) && isset($arr_value[$str_value])) {
    
                    // This is the changed line:
                    $arr_list[ $arr_value[$str_key] ] = $arr_value[$str_value];
    
                }
            }
        }
        return $arr_list;
    }
    

    This just sets the values on the output array directly. There’s no reason to make a new array and merge it each time. It also should be a bit faster.

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

Sidebar

Related Questions

I have a PHP array which looks like this example: $array[0][0] = 'apples'; $array[0][1]
I have an array which looks like this: array[0]: 6 8 array[1]: 12 9
I have an array which looks like this $dataArray = array ( 0 =>
I Have an array which looks like this: Array ( [0] => Array (
I have an array with numerical indices, which looks like this (after I unset
I have a php multidimensional array which looks like this: $fields = array( array('input',
I have an array in PHP which looks like this: Array ( [2] =>
I have an Array which looks like this. [{title=>ga:browser=Internet Explorer, dimensions=>[{:browser=>Internet Explorer}], metrics=>[{:pageviews=>2047}], id=>http://www.google.com/analytics/feeds/data?ids=ga:12316&ga:browser=Internet%20Explorer&start-date=2010-02-27&end-date=2011-02-27,
I have an array of data which looks like this [0.5, 0.3, 0.15,0.0, 0.0,
I have an array which for arguments sake looks something like this: a =

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.