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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:45:42+00:00 2026-05-23T10:45:42+00:00

After asking a question yesterday, I’ve learned that I need to have an array

  • 0

After asking a question yesterday, I’ve learned that I need to have an array in Php and use JSON to pass it to Javascript. I’ve got all that working up to a point, but I need to add one more dimension to my array and not sure how.

The current array is:

$result = array(
  'NumberSelected' => $number,
  'TargetPerc'     => array (),
  'KpiDescription' => array (),
  'KpiName'        => array (),
  'ValuetoPrint'   => array (),
  'ValueNow'       => array (),
  'ValueCompare'   => array (),
  'Target'         => array (),
  'KpiUnits'       => array ()
);

But I now need to enclose this in another dimension to sort these by group.

This is what I’ve tried:

$result = array(
  $i => array(
    'NumberSelected' => $number,
    'TargetPerc'     => array (),
    'KpiDescription' => array (),
    'KpiName'        => array (),
    'ValuetoPrint'   => array (),
    'ValueNow'       => array (),
    'ValueCompare'   => array (),
    'Target'         => array (),
    'KpiUnits'       => array ()
  )
);

With $i defined as 0 two lines before that. But it doesn’t seem to recognise the outermose array. Could be something to do with my collecting code which is result.KpiName[i] currently (the JSON variable is passed to this javascript function as result). And I’ve tried result.i[KpiName[i]] with no success.

Any help would be appreciated.

  • 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-23T10:45:43+00:00Added an answer on May 23, 2026 at 10:45 am

    If you have a single result:

    $result = array(
        'NumberSelected' => $number,
        'TargetPerc' => array(),
        ....
    );
    

    Then you want to add it in with some other results:

    $result_set = array();
    $result_set[] = $result;
    

    At this point, $result_set is an array with one element, an array containing keys 'NumberSelected', 'TargetPerc', etc.

    I think you are getting tripped up with the idea of having arrays in arrays in arrays, so think of it like this: each array index is a component of an “path” to a specific piece of data, like in a filesystem. In this case, $result_set is the most general piece of the “path”, like a drive letter on Windows. Inside our pretend drive are a bunch of folders named with numbers, so $result_set[0] is the first folder, $result_set[1] would be the second. In each of those folders are sub-folders called NumberSelected (actually that one would be like a file, since it has a scalar value), TargetPerc, etc, which correspond to the second layer of your result set: $result_set[0]['NumberSelected']. Now, the final dimension of your array corresponds to the individual files inside each of 'TargetPerc', 'KpiDescription', etc, where the files, unless you gave them named keys, are named with numbers.

    So, to access the first item of 'KpiDescription' in the first result in your result set, your filesystem “path” (keeping with the analogy) would be

    result_set/0/KpiDescription/0
    

    Now, translating it back to PHP:

    $result_set[0]['KpiDescription'][0]
    

    Easy, right?

    Now the JSON part of your question: once you json_encode it, it’s just going to look like a big string of text to PHP, but the magic thing about json_encode is that it maintains the original data structure, but in Javascript.

    So your result set would look like this:

    result_set = [
        {
            NumberSelected: 123,
            TargetPerc: [ ... ],
            KpiDescription: [ ... ]
        }
    ];
    

    As you can see, the Javascript is just an array of objects, where each object has some properties that happen to be arrays. To access the first item in the first KpiDescription (just like above), you would use this:

    result_set[0].KpiDescription[0]
    

    Alternatively, because Javascript allows you to treat object properties as array indexes (actually that’s not quite true, but that’s another story), you could do this:

    result_set[0]['KpiDescription'][0]
    

    Look familiar? All it’s missing from the PHP is the leading $.

    I hope that helps clear some things up for you.

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

Sidebar

Related Questions

After asking this question I realized that to build what I need I'd better
After asking this question , it appears that a lot of CSS hacks and
After asking this question : jQuery die() does not work . I found that
I'm asking this question because I finally solved a problem that I have been
Apologies for asking a related question yesterday, but after some sleep I've simplified my
After asking this question about interface fields in records I assumed that the following
I am asking my question after I have tried many ways to solve it
After asking this question yesterday about branching in mercurial, I've decided to try out
I am writing an application that requires accurate timing. After asking this question ,
After asking this perl newbie question , I have a perl newbie follow-up. I

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.