2

I'm retrieving data from MySQL using json_encode().

$sd=array();
foreach($rslt as $val){
     $sd[] = $val; 
}
echo json_encode($sd);

My question is How do I add the word "grade" in to all the results?

example: if the output is

97
38
73

how do I get the results as:

grade97
grade38
grade73

example Json output:

[{"physics":"97","0":"97","chemistry":"38","1":"38","agriculture":"73","2":"73"}]
1
  • 1
    you could do it in the sql using concat('grade',fieldname) etc Jun 17, 2015 at 16:06

2 Answers 2

2

You should not pollute your data like that as you might need the original values in the future and you will be sending a lot of redundant information (repeated words) down the line.

Instead, I would add the additional information that you want to send in another parameter.

For example:

$sd=array();
foreach($rslt as $val){
     $sd[] = $val; 
}

// add another chunk of information
$translations = array('grade' => 'Grade');

// send everything in one json string
echo json_encode(array('data' => $sd, 'translations' => $translations));

Now you can build the output correctly on the receiving end, you send less information and you have the original values available if needed.

5
  • thanks for your input. Actually it's essential to have the word "grade" in all those data. But my idea was to reduce the length of the data on the MYSQL and do something similar when retrieving data from the database. Hope that makes scenes?
    – Becky
    Jun 17, 2015 at 16:19
  • @Becky Just add the word when you build your (html?) output.
    – jeroen
    Jun 17, 2015 at 16:20
  • yes, that's another option. Are you saying that adding the "grade" in html is better or safer than adding it in php itself?
    – Becky
    Jun 17, 2015 at 16:24
  • He is right. The word "grade" is better added when rendering the HTML(the view).
    – CodeGodie
    Jun 17, 2015 at 16:25
  • @Becky Where and how are you building the html / output, in javascript? If so, doing it there and only sending the word once, makes your system more responsive because you have to send less data. There is no safety improvement or anything like that.
    – jeroen
    Jun 17, 2015 at 16:27
1

all you need to do is concatenate the string like this:

foreach($rslt as $val){
    foreach($val as &$grade){
         $grade = "Grade ".$grade;
    }
    $sd[] = $val;
}

However, you should clean your array. You currently have this structure:

array(
    array(
        "0" => 97,
        "1" => 38,
        "2" => 73,
        "physics" => 97,
        "chemistry" => 38,
        "agriculture" => 73,
    )
);

I would redo the code that produces this array so that you get something like this:

array(
    array(
        "class" => "physics",
        "grade" => 97
    ),
    array(
        "class" => "chemistry",
        "grade" => 38
    ),
    array(
        "class" => "agriculture",
        "grade" => 73
    ),
);

Its better for iteration, and way more readable.

5
  • Except that from the generated json it seems that $val is an array.
    – jeroen
    Jun 17, 2015 at 16:06
  • I get an error. Notice: Array to string conversion in path/my.php on line... ["CArray"] It points to this line $sd[] = "Grade ".$val;
    – Becky
    Jun 17, 2015 at 16:08
  • ok, i didnt know it was an array. Try it now, i updated my answer
    – CodeGodie
    Jun 17, 2015 at 16:11
  • can you please explain this &$grade ? what's that & for?
    – Becky
    Jun 17, 2015 at 16:31
  • that means you are passing by reference rather than by value. In other words, since we are iterating through an array and we want to change the variable $grade without having to create extra variables, we pass $grade by reference, so that once the loop is finished your initial array will have the changes made. hope that makes sense
    – CodeGodie
    Jun 17, 2015 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.