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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:13:28+00:00 2026-05-23T03:13:28+00:00

Given the following array: Array ( [143] => Car #1 [144] => Car #2

  • 0

Given the following array:

Array
(
    [143] => Car #1
    [144] => Car #2
    [145] => Car #3
)

I am currently using this

implode(', ', array_values($car_names))

to generate a string like

Car #1, Car #2, Car #3

I would like to actually get something like

Car #1, Car #2 and Car #3

The idea would be to insert ” and “ between the two last elements of the array.

If the array happens to contain two key/value pairs (eg, user has 2 cars), there would be no commas.

Car #1 and Car #2

And if the array contains one key/value (eg, user has 1 car)

Car #1

Any suggestion how to get this done? I tried using array_splice but I’m not sure that’s the way to go (ie, inserting a new element into the array).

Thanks for helping!

  • 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-23T03:13:29+00:00Added an answer on May 23, 2026 at 3:13 am

    I think you probably want something like this, using array_pop to get the last element off the end of the array. You can then implode the rest of the array, and add the last element in a custom fashion:

    $last_value = array_pop($car_names);
    $output = implode(', ', array_values($car_names));
    $output .= ($output ? ' and ' : '') . $last_value;
    

    Edit: added conditional to check if the array had only one element.

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

Sidebar

Related Questions

Can you help me out with this question please. Question: Given the following array
ok...in php how do i do this? given the following scenario: // array of
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given following Ruby statements: (Read input and store each word in array removing spaces
Given the following two indexed arrays: $a = array('a', 'b', 'c'); $b = array('red',
Given the following string: var str = one,two,three; If I split the string on
given the following function signature: void readFileData(FILE* fp, double inputMatrix[][], int parameters[]) this doesn't
I'm trying to write the following Perl subroutine. Given are an array a of
Given the following program, #include <iostream> using namespace std; void foo( char a[100] )
Given the following array: var things = ['sandwich', 17, 'fake@sample.com', 3, 'horse', 'octothorpe', 'anotheremail@sample.com',

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.