2013-08-09 1 views
0

아래 내가 옵션 필드 만 니코틴 수준을 보여주고, 다를 수 있습니다 옵션, 나랑 옵션을 많이 가지고 도와주세요 싶어 내 배열 출력어떻게 배열을 분할 할 특정 요소를 보여

[0] => Array 
[OrderDetails] => Array 
(

    [Options] => [Nicotine Level:12mg Nicotine][Sampler Item 1:Krankberry][Sampler Item 2:Unicorn Blood][Sampler Item 3:WaterFelons] 
) 
) 

입니다 .

나는 코드 아래에 사용한

$explode_var = explode('[','[Nicotine Level:12mg Nicotine][Sampler Item 1:Krankberry][Sampler Item 2:Unicorn Blood][Sampler Item 3:WaterFelons]'); 

echo '<pre>'; 
print_r($explode_var); 
echo '</pre>'; 

답변

2

이이 코드에 코멘트를 읽는 한 가지 방법입니다 :

<?php 
$data = array(array('OrderDetails' => array('Options' => '[Nicotine Level:12mg Nicotine][Sampler Item 1:Krankberry][Sampler Item 2:Unicorn Blood][Sampler Item 3:WaterFelons]'))); 

$options = $data[0]['OrderDetails']['Options']; 
$options = str_replace(']', '', $options); 
$old_var = array_filter(explode('[',$options)); 
$result = array(); 
foreach ($old_var as $items) 
{ 
    $item = explode(':', $items); 
    $result[$item[0]] = $item[1]; 
} 

foreach ($result as $key=>$value) 
{ 
    echo $key, " => ", $value, "<br/>\n"; 
} 

Live DEMO.

관련 문제