如何将Json数组转换为PHP数组

时间:2014-01-31 05:18:10

标签: php arrays json

这些是我在模型中用于保存和放大的两个功能。检索我的数据库中的数据

这是我的保存数据函数//

  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;   
        }   

  }

这是我的检索数据的代码

    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;

        } 

现在我正在调用我的控制器中的检索功能。

 $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 .

请帮助我以PHP数组格式获取数据。

(
    [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}}
)

我想将此数组转换为php数组

3 个答案:

答案 0 :(得分:3)

  

json_decode()期望参数1为字符串,给定数组

我认为错误信息非常明确:您正在将数组传递给json_decode,但它需要一个字符串。看起来你必须迭代数组:

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

答案 1 :(得分:0)

您的jsonarray

因此您可以使用迭代数组并使用json_decode More Details on Json_decode

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

答案 2 :(得分:0)

使用此功能并将结果保存在另一个变量中。

<强>功能:

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

用法:

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