2015-11-04 203 views
0

我试图构建多维获取json,但它变得复杂一点。当然有更简单的方法来做到这一点。这是我的代码。多维数组php

$rows = array(); 
$idx = 0; 
$sql = "SELECT products, GROUP_CONCAT(title,',' ,price SEPARATOR ', ') prods FROM mylist GROUP BY products";  
$query = mysqli_query($con, $sql); 
while($row = mysqli_fetch_assoc($query)){ 
    $rows[$idx]['products'] = $row['products']; 
    $title = explode(',',$row['prods']); 
    $rows[$idx]['prods'] = $title; 
    $idx++; 
}; 
echo '<pre>' . var_export($rows, true) . '</pre>'; 
echo json_encode($rows); 

它给我这个结果

array (
    0 => 
    array (
    'prods' => 
    array (
     0 => 'title 4', 
     1 => '4', 
     2 => ' title 1', 
     3 => '1', 
    ), 
), 
    1 => 
    array (
    'prods' => 
    array (
     0 => 'title 2', 
     1 => '21', 
    ), 
), 
    2 => 
    array (
    'prods' => 
    array (
     0 => 'title 3', 
     1 => '3', 
    ), 
), 
) 
[{"prods":["title 4","4"," title 1","1"]},{"prods":["title 2","21"]},{"prods":["title 3","3"]}] 

但我想这样

array (
    0 => 
    array (
    'prods' => 
    array (
     array(title => 'title 4', price => '4'), 
     array(title => ' title 1', price => '1'), 
    ), 
), 
    1 => 
    array (
    'prods' => 
    array (
     array(title => 'title 2', price => '21'), 
    ), 
), 
    2 => 
    array (
    'prods' => 
    array (
     array(title => 'title 3', price => '3'), 
    ), 
), 
) 



    [ 
    { 
     "prods": [ 
      { 
       "title": "title 4", 
       "price": "4" 
      }, 
      { 
       "title": "title1", 
       "price": "1" 
      } 
     ] 
    }, 
    { 
     "prods": [ 
      { 
       "title": "title2", 
       "price": "21" 
      } 
     ] 
    }, 
    { 
     "prods": [ 
      { 
       "title": "title3", 
       "price": "3" 
      } 
     ] 
    } 
] 

所以,不知道如何分解的数据混合在阵列来获得编码的右JSON。

+1

该代码不会产生该输出。 – AbraCadaver

+0

你放弃了还是什么? – AbraCadaver

+0

仍然在努力... – alflashy

回答

1

你可以对$ title数组做一个foreach,如果键是偶数,那么用所需的键构建输出数组。在下面我编辑替代你的数据库查询的JSON,以便完整的示例可以包含在没有SQL查询。

$rows = array(); 
$idx = 0; 
$data = json_decode('[{"products":"the product", "prods":"title 4,4,title 1,1"},{"products":"the product", "prods":"title 2,21"},{"products":"the product", "prods":"title 3,3"}]', true); 
foreach ($data as $row) 
{ 
    $rows[$idx]['products'] = $row['products']; 
    $title = explode(',',$row['prods']); 
    foreach ($title as $k => $v) { 
    // check if even 
    if ($k % 2 == 0) $rows[$idx]['prods'][] = array('title' => $v, 'price' => $title[$k+1]); 
    } 
    $idx++; 
}; 
echo '<pre>' . var_export($rows, true) . '</pre>'; 
echo json_encode($rows); 

会输出如下

array (
    0 => 
    array (
    'products' => 'the product', 
    'prods' => 
    array (
     0 => array ('title' => 'title 4','price' => '4'), 
     1 => array ('title' => 'title 1','price' => '1'), 
    ), 
), 
    1 => 
    array (
    'products' => 'the product', 
    'prods' => 
    array (
     0 => array ('title' => 'title 2','price' => '21'), 
    ), 
), 
    2 => 
    array (
    'products' => 'the product', 
    'prods' => 
    array (
     0 => array ('title' => 'title 3','price' => '3'), 
    ), 
), 
) 
+0

谢谢特里斯坦,但产品可能有更多的两个产品,所以想知道如果我必须循环内循环。就像我已经有产品循环,所以现在我有产品循环内的循环标题和价格 – alflashy

+0

看起来不错Tristan,我得到了类似的解决方案,但有点不同,我发布为答案2下面。你能告诉我哪个更好吗?谢谢 – alflashy

1

基于特里斯坦早先提出的建议,我可以用下面的代码,以获得期望的结果,不知道这是最好的方式。

$rows = array(); 
$idx = 0; 
$sql = "SELECT products, GROUP_CONCAT(title,',',price SEPARATOR ';') prods FROM mylist GROUP BY products";  
$query = mysqli_query($con, $sql); 

while($row = mysqli_fetch_assoc($query)){ 
    $prods = explode(';',$row['prods']); 
    foreach ($prods as $key => $value) { 
     $expValue = explode(',',$value); 
     $rows[$idx]['prods'][] = array('title' => $expValue[0], 'price' => $expValue[1]); 
    }; 
    $idx++; 
}; 
echo '<pre>' . var_export($rows, true) . '</pre>'; 

echo json_encode($rows); 
+0

看起来不错@alflashy,并且可能通过更改sql查询更易读。 – Tristan

+0

感谢您的帮助@Tristan – alflashy