PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, December 4, 2022

[FIXED] How to json encode Specific Key value using PHP?

 December 04, 2022     arrays, javascript, json, jsonencoder, php     No comments   

Issue

I have tried this code but receiving error.

$queryfetch = 'select * from table';
    
$result = mysqli_query($connection, $queryfetch);
        
$row = mysqli_fetch_assoc($result);
        
$data = [];
foreach($row as $key) {
   $data[] = [
       'first_name' => $key['first_name'],
       'last_name' => $key['last_name']
    ];
}
        
echo json_encode($data);

How can I json encode specific keys using php ?


Solution

I have solved..

$queryfetch = 'select * from table';
    
$result = mysqli_query($connection, $queryfetch);
        
$row = mysqli_fetch_assoc($result);

$data[] = array(
    'first_name' => $row['first_name'],
    'last_name' => $row['last_name']
);

echo json_encode($data);

This code is working very well..



Answered By - Dhaval Makwana
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing