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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:28:48+00:00 2026-05-28T20:28:48+00:00

I have a simple value pair array like this : array(4) ( [4] =>

  • 0

I have a simple value pair array like this :

array(4) (
  [4] => (string) Barcelona
  [3] => (string) Cordoba
  [1] => (string) Granada
  [2] => (string) Jaen
)

I need to encode this to JSON to respond to an AJAX request in the following format :

[{"pk": 4, "name": "Barcelona"},
{"pk": 3, "name": "Cordoba"},
{"pk": 1, "name": "Granada"},
{"pk": 2, "name": "Jaen"}]

If I use :

json_encode($a)

I get the following :

{"4":"Barcelona","3":"Cordoba","1":"Granada","2":"Jaen","0":"Select a province"}

How do I get PHP to format my simple array to include properties in the JSON?

  • 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-28T20:28:49+00:00Added an answer on May 28, 2026 at 8:28 pm

    Just create a new array:

    $data = array();
    
    foreach($array as $key => $value) {
        $data[] = array('pk' => $key, 'name' => $value);
    }
    
    $json = json_encode($data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a json string like this: { ... key1:value1; ... } with
I have the following simple function: private void EnableDisable941ScheduleBButton() { if (this._uosDepositorFrequency.Value != null)
I have a datatable that contains simple Key Value pair how do I return
I have a simple table with autoincrementing ID's and one text column. id(int), value(nvarchar)
I have what I hope is a fairly simple question about using the value
i have the following simple script <input class=input type=text name=password style=color: #797272; value= <?php
well i have this messages table with sample values like these: msg_id recipient_id read
I have a simple content edit form (Zend_Form) that I populate() with an array
I suspect this is simple and I'm just missing it. I have a view
This is a pretty complex regular expression that returns an array of key/value pairs

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.