user3141441
user3141441

Reputation: 31

How to Convert Json Array To PHP Array

these are my two functions used in model for save & retrieve the data in my database

Here is my function for save data //

  public function save_po($supid,$cart,$total)
  {
    $num=rand(1,99999);
    $date=Date('Y-m-d');
    $array_string=json_encode($cart);// $cart is an multi dimensional array here
    $data = array(
   'num' => $num ,
   'date' => $date ,
   'supplier' => $supid,
   'status' => 'RAISED',
   'items'=>$array_string,
   'terms'=>'Purchase Order Valid For 15days From the day Of Creation',
   'user'=>'1',
   'total'=>$total);

         $result=$this->db->insert('purchase_order',$data);
        if($this->db->affected_rows()>0)
        {
        return TRUE;    
        }
        else
        {
        return FALSE;   
        }   

  }

here is my code for retrieve the data

    public function get_previous($brand,$model,$sup_id) {


                     $this->db->where('supplier',$sup_id);    
            $result= $this->db->get(' purchase_order');
            if($result->num_rows()>0)
            {
                $result_array=array();
                foreach($result->result_array() as $row)
                {
                $result_array[]=$row['items'];
                }

                return $result_array;
            }
            else
            return FALSE;

        } 

now i'm calling that retrieve function in my controller like this.

 $data['previous']=$this->purchases_model->get_previous($x,$y,$z);
 $arr=(array)($data['previous']);
 print_r($arr);// it was giving the below output of array which is in json format.
 $php_array=json_decode($arr); // here it was displaying error like json_decode() expects parameter 1 to be string, array given .

Can you please help me to get that data in PHP array format.

(
    [0] => {"526728b1bdca73ca2919bb92e0dbe197":{"rowid":"526728b1bdca73ca2919bb92e0dbe197","id":"6","qty":"4","price":"25","name":"PARACETMOL","tax":5,"options":{"Brand":"RANBAXY","Category":"MEDICINES","Taxes":"ADDITIONAL(5%)=>5|","FT":5},"subtotal":100}}
    [1] => {"d28401e301187f7934510c203ffa90e2":{"rowid":"d28401e301187f7934510c203ffa90e2","id":"1","qty":"4","price":"15000","name":"CANVAS2","tax":3000,"options":{"Brand":"MICROMAX","Category":"ANDROID MOBILES","Taxes":"ADDITIONAL(5%)=>3000|","FT":3000},"subtotal":60000}}
    [2] => {"769f5f6be645e88fcb574d5e2ada318a":{"rowid":"769f5f6be645e88fcb574d5e2ada318a","id":"3","qty":"4","price":"19000","name":"JUNGLE","tax":9120,"options":{"Brand":"MICROMAX","Category":"FEATURED MOBILES","Taxes":"VAT(12%)=>9120|","FT":9120},"subtotal":76000}}
    [3] => {"8875353ac430b7302eb1bef0660205b3":{"rowid":"8875353ac430b7302eb1bef0660205b3","id":"6","qty":"4","price":"25","name":"PARACETMOL","tax":12,"options":{"Brand":"RANBAXY","Category":"MEDICINES","Taxes":"VAT(12%)=>12|","FT":12},"subtotal":100}}
)

i want to convert this array into php array

Upvotes: 0

Views: 4113

Answers (3)

Waqas Ghouri
Waqas Ghouri

Reputation: 1140

Use this function and get save the result in another variable.

Function:

function objectToArray( $object ) {
    if( !is_object( $object ) && !is_array( $object ) ) {
        return $object;
    }
    if( is_object( $object ) ) {
        $object = (array) $object;
    }
    return array_map( 'objectToArray', $object );
}

Usage:

$variable = objectToArray(json_decode($arr));
print_r($variable);

Upvotes: 0

Kaushik
Kaushik

Reputation: 2090

Your json is an array

so you can use iterate the array and the use json_decodeMore Details on Json_decode

foreach($array as $key => $Value) {
    $arr[$key] = json_decode($Value, true); // this will give key val pair array
}

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 817030

json_decode() expects parameter 1 to be string, array given

I think the error message is pretty clear: You are passing an array to json_decode, but it expects a string. It looks like you have to iterate over array:

foreach($arr as $i => $json) {
    $arr[$i] = json_decode($json, true);
}

Upvotes: 3

Related Questions