简体   繁体   English

在json_encode()之后在数据库中插入值

[英]Insert values in database after json_encode()

I have two dimensional array, and I got needed result. 我有二维数组,得到了所需的结果。 Now I need to insert it into db. 现在,我需要将其插入数据库。

If the situation is [[]], that is one row in db and has specific number in db (it should start with 1). 如果情况为[[]],则这是db中的一行,并且在db中具有特定编号(应以1开头)。

If the situation is [[] , []], in that case values in brackets are dimension, each bracket is row, but they have the same name, for ex 2. 如果情况为[[],[]],在这种情况下,方括号中的值是维度,每个方括号是行,但它们的名称相同,例如ex 2。

If there are more brackets inside [[] , [], [], []], in this case we will have 4 rows with the same name. 如果[[],[],[],[]]内有更多括号,在这种情况下,我们将有4行具有相同的名称。

My database looks like 我的数据库看起来像

ID | ID | DIMENSIONS | 尺寸| NAME 名称

ID - auto increment DIMENSIONS I take from below. ID-我从下面获取的自动增量尺寸。

For ex. 对于前。 [[12500,10]] - is one row, and for NAME it will have number 1. [[12500,8],[6400,2]] - this is six element in array below. [[12500,10]]-是一行,对于NAME,它的编号为1。[[12500,8],[6400,2]]-这是下面数组中的六个元素。 It will have two rows in database, and will have NAME 6. 数据库中将有两行,并且名称为6。

echo json_encode($pak); 回声json_encode($ pak); // produces output below //在下面产生输出

"[[[12500,10]],[[12500,10]],[[12500,10]],[[12500,10]],[[12500,10]],[[12500,8],[6400,2]],[[6400,10]],[[6400,10]],[[6400,10]],[[6400,10]],[[6400,10]],[[6400,10]],[[6400,10]],[[6400,10]],[[6400,10]],[[5558,10]],[[5558,10]],[[5558,8],[4600,2]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,6],[4500,4]],[[4500,10]],[[4500,10]],[[4500,10]],[[4500,10]],[[4500,8]]]"

I got stuck here, any help is appreciated. 我被困在这里,任何帮助表示赞赏。

We can achieve it by Recursion. 我们可以通过递归来实现。

But here I didn't use recursion (Because I am not good at that) 但是这里我没有使用递归(因为我不擅长此事)

I haven't tested properly, so please go through it carefully. 我没有正确测试,因此请仔细检查。 let me know if it has any problem. 让我知道是否有任何问题。

$data = "[[[12500,1]],[[12500,2]],[[12500,3]],[[12500,4]],[[12500,5]],[[111111,6],[22222,6]],[[6400,7]],[[6400,8]],[[6400,9]],[[6400,10]],[[6400,10]],[[6400,10]],[[6400,10]],[[6400,10]],[[6400,10]],[[5558,10]],[[5558,10]],[[5558,8],[4600,2]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,10]],[[4600,6],[4500,4]],[[4500,10]],[[4500,10]],[[4500,10]],[[4500,10]],[[4500,8]]]";
$decoded = json_decode($data);
$namedArray = fix_name($decoded);
$insertArray = arrange_array($namedArray);
echo "<pre>";
print_r($insertArray);
echo "</pre>";die;

// Fix the Name first
function fix_name($decoded)
{
$number = 1;
foreach ($decoded as $key=> $value) {
    $value['name'] = $number;
    $insertArray[] = $value;
    $number++;
}
return $insertArray;
}
function arrange_array($namedArray)
{
    $insertArray = [];
  foreach ($namedArray as $array) {
    if (count($array) > 2) {
        $name = $array['name'];
        unset($array['name']);
        foreach ($array as $key => $value) {
            $array[$key]['name'] = $name;
        }
        foreach ($array as $arr) {
            $insertArray[] = arrange($arr);
        }
    } else {
        $temp['DIMENSIONS '] = $array[0];
        $temp['NAME'] = $array['name'];
        $insertArray[] = $temp;
    }
    }
   return $insertArray;
}
 function arrange($array)
{
    $temp = [];
    $temp['DIMENSIONS'] = [$array[0], $array[1]];
    $temp['NAME'] = $array['name'];
    return $temp;
}

Process: 处理:

First: Fix the names 第一:固定名称

Next: If count > 2 (because "name" key was added in previous function so will have minimum two ) 下一个:如果count> 2(因为在上一个函数中添加了“ name”键,所以至少要有两个)

then array in different way 然后以不同的方式排列

else 其他

key 0 as DIMENSIONS and key name as NAME 键0为DIMENSIONS,键名称为NAME

NOTE: There are other ways to achieve it but for quick fix I gave this. 注意:还有其他方法可以实现它,但是为了快速修复,我给出了此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM