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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:47:52+00:00 2026-06-15T08:47:52+00:00

I need to write an array in a CSV file where column order must

  • 0

I need to write an array in a CSV file where column order must not be changed. But I want to reduce column titles to a length of max 64 chars. To make the question more general, let’s take a simple example :

$array = array(
    'A te' => 'foo A',
    'Q test' => 'foo Q',
    'Z test' => 'foo Z',
);

var_dump($array);

Give :

array (size=3)
  'A te' => string 'foo A' (length=5)
  'Q test' => string 'foo Q' (length=5)
  'Z test' => string 'foo Z' (length=5)

Now, I want to rename the “Q test” key to “Q tes”. If I do :

$swap = $array["Q test"];
unset($array["Q test"]);
$array["Q tes"] = $swap;
var_dump($array);

Displays :

array (size=3)
  'A te' => string 'foo A' (length=5)
  'Z test' => string 'foo Z' (length=5)
  'Q tes' => string 'foo Q' (length=5)

The column’s position changed, that’s not what I was looking for.

I solved my problem by using (here, reducing keys to 5 chars) :

$keys = array_keys($array);
$values = array_values($array);
$count = count($keys);
for ($i = 0; ($i < $count); $i++)
{
   if (mb_strlen($keys[$i]) > 5)
   {
      $keys[$i] = mb_substr($keys[$i], 0, 5);
   }
}
$array = array_combine($keys, $values);

var_dump($array);

Outputs:

array (size=3)
  'A te' => string 'foo A' (length=5)
  'Q tes' => string 'foo Q' (length=5)
  'Z tes' => string 'foo Z' (length=5)

But this looks very resources-devour… I do need to execute such a code on each of my rows, and I have from 10k to 100k rows.

Is there a simpler way to rename array keys without moving them?

  • 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-15T08:47:53+00:00Added an answer on June 15, 2026 at 8:47 am

    Try this, which should be simpler,

    foreach($array as $k=>$v){
       if($k=='Q test'){
          $new_arr['Q tes'] = $array['Q test'];
       }else{
          $new_arr[$k] = $v;
       }
    }
    
    print_r($new_arr); //set $array = $new_arr;  to overwrite all values
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write an array to a .csv file in PHP. Example array:
I have a csv file to read, but in one column there are some
I need to write the content of an array into a file. Let's suppose
I have a short[512x512] array need to write to a binary file with little
I need to save NSStrings from an array into a .csv file in my
I have a task where i need to write a multidimensional array to HDFS.
I need to write a macro that operates on bitarray looking like this: array[0]
I need to write effective and quick method to search byte array for given
The application need write file's last modification date. void Dater(String DateFile) { File file
I need to write to a text file using JavaScript. I have a machine

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.