PHP создает многомерный массив в цикле do-while из динамически (пользовательских) сгенерированных запросов

Мне нужна помощь с моей проблемой. У меня есть администрация для пользователей, где возможно создавать запросы из реальной базы данных.

Пользователь определяет запрос следующим образом:

["mysql_query"]=>array(3) {
[0]=>string(29) "SELECT `order`.* FROM `order`"[1]=>string(92) "SELECT `order_product`.* FROM `order_product` WHERE `order_product`.`order_id` = {query0.id}"[2]=>string(121) "SELECT `order_product_option`.* FROM `order_product_option` WHERE `order_product_option`.`order_product_id` = {query1.id}"}

Тогда у меня есть функция для обработки запросов:

function mysql_structure($mysql_key, $mysql_query = null)
{
global $param, $mysql_output;

${'sql' . $mysql_key . 'db'} = (($mysql_query == null) ? $param['mysql_query'][$mysql_key] : $mysql_query);
${'query' . $mysql_key . 'db'} = get_query(${'sql' . $mysql_key . 'db'});
${'row' . $mysql_key . 'db'} = mysqli_fetch_assoc (${'query' . $mysql_key . 'db'});
${'total_rows' . $mysql_key . 'db'} = mysqli_num_rows (${'query' . $mysql_key . 'db'});

if (${'total_rows' . $mysql_key . 'db'} > 0)
{
do
{
foreach (${'row' . $mysql_key . 'db'} as $field => $value)
{
$mysql_output['query' . $mysql_key][${'row' . $mysql_key . 'db'}['id']]["{$field}"] = "{$value}";
}

if ($param['mysql_query'][($mysql_key + 1)] != null)
{
$q = get_string_between($param['mysql_query'][($mysql_key + 1)], '{', '}');

if ($q != null)
{
$qe = explode ('.', $q);

$mysql_query = str_replace ('{' . $q . '}', ${'row' . $mysql_key . 'db'}[$qe[1]], $param['mysql_query'][($mysql_key + 1)]);
}

mysql_structure(($mysql_key + 1), $mysql_query);
}

unset ($mysql_query);
}
while (${'row' . $mysql_key . 'db'} = mysqli_fetch_assoc (${'query' . $mysql_key . 'db'}));
}
}

mysql_structure(0);

$param['mysql_output'] = $mysql_output;

unset ($mysql_output);

echo '<pre>';
var_dump ($param['mysql_output']);
echo '</pre>';

Выход из этой функции:

array {
["query0"]=>array {
[8]=> array {
["id"]=>string(1) "8"["cart_code"]=>string(10) "3nlc7x8ri3"["order_code"]=>string(9) "201800010"["user_id"]=>string(1) "1"//...
}
[10]=> array {
["id"]=>string(2) "10"["cart_code"]=>string(10) "awzulr7bbm"["order_code"]=>string(9) "201800012"["user_id"]=>string(1) "27"//...
}
}
["query1"]=>array {
[9]=>array {
["id"]=>string(1) "9"["product_id"]=>string(1) "5"//...
}
[11]=>array {
["id"]=>string(1) "11"["product_id"]=>string(1) "7"//...
}
[12]=>array {
["id"]=>string(1) "12"["product_id"]=>string(1) "5"//...
}
}
["query2"]=>array {
[1]=>array {
["id"]=>string(1) "1"["option_title"]=>string(12) "title"//...
}
}
}

Но мне нужно создать в этом многомерном массиве цикла do-while:

array {
["query0"]=>array {
[8]=> array {
["id"]=>string(1) "8"["cart_code"]=>string(10) "3nlc7x8ri3"["order_code"]=>string(9) "201800010"["user_id"]=>string(1) "1"//...
["query1"]=>array {
[9]=>array {
["id"]=>string(1) "9"["product_id"]=>string(1) "5"//...
}
[11]=>array {
["id"]=>string(1) "11"["product_id"]=>string(1) "7"//...
["query2"]=>array {
[1]=>array {
["id"]=>string(1) "1"["option_title"]=>string(12) "title"//...
}
}
}
}
}
[10]=> array {
["id"]=>string(2) "10"["cart_code"]=>string(10) "awzulr7bbm"["order_code"]=>string(9) "201800012"["user_id"]=>string(1) "27"//...
["query1"]=>array {
[12]=>array {
["id"]=>string(1) "12"["product_id"]=>string(1) "5"//...
}
}
}
}
}

Спасибо.

0

Решение

Проблема в ключах, потому что эта функция работает. Вывод отличается, но мне нужен формат.

function mysql_structure($mysql_key, $mysql_query = null) {
global $param;

$sql = (($mysql_query == null) ? $param['mysql_query'][$mysql_key] : $mysql_query);
$query = get_query($sql);
$row = mysqli_fetch_assoc($query);
$total_rows = mysqli_num_rows($query);

if ($total_rows > 0) {
do {
unset ($temp);
foreach ($row as $field => $value) {
$temp["{$field}"] = "{$value}";
}

$mysql_output[] = $temp;

if ($param['mysql_query'][($mysql_key + 1)] != null) {
$q = get_string_between($param['mysql_query'][($mysql_key + 1)], '{', '}');

if ($q != null) {
$qe = explode ('.', $q);
$mysql_query = str_replace ('{' . $q . '}', $row[$qe[1]], $param['mysql_query'][($mysql_key + 1)]);
}

$mysql_output[] = mysql_structure(($mysql_key + 1), $mysql_query);
}
}
while ($row = mysqli_fetch_assoc ($query));
}
return @array_filter ($mysql_output);
}

$param['mysql_output'] = mysql_structure(0);

Я также могу посоветовать с этим ..

0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector