在 PHP 中将数组添加到关联数组

发布于 2024-09-08 19:35:51 字数 2869 浏览 4 评论 0原文

<?php
$tab=1;/*$_GET['tab'];*/
$id=1111;/*$_GET['id'];*/
include_once('solve.php');
  $query="SELECT
  user_job.level1,
  user_job.tab_level,
  job.money_gain,
  job.exp_gain,
  job.energy_required,
  job.name,job.job_id
FROM user_job
RIGHT JOIN job USING(job_id) WHERE job.tab=".$tab." AND user_job.user_id=".$id." LIMIT 0,10";
     $result =solve1($query);
        while($data=mysql_fetch_array($result))
        {
            $i=0;
            $rows[]=array('name'=>array(
                                        'name_id'=>$data['name'],
                                        'tab_level'=>$data['tab_level']
                                        ),
                          'job_reward'=>array(
                                        'job_money'=>$data['money_gain'],
                                        'job_exp'=>$data['exp_gain'],
                                        'job_energy'=>$data['energy_required']
                                        ),
                          'job_required_items'=>array()

                        );
                    echo $data['job_id'];
                    $q="SELECT job_item.no,job_item.item_name FROM job_item WHERE job_id=".$data['job_id'];
                    $data_result=solve1($q);
                    $re=array();
                    $data_data="";
                    while($data_data=mysql_fetch_array($data_result))       
                    {
                        $r=array( 'filename'=>$data_data['item_name'],
                                  'no'=>$data_data['no']
                                  );

                        array_push($re,$r);
                    }
                    //print_r(json_encode($re));
                    $row['job_required_items']=array_merge($row['job_required_items'],$re);
                    //echo $data['job_id']."==>".var_dump($re);
                    //$rows['job_required_items']=$r;
                    //$rows['']
                    //$rows[$i]=$rows[$i]+$re;
                    //array_merge($rows[$i],$re['job_required_items']);
                    //array_push($rows[$i],re);
                    //print_r($re);
                    //$row[]=array('job_required_items'=>array());                  
                    //$rows[$i]=$rows[$i]+$re;
                    $r=array();
                    $re=array();
                    //unset($r);
                    //unset($re['job_required_items']);
                //print_r(json_encode($re));                                
                        //($rows);              
            $i++;
        }
        //print_r($rows);
        print_r(json_encode($rows));





?>

如何将一个单独的数组添加到关联数组中?

例如,我有一个数组 $re 和关联数组 required_item。 我想将 $re 添加到 required_item

<?php
$tab=1;/*$_GET['tab'];*/
$id=1111;/*$_GET['id'];*/
include_once('solve.php');
  $query="SELECT
  user_job.level1,
  user_job.tab_level,
  job.money_gain,
  job.exp_gain,
  job.energy_required,
  job.name,job.job_id
FROM user_job
RIGHT JOIN job USING(job_id) WHERE job.tab=".$tab." AND user_job.user_id=".$id." LIMIT 0,10";
     $result =solve1($query);
        while($data=mysql_fetch_array($result))
        {
            $i=0;
            $rows[]=array('name'=>array(
                                        'name_id'=>$data['name'],
                                        'tab_level'=>$data['tab_level']
                                        ),
                          'job_reward'=>array(
                                        'job_money'=>$data['money_gain'],
                                        'job_exp'=>$data['exp_gain'],
                                        'job_energy'=>$data['energy_required']
                                        ),
                          'job_required_items'=>array()

                        );
                    echo $data['job_id'];
                    $q="SELECT job_item.no,job_item.item_name FROM job_item WHERE job_id=".$data['job_id'];
                    $data_result=solve1($q);
                    $re=array();
                    $data_data="";
                    while($data_data=mysql_fetch_array($data_result))       
                    {
                        $r=array( 'filename'=>$data_data['item_name'],
                                  'no'=>$data_data['no']
                                  );

                        array_push($re,$r);
                    }
                    //print_r(json_encode($re));
                    $row['job_required_items']=array_merge($row['job_required_items'],$re);
                    //echo $data['job_id']."==>".var_dump($re);
                    //$rows['job_required_items']=$r;
                    //$rows['']
                    //$rows[$i]=$rows[$i]+$re;
                    //array_merge($rows[$i],$re['job_required_items']);
                    //array_push($rows[$i],re);
                    //print_r($re);
                    //$row[]=array('job_required_items'=>array());                  
                    //$rows[$i]=$rows[$i]+$re;
                    $r=array();
                    $re=array();
                    //unset($r);
                    //unset($re['job_required_items']);
                //print_r(json_encode($re));                                
                        //($rows);              
            $i++;
        }
        //print_r($rows);
        print_r(json_encode($rows));





?>

how do I add one separate array into associative array?

For example, I have an array $re and I have associative array required_item.
I want to add $re to required_item.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

输什么也不输骨气 2024-09-15 19:35:52
$row['jon']=$re
$row['jon']=$re
如歌彻婉言 2024-09-15 19:35:52

也许是 array_merge ?

$new_array = array_merge($jon,$re);

array_merge maybe ?

$new_array = array_merge($jon,$re);
关于从前 2024-09-15 19:35:51

假设您刚刚添加的数组是最后一个:

$row[count($row) - 1]['jon'] = array_merge($row[count($row) - 1]['jon'], $re);

如果该假设不正确,那么您必须知道其中包含“jon”的数组的索引(我在这里将其称为 $index):

$row[$index]['jon'] = array_merge($row[$index]['jon'], $re);

如果您不知道索引,并且无法确定它是 $row 中的最后一个条目,那么您必须通过以某种方式搜索数组来找出索引。

Assuming the array you've just added is the last one:

$row[count($row) - 1]['jon'] = array_merge($row[count($row) - 1]['jon'], $re);

If that assumption isn't correct, then you have to know the index of the array with 'jon' in it (which I'll call $index here):

$row[$index]['jon'] = array_merge($row[$index]['jon'], $re);

If you don't know the index, and you can't be sure it's the last entry in $row, then you'll have to figure out the index by searching the array somehow.

青衫负雪 2024-09-15 19:35:51

而不是 $rows[]=array('name'=>array(...) 使用类似 $newrow = array('name'=>array(...< 将您想要的所有内容添加到 $newrow 中,完成后将其附加到 $rows 中: $rows[] = $newrow;

然后 仅通过一次查询即可获取所有信息,也许传输所有冗余数据的开销并不理想。
但请至少考虑(服务器端)准备好的语句。这也将处理当前代码中可能的 sql 注入。

更新:
使用 $rows[] 您无法获得修改新附加元素所需的索引/键。这就是为什么你必须使用 end()/current() 或 Ryan Kinal 所示的东西。另一方面,如果您使用临时变量,则不需要键,即临时变量。变量或多或少是关键。由于 php 对数组使用写时复制,因此这样做的开销非常小。

服务器端准备好的语句将减少 mysql 服务器解析每个查询所需的时间。由于您的依赖查询( FROM job_items )非常简单,因此这种影响肯定是完全微不足道的。但使用它们还有另一个好处:您还可以获得命名参数查询。现在,您将参数混合到 sql 语句中,例如在 WHERE job.tab=".$tab." 中。如果由于某种原因 $tab 为空或包含字符串,在最好的情况下您会得到一个 sql 语法错误,在最坏的情况下会得到一个不同的、可能有害的 sql 语句。这不仅适用于从用户输入获取的参数(在本例中为 $_GET['tab']),还适用于混合到语句中的任何参数,例如 WHERE job_id=".$data['job_id' ]
使用服务器端准备好的参数化查询,实际参数将与 SQL 语句分开传输,因此服务器区分语句和参数没有问题。只需在 stackoverflow 中搜索“sql 注入”和“准备好的语句”主题,肯定有成百上千个关于此的帖子。

Instead of $rows[]=array('name'=>array(... use something like $newrow = array('name'=>array(.... Then add all the stuff you want to $newrow and when you're done append it to $rows: $rows[] = $newrow;.

Btw: It would be possible to query all the information with only one query. Maybe the overhead for transferring all that redundant data isn't desirable.
But please at least consider (server-side) prepared statements. This would also take care of the possible sql injections in your current code.

update:
With $rows[] you don't get the index/key you need to modify the newly appended element. That's why you'd have to use end()/current() or something like Ryan Kinal showed. On the other hand if you use a temporary variable you don't need the key, the temp. variable is more or less the key. And since php uses copy-on-write for arrays there's very little overhead in doing so.

Server-side prepared statements would reduce the amount of time it takes the mysql server to parse each query. Since your dependant query ( FROM job_items ) is quite simple this effect would most certainly be completely insignificant. But there's another benefit from using them: You also get named parameter queries. Right now you mix the parameters into the sql statement like e.g. in WHERE job.tab=".$tab.". If for some reason $tab is empty or contains a string you'll get an sql syntax error in the best case or a different, possibly harmful sql statement in the worst case. This doesn't only apply to parameters you get from user input (in this case $_GET['tab']) but any parameter you mix into the statement, like e.g. WHERE job_id=".$data['job_id'].
With server-side prepared, parametrized queries the actual arguments are transferred apart from the sql statement and therefore the server has no problem differentiating both the statement and the arguments. Just search stackoverflow for the topics "sql injection" and "prepared statements", there must be hundreds, thousands of posts about this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文