开发者

How to convert string to array when text like array

Can i convert the this text to array type. Because I curl for get result on somepage but it's return to text not array.

$array="Array ( [Dog] => Array ( [leg] => 4 [eyes] => 2 ) [Cat] => Array ( [leg] => 3 [eyes] => 4 ) )";
开发者_如何学C


You can use var_export to store parsable string as the above string give syntax error when executed with eval because that is not a valid array declaration syntax.

$array=array(1,2,3,4,5);
$b=var_export($array,true);
$c=eval("return $b;");
print_r($c);


I dont think it should be in quotes. By, putting it within quotes, you are telling the compiler that it is a string. If it is PHP it should be like $array = array("Dog" => array("leg"=>4, "eyes" => 2),"Cat" => array("leg"=>3, "eyes" => 4));

Or is it like you have a string and want to convert the data in it into string???


You have got this array from var_export function

You can bring back your array like this.

<?php
$array="Array ( [Dog] => Array ( [leg] => 4 [eyes] => 2 ) [Cat] => Array ( [leg] => 3 [eyes] => 4 ) )";
$finalRes=eval("return $array;");
print_r($finalRes);
?>

You can use var_export to store parsable string as the above string give syntax error when executed with eval because that is not a valid array declaration syntax.

Definition: Eval () is used to evaluate the input string as PHP. It is like using the Echo () function in the sense that it outputs everything, except instead of outputting it as text, it outputs it as PHP code to be executed. One use of this is to store code in a database to execute later. Also Known As: Evaluate String Examples:

 <?php 
 $name = 'Joe';
 $name2 = 'Jim';
 $a = 'My friends are $name and $name2';
 print $a . "<br>"; 
 eval("\$a = \"$a\";"); 
 print $a . "<br>"; 
 ?> 

This would output My friends are $name and $name2 when first called with the print statement, but would output My friends are Joe and Jim when called the second time after running eval ()

Your can get the array from here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜