2017-05-23 37 views
0

我有一個多維數組,我想從數組中獲取'img'數據。如何從數組中獲取圖像數據

下面是我得到的陣列,

"{'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http:\/\/192.168.1.156\/dutees\/tshirtecommerce\/\/uploaded\/products\/dg-designer-fbfe5ba2144332567936169114610928496.png'}}""{'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http:\/\/192.168.1.156\/dutees\/tshirtecommerce\/\/uploaded\/products\/dg-designer-fbfe5ba2144332567936169114610928496.png'}}""{'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http:\/\/192.168.1.156\/dutees\/tshirtecommerce\/\/uploaded\/products\/dg-designer-56a3107c144332567919932061810464914.png'}}""{'0':{'id':'area-design'},'1':{'id':'images-1','width':'500px','height':'500px','top':'0px','left':'0px','zIndex':'auto','img':'http:\/\/192.168.1.156\/dutees\/tshirtecommerce\/\/uploaded\/products\/dg-designer-69b4fa3b144332567968295645910544813.png'}}" 

下面是代碼,

$file = dirname(DIR_SYSTEM) . '/tshirtecommerce/data/products.json'; 

if (file_exists($file)) 
{  
    $string = file_get_contents($file); 
    if ($string != false) 
    { 
    $e_products = json_decode($string); 
    if (isset($e_products->products) && count($e_products->products) > 0) 
    { 
     foreach($e_products->products as $values) 
     { 

     foreach ($values->design->back as $ck => $ch){ 
      echo json_encode($ch); 
     } 
     } 
    } 
} 
} 
+1

@Mohan你'JSON'無效。 –

+0

請檢查您的JSON其無效[http://jsonviewer.stack.hu/] –

+0

我認爲json鍵和值是用雙引號括起來的 – JYoThI

回答

0

你JSON是無效的!我轉換爲有效:

[ 
    { 
     "0":{ 
     "id":"area-design" 
     }, 
     "1":{ 
     "id":"images-1", 
     "width":"500px", 
     "height":"500px", 
     "top":"0px", 
     "left":"0px", 
     "zIndex":"auto", 
     "img":"http:\/\/192.168.1.156\/dutees\/tshirtecommerce\/\/uploaded\/products\/dg-designer-fbfe5ba2144332567936169114610928496.png" 
     } 
    }, 
    { 
     "0":{ 
     "id":"area-design" 
     }, 
     "1":{ 
     "id":"images-1", 
     "width":"500px", 
     "height":"500px", 
     "top":"0px", 
     "left":"0px", 
     "zIndex":"auto", 
     "img":"http:\/\/192.168.1.156\/dutees\/tshirtecommerce\/\/uploaded\/products\/dg-designer-fbfe5ba2144332567936169114610928496.png" 
     } 
    }, 
    { 
     "0":{ 
     "id":"area-design" 
     }, 
     "1":{ 
     "id":"images-1", 
     "width":"500px", 
     "height":"500px", 
     "top":"0px", 
     "left":"0px", 
     "zIndex":"auto", 
     "img":"http:\/\/192.168.1.156\/dutees\/tshirtecommerce\/\/uploaded\/products\/dg-designer-56a3107c144332567919932061810464914.png" 
     } 
    }, 
    { 
     "0":{ 
     "id":"area-design" 
     }, 
     "1":{ 
     "id":"images-1", 
     "width":"500px", 
     "height":"500px", 
     "top":"0px", 
     "left":"0px", 
     "zIndex":"auto", 
     "img":"http:\/\/192.168.1.156\/dutees\/tshirtecommerce\/\/uploaded\/products\/dg-designer-69b4fa3b144332567968295645910544813.png" 
     } 
    } 
] 

所以,如果你想IMG數據,可以使用下面的代碼:

$file = dirname(DIR_SYSTEM) . '/tshirtecommerce/data/products.json'; 

if (file_exists($file)) 
{  
    $string = file_get_contents($file); 
    if ($string != false) 
    { 
    $e_products = json_decode($string, true); 
    if ($e_products) 
    { 
     foreach($e_products as $item) 
     { 
     $first_id = (isset($item[0]) && isset($item[0]['id']) ? $item[0]['id'] : null; 
     $second_id = (isset($item[1]) && isset($item[1]['id']) ? $item[1]['id'] : null; 
     $width  = (isset($item[1]) && isset($item[1]['width']) ? $item[1]['width'] : null; 
     $height = (isset($item[1]) && isset($item[1]['height']) ? $item[1]['height'] : null; 
     $top  = (isset($item[1]) && isset($item[1]['top']) ? $item[1]['top'] : null; 
     $left  = (isset($item[1]) && isset($item[1]['left']) ? $item[1]['left'] : null; 
     $zIndex = (isset($item[1]) && isset($item[1]['zIndex']) ? $item[1]['zIndex'] : null; 
     $img  = (isset($item[1]) && isset($item[1]['img']) ? $item[1]['img'] : null; 
     } 
    } 
} 
} 
+0

不,它不是在處理它的非法字符串偏移。 – Mohan

+0

錯誤在哪裏?哪個文件和行有錯誤? –

+0

其實如果我嘗試使用我的數組代碼它顯示非法的字符串偏移量。 – Mohan