简体   繁体   English

PHP:使用array_merge的foreach循环创建json对象

[英]PHP: foreach loop with array_merge to create json objects

I'm new to php but i really like like it so far! 我是php新手,但到目前为止我真的很喜欢! Now i stumbled over a problem with array_merge . 现在我偶然发现了array_merge的问题。

This is my code, it simply grabs each table specified in my database then makes it to a big json file.: 这是我的代码,它只是获取数据库中指定的每个表,然后将其制成一个大的json文件。

  $tables = array("buildings", "medical", "other", "tools", "traps", "weapons"); 

    foreach ($tables as $table) {

        // Get tables from database
        $sth = $db->query("SELECT * FROM $table");
        $result = $sth->fetchAll();

        // Merge arrays together
        if ($arr === null) {
            //echo "its 0 <br/> ";
            $arr = array( "$table" => $result );
        } else {
            //echo "more than 0 <br/> ";
            $arr2 = array( "$table" => $result );
            $merge = array_merge($arr, $arr2);
        }

    } //End loop

    echo $merge;

So far It's working somehow, I manage to get the first table "buildings" and the last table "weapons" to be displayed the way i want perfectly! 到目前为止,它以某种方式起作用,我设法以我想要的完美方式显示了第一个表格“建筑物”和最后一个表格“武器”!

But I don't understand why it jumps over the other ones.. I believe it has something to do with $arr2 and that i need to specify a unique array for each of the tables. 但是我不明白为什么它会跳过其他表。我相信它与$arr2 ,我需要为每个表指定一个唯一的数组。 But how can i achieve this? 但是我怎么能做到这一点? Is this the way to go or is there a more efficient way to achieve this? 这是走的路还是有一个更有效的方法来做到这一点? Thanks! 谢谢!

您可以将所有内容放入唯一的数组中,例如:

$myArray["$table"] = $result;
$tables = array("buildings", "medical", "other", "tools", "traps", "weapons"); 
$tableArr = array();
foreach ($tables as $table) {

    // Get tables from database
    $sth = $db->query("SELECT * FROM $table");
    $result = $sth->fetchAll();

    if(isset($result)){
        $tableArr[$table] = $result;
    }else{
        $tableArr[$table] = '';
    }


} //End loop

print_r($tableArr);

Created new array and set index as table name and store its result in that array. 创建新数组并将索引设置为表名,并将其结果存储在该数组中。

If I understand correctly... 如果我理解正确...

Instead of this two lines 代替这两行

$arr2 = array( "$table" => $result );
$merge = array_merge($arr, $arr2);

You can try this: 您可以尝试以下方法:

$tables[$table] = $result;

Its failing because of this line 由于这条线而失败

    $merge = array_merge($arr, $arr2);

Your merge is always a merge of $arr (which is the first entry in $tables) and $arr2, which is the latest entry being processed. 您的合并始终是$ arr(这是$ tables中的第一个条目)和$ arr2(这是正在处理的最新条目)的合并。 You are not merging the data with the previously merged data, such as 您没有将数据与先前合并的数据合并,例如

    $arr = array_merge($arr, $arr2)

Also, you can get rid of the if/else logic by just starting off by setting $arr to an empty array to start. 同样,您可以通过将$ arr设置为一个空数组来启动,从而摆脱if / else逻辑。

    $arr = array();

Your code can be greatly simplified. 您的代码可以大大简化。

$tables = array("buildings", "medical", "other", "tools", "traps", "weapons"); 
foreach ($tables as & $table) {
    $table = $db->query("SELECT * FROM $table")->fetchAll();
}
echo json_encode($tables);

Of course, you still need to check for errors (database and JSON errors), in order for the code to be robust, but the simpler you can make something the better. 当然,为了使代码更健壮,您仍然需要检查错误(数据库和JSON错误),但越简单越好。 Simple is good, especially when it comes to programming. 简单是件好事,尤其是在编程方面。

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

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