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

我是php新手,但到目前为止我真的很喜欢它!
我现在偶然发现了一个问题< code> array_merge </ code>。</ p>

这是我的代码,它只是抓取我数据库中指定的每个表,然后将其转换为一个大的json文件:</ p>
\ n

  $ tables = array(“buildings”,“medical”,“other”,“tools”,“traps”,“weapon”);  

foreach($ tables as $ table){

//从数据库获取表
$ sth = $ db-&gt; query(“SELECT * FROM $ table”);
$ result = $ sth-&gt; fetchAll();

//将数组合并在一起
if($ arr === null){
// echo“其0&lt; br /&gt;”;
$ arr = array(“$ table”=&gt; $ result);
} else {
// echo“大于0&lt; br /&gt;”;
$ arr2 = array(“$ table”=&gt; $ 结果);
$ merge = array_merge($ arr,$ arr2);
}

} //结束循环

echo $ merge;
</ code> </ pre>
\ n

到目前为止它正在以某种方式工作,我设法得到第一个表“建筑物”和最后一个表“武器”以我想要的方式显示! </ p>

但我不明白为什么它跳过其他的..
我相信它与 $ arr2 </ code>有关,我需要指定 每个表的唯一数组。 但是我怎样才能做到这一点? 这是要走的路还是有更有效的方法来实现这个目标?
谢谢!</ p>
</ div>

展开原文

原文

I'm new to php but i really like like it so far! Now i stumbled over a problem with array_merge.

This is my code, it simply grabs each table specified in my database then makes it to a big json file.:

  $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. But how can i achieve this? Is this the way to go or is there a more efficient way to achieve this? Thanks!

5个回答



因为这行而失败</ p>

  $ merge = array_merge($ arr,  $ arr2); 
</ code> </ pre>

您的合并始终是$ arr(这是$ tables中的第一个条目)和$ arr2的合并,这是最新的条目 处理。 您没有将数据与先前合并的数据合并,例如</ p>

  $ arr = array_merge($ arr,$ arr2)
</ code> </ pre> \ n

此外,您可以通过将$ arr设置为空数组来启动来摆脱if / else逻辑。</ p>

  $ arr =  array(); 
</ code> </ pre>
</ div>

展开原文

原文

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. 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 = array();

doucuan5365
doucuan5365 我真的很喜欢所有答案! 他们都是正确的,他们都解决了我的问题! 但我想接受这个答案,因为它描述了它失败的原因和原因。 给我一个更大的理解,而不仅仅是代码。 但我相信你应该像其他人一样添加一个解决方案,以便将来的人们可以看到:)感谢所有帮助人员!
5 年多之前 回复



您可以将所有内容放在一个唯一的数组中,例如:</ p>

  $ myArray [  “$ table”] = $ result; 
</ code> </ pre>
</ div>

展开原文

原文

You can put everything inside an unique array, like:

$myArray["$table"] = $result;



  $ tables = array(“buildings”,“medical”,“other”,“tools”,“traps”,  “武器”);

$ tableArr = array();
foreach($ tables as $ table){

//从数据库中获取表
$ sth = $ db-&gt; query(“SELECT * FROM $ table”);

$ result = $ sth-&gt; fetchAll();

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

} //结束循环

print_r($ tableArr);
</ code> </ pre>

创建新数组 将index设置为表名并将其结果存储在该数组中。</ p>
</ div>

展开原文

原文

$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.



您的代码可以大大简化。</ p>

  $ tables = array(“buildings”  “,”“医疗”,“其他”,“工具”,“陷阱”,“武器”);

foreach($ tables as&amp; $ table){
$ table = $ db-&gt; query(“SELECT * FROM $ table”) - &gt; fetchAll();
}
echo json_encode($ tables);

</ code> </ pre>

当然,您仍然需要检查错误(数据库和JSON错误),以便代码可靠,但您可以更简单地制作一些东西 更好。 简单就是好,特别是在编程方面。</ p>
</ div>

展开原文

原文

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. Simple is good, especially when it comes to programming.



如果我理解正确... </ p>

而不是这两行</ p> \ n

  $ arr2 = array(“$ table”=&gt; $ result); 
$ merge = array_merge($ arr,$ arr2);
</ code> </ pre> \ n

你可以试试这个:</ p>

  $ tables [$ table] = $ result; 
</ code> </ pre>
</ div >

展开原文

原文

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;

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐