0

enter image description here

How can i split that image or assign an array in PHP.

[{"ID":2,"no":123,"pname":"400","unit":"mt","bid_count":"4568","unit_price":"56","est_unit_price":"21.31","progress_count":"3841","i_project":117,"seq":0}]

I tried to split but it is a little weird. I use this =>

$object = str_replace('[{', '', $object);  
    $object = str_replace('}]', '', $object);
    $pieces = explode(",", $object);
   // return $pieces[0];

    $datas = array(
        'ID' =>str_replace('"ID":','',$pieces[0]),
        'poz_no'=>str_replace('"poz_no":','',$pieces[1]),
        'poz_name'=>"asd",
        'unit'=>str_replace('"unit":','',$pieces[3]),
        'bid_count'=>str_replace('"bid_count":','',$pieces[4]),
        'unit_price'=>str_replace('"unit_price":','',$pieces[5]),
        'est_unit_price'=> str_replace('"est_unit_price":','',$pieces[6]),
        'progress_count' =>str_replace('"progress_count":','',$pieces[7]),
        'i_project'=>str_replace('"i_project":','',$pieces[8]),
        'seq' =>str_replace('"seq":','',$pieces[9])
     );

I did it myself but it seems weird:

$object = str_replace('[{', '', $object);  
    $object = str_replace('}]', '', $object);
    $pieces = explode(",", $object);


    $datas = array(
        'ID' =>str_replace('"','',str_replace('"ID":','',$pieces[0])),
        'poz_no'=>str_replace('"','',str_replace('"poz_no":','',$pieces[1])),
        'poz_name'=>str_replace('"','',str_replace('"poz_name":','',$pieces[2])),
        'unit'=>str_replace('"','',str_replace('"unit":','',$pieces[3])),
        'bid_count'=>str_replace('"','',str_replace('"bid_count":','',$pieces[4])),
        'unit_price'=>str_replace('"','',str_replace('"unit_price":','',$pieces[5])),
        'est_unit_price'=>str_replace('"','',str_replace('"est_unit_price":','',$pieces[6])),
        'progress_count' =>str_replace('"','',str_replace('"progress_count":','',$pieces[7])),
        'i_project'=>str_replace('"','',str_replace('"i_project":','',$pieces[8])),
        'seq' =>str_replace('"','',str_replace('"seq":','',$pieces[9]))
     );
LuaLua
  • 29
  • 5

2 Answers2

1

You can use json_decode for this.

If you have your string:

 $json = '[{"ID":2,"poz_no":123,"poz_name":"400mm Muflu Beton ve C Parçası Döşenmesi","unit":"mt","bid_count":"4568","unit_price":"56","est_unit_price":"21.31","progress_count":"3841","i_project":117,"seq":0}]';

You can simply do:

$datas = json_decode($json, true);

Now in $datas you have an array with all the fields from json.

As you can see in json you have an array of objects with only one element. If you want only that first element you can do after:

$datas = $datas[0]; //get first element of array.
Daniel Dudas
  • 2,972
  • 3
  • 27
  • 39
0

Your are actually dealing with a JSON encoded Data here. So, you may need to first convert the JSON Data to Standard PHP Object. And then you can simply access your preferred values using normal PHP Object Access Notation. The commented Code below attempts to illustrate (however vaguely) the Idea.

THE GIVEN JSON DATA:

    <?php

        $jsonString = '[{
                        "ID":2,
                        "poz_no":123,
                        "poz_name":"400mm Muflu Beton ve C Parçası Döşenmesi",
                        "unit":"mt",
                        "bid_count":"4568",
                        "unit_price":"56",
                        "est_unit_price":"21.31",
                        "progress_count":"3841",
                        "i_project":117,
                        "seq":0
                    }]';

THE PROCEDURE:

    <?php

        // CONVERT THE JSON DATA TO NATIVE PHP OBJECT...
        $arrJSONData    = json_decode($jsonString);

        // NOW, YOU COULD ACCESS THE PHP OBJECT BY DOING: $arrJSONData[0]
        $objJson        = $arrJSONData[0];

        // IF YOU DID IT LIKE THIS; FROM THIS POINT YOU COULD ACCESS YOUR DATA
        // LIKE YOU WOULD ACCESS ANY NORMAL PHP OBJECT LIKE SO:
        $id             = $objJson->ID;
        $pozNo          = $objJson->poz_no;
        $unit           = $objJson->unit;

        // HOWEVER, WHAT IF THE JSON DATA CONTAINS MORE THAN ONE ENTRY?
        // THEN A LOOP WOULD BE NECESSARY... 

        // LOOP THROUGH THE RESULTING ARRAY OF OBJECTS AND GET YOUR DATA...
        foreach($arrJSONData as $jsonData){             
            // AGAIN; YOU COULD ACCESS YOUR DATA
            // LIKE YOU WOULD ACCESS ANY NORMAL PHP OBJECT LIKE SO:
            $id             = $objJson->ID;
            $pozNo          = $objJson->poz_no;
            $unit           = $objJson->unit;               
        }
Poiz
  • 7,611
  • 2
  • 15
  • 17