Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我想将sting(关联数组sting)转换为关联数组?_Php_Arrays_Associative Array - Fatal编程技术网

Php 我想将sting(关联数组sting)转换为关联数组?

Php 我想将sting(关联数组sting)转换为关联数组?,php,arrays,associative-array,Php,Arrays,Associative Array,我想将关联数组作为字符串存储到变量a中,然后将变量转换为数组 $var='"electirc_bill"=>array( "type" => "number", "required"=>"yes" ), "electirc_bill_per"=>arra

我想将关联数组作为字符串存储到变量a中,然后将变量转换为数组

$var='"electirc_bill"=>array(
                                   "type" => "number",
                                    "required"=>"yes"
                                  ),
      "electirc_bill_per"=>array(
                                   "type" => "number",
                                    "required"=>"yes"
                                 ),                   
              "gass_bill"=>array(     
                                   "type" => "number",
                                    "required"=>"yes"
                                  )
    )';

    var_dump($var);
如下图所示

$json_str = json_encode($var); 

first then use json_decode($json_str); where required 
使用和

将数组转换为字符串:

$string = serialize($array);
将其转换回数组:

$array = unserialize($string);
编辑:根据您的评论,您似乎已经将数组存储为字符串,并希望能够将其转换为数组。为此,我将使用它,但在与任何用户输入一起使用时要小心,因为它可能导致代码中存在安全漏洞

我在这里用您的代码做了一个小例子:


您可以像这样使用序列化和取消序列化:

<?
$var=array("electirc_bill"=>array(
                 "type" => "number",
                 "required"=>"yes"
               ),
      "electirc_bill_per"=>array(
                 "type" => "number",
                 "required"=>"yes"
               ),                   
      "gass_bill"=>array(     
               "type" => "number",
               "required"=>"yes"
           )
    );
    var_dump($var);
    $string = serialize($var);
    var_dump($string);
    $array = unserialize($string);
    var_dump($array );
?> 

在这里,我建议使用此阵列将满足您的要求
$name=array('parent1'=>array('childone'=>'harish','childtwo'=>'vignesh'),'parent2'=>array('childone'=>'our children');
回声“;
打印(姓名);
foreach($父项名称)
{
foreach($parents as$child)
{
echo“;print_r($child);
}
}

使用并感谢。我想把完整的关联数组syntx作为字符串,并想转换成数组<代码>$var='$array_var=array(“一个”=>数组(“1.1”、“1.2”),“两个”=>数组(“2.1”、“2.2”)@navidaziz我想你可以试试
eval
,但请先了解一下它的漏洞。在使用任何用户输入时都要小心。下面是一个例子:
// save
file_put_contents('file.json', json_encode($array));

// load
$array = json_decode(file_get_contents('file.json'), true);
<?
$var=array("electirc_bill"=>array(
                 "type" => "number",
                 "required"=>"yes"
               ),
      "electirc_bill_per"=>array(
                 "type" => "number",
                 "required"=>"yes"
               ),                   
      "gass_bill"=>array(     
               "type" => "number",
               "required"=>"yes"
           )
    );
    var_dump($var);
    $string = serialize($var);
    var_dump($string);
    $array = unserialize($string);
    var_dump($array );
?> 
  Here i give suggestion to use this array will meet your requirement 

$name=array('parent1'=>array('childone'=>'harish','childtwo'=>'vignesh'),'parent2'=>array('childone'=>'our children'));
echo "<pre>";
print_r($name);

foreach($name as $parents)
 {
  foreach($parents as $child)
   {
     echo "<pre>"; print_r($child);
   }
 }