0

I have a multi dimensional array, I want to get 'img' data from array.

Below is my resultant array,

"{'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'}}"

Below is code,

$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);
        }
      }
   }
 }
}
Mohan
  • 281
  • 2
  • 14

1 Answers1

0

You json is invalid! I convert to valid:

[  
   {  
      "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"
      }
   }
]

So if you want img data, use below code:

$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;
      }
   }
 }
}
Mohammad Hamedani
  • 3,304
  • 3
  • 10
  • 22
  • No it is not working its throwing illegal string offset. – Mohan May 23 '17 at 06:14
  • Where is error? which file and line has error? – Mohammad Hamedani May 23 '17 at 06:17
  • Actually If I try code with my array it is showing illegal string offset. – Mohan May 23 '17 at 06:21
  • My json Array, '{'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'}}' – Mohan May 23 '17 at 06:22
  • It's invalid json, in my answer i validate this. So you must be change single quotes to double quotes and use `[` at the beginning of string and `]` at the end. and instead of `""` in your code must be used `,` for seperating array item. – Mohammad Hamedani May 23 '17 at 06:27
  • @mohanmmad How replace that can please help me – Mohan May 23 '17 at 06:28
  • first replace `""` with `,`, then replace `'` with `"`, and then add `[` at the start and add `]` at the end. If your data only it's exist in question, i convert it to validate. – Mohammad Hamedani May 23 '17 at 06:38
  • Below json is valid but how to extract img data from below json array, '{"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-f127a3f7144332566936559141210548143.png"}}' – Mohan May 23 '17 at 07:35
  • I updated my answer, So can access all of data by php variable. – Mohammad Hamedani May 23 '17 at 07:44