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

Saturday, January 1, 2022

[FIXED] Get JSON Encode Data PHP CodeIgniter

 January 01, 2022     codeigniter, json, php     No comments   

Issue

I have json data stored in database with field name "field_form" like this:

[
    {
        "panjang": "200",
        "tipe_data": "text",
        "nama_field": "nama lengkap"
    },
    {
        "panjang": "201",
        "tipe_data": "number",
        "nama_field": "tahun lahir"
    }
]

I need to get "nama_field" data in PHP CodeIgniter controller, when i get the data with code:

$data_form = $this->perizinan_model->get_sub_field_form($id_jenis_izin)->result();

foreach($data_form as $data_field){
   var_dump(json_decode($data_field->field_form));
}
        

var_dump result is

array(2) { [0]=> object(stdClass)#21 (3) { ["panjang"]=> string(3) "200" ["tipe_data"]=> string(4) "text" ["nama_field"]=> string(12) "nama lengkap" } [1]=> object(stdClass)#23 (3) { ["panjang"]=> string(3) "201" ["tipe_data"]=> string(6) "number" ["nama_field"]=> string(11) "tahun lahir" } }

But, i just want to get nama_field data, and store it on array. Thanks before.


Solution

From your data structure. It is object inside array then this is the code.

$nama_fields = [];
foreach($data_form as $data_field){
    $jsonData = json_decode($data_field->field_form);

    if (is_array($jsonData)) {
        foreach ($jsonData as $index => $item) {
            if (isset($item->nama_field)) {
                $nama_fields[] = $item->nama_field;
            }
        }
    }
}

// then use $nama_fields variable.
var_dump($nama_fields);


Answered By - vee
  • 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