dskld5423 2016-08-02 20:40
浏览 48
已采纳

PHP array_intersect / array_diff / combine数组

$arr[] = array('title' => 'Overview');
$arr[] =array('title' => 'General');
$arr[] =array('title' => 'History');
$arr[] =array('title' => 'Construction');
$arr[] =array('title' => 'Plan');
$arr[] =array('title' => 'Other');

$info_arr[] = array("title" => "General", text => "value1");
$info_arr[] = array("title" => "History", text => "value1");
$info_arr[] = array("title" => "Construction", text => "value1");
$info_arr[] = array("title" => "Plan", text => "value1");

I need to be able merge these arrays together.So they look something like this. As I will need to loop thru the consolidated array. Other, Overview do not have any text values but still need to placed into the array.

$new_arr[] = array("title" => "General", text => "value1", "title" => "History", text => "value1", "title" => "Construction", text => "value1"
,"title" => "Plan", text => "value1","title" => "Overview", text => "","title" => "Other", text => "");

I have tried for loops (using count value), foreach loops, I thought array_intersect or array_diff don't see to solve the issue. This should not be so difficult, but I'm trying to piece together some really bad legacy code. Or the cube/florescent lights might have finally got to me.

Update:

 while ($stmt->fetch()) {
    $arr[] = array("title" => $Title);
}

and

while ($dstmt->fetch()) {
   $info_arr[] = array("title" => $descriptionType, "descriptiontext" => $descriptionText); , "descriptiontext" => $val );
      }

$dstmt & $stmt are queries.

I thought this would work but not so much

$r = array_intersect($arr, $info_arr);
var_dump($r);

Something like this Let me clarify:

$new_arr = array(array("title" => "General", text => "value1"),
    array("title" => "History", text => "value1"),
    array("title" => "Construction", text => "value1"),
    array("title" => "Plan", text => "value1"),
    array("title" => "Overview", text => ""),
    array("title" => "Other", text => "")
);
  • 写回答

2条回答 默认 最新

  • doujiu8826 2016-08-02 22:26
    关注

    If you want to work with these two arrays, you can just use the title as the key in $r.

    foreach (array_merge($arr, $info_arr) as $x) {
        $r[$x['title']]['title'] = $x['title'];
        $r[$x['title']]['text'] = isset($x['text']) ? $x['text'] : '';
    }
    

    Or, you can go back a step and avoid having separate arrays by building the $r array in the same manner as you fetch your query results:

    while ($stmt->fetch()) {
        $r[$Title] = array('title' => $Title, 'text' => '');
    }
    while ($dstmt->fetch()) {
        $r[$descriptionType] = array("title" => $descriptionType, "text" => $descriptionText);
    }
    

    Or, ideally, you could go back another step and avoid having separate queries by using a JOIN to get the same results in one query, but there's nothing in the question on which to base any specific suggestion for that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?