Home >Backend Development >PHP Tutorial > 字符串转入二维数组

字符串转入二维数组

WBOY
WBOYOriginal
2016-06-13 13:17:421184browse

字符串转为二维数组
$str = array ( 'digest' => '2012', ), 1 => array ( 'digest' => '2011', ), 2 => array ( 'digest' => '2010', ), 3 => array ( 'digest' => '2009', ), 4 => array ( 'digest' => '2008', ), 5 => array ( 'digest' => '2007', ), ); ?> 

需要转成
Array
(
  [0] => Array
  (
  [digest] => 2012
  )

  [1] => Array
  (
  [digest] => 2011
  )

  [2] => Array
  (
  [digest] => 2010
  )

  [3] => Array
  (
  [digest] => 2009
  )

  [4] => Array
  (
  [digest] => 2008
  )

  [5] => Array
  (
  [digest] => 2007
  )

)
求高人帮忙啊

------解决方案--------------------
你字符串是这样的吗?

$str = "array ( 'digest' => '2012'), 1 => array ( 'digest' => '2011' )........"

是的话

PHP code


<?php $str ="array ( 'digest' => '2012'), 1 => array ( 'digest' => '2011' )";

$arr=explode(',',$str);

$temp=array();

foreach($arr as $key=>$value)
{    
    $arr_temp=explode("'",$value);
    $real_value=$arr_temp[3];
    $temp[$key]['digest'] = $real_value;
}

print_r($temp);
?> <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn