如何从数组中获取图像数据

时间:2017-05-23 05:51:30

标签: php arrays json

我有一个多维数组,我想从数组中获取'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 个答案:

答案 0 :(得分: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;
      }
   }
 }
}