Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →
$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 => "")
);
share|improve this question
    
Show your code. – E_p Aug 2 at 20:41
1  
It's not possible to have duplicate array keys, $new_arr[] = array("title" => "General", text => "value1", "title" => will not work, as title is duplicate at the same level. – ArtisticPhoenix Aug 2 at 20:46
    
Your something like this is incorrect, an impossible array. Edit your question to show what you actually want. Construct the array manually if you have to. – user3137702 Aug 2 at 20:55
1  
What sort of "join" do you want to enforce? Do you want all records in $arr and only matching records (based on title) from $info_arr? What is there is a title in $info_arr that doesn't exist in $arr? – Mike Brant Aug 2 at 21:12
up vote 1 down vote accepted

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.

share|improve this answer
    
I didn't actually implement the PHP answer. The problem I was initially having was because I was using 2 queries, then trying to sort them out in php. But based @Don'tPanic answer, it was better to to solve the problem in the sql query. I used JOIN, NOT IN and combined it all with UNION. – Chad Aug 3 at 15:55

As stated in the comments, the exact $new_arr you're requesting isn't possible. However, I think this will give you a similar result that should work for your purposes:

foreach ($info_arr as $infoArray) {
    $found = array_search(array('title' => $infoArray['title']), $arr);
    if (false !== false) {
        unset($arr[$found]);
    }
}
$new_arr = array_merge($info_arr, $arr);

It works by removing the "duplicates" from the original $arr before doing the array_merge().

If it's important to add an empty text value for the remaining items in $arr, do this before the array_merge():

foreach ($arr as &$arrArray) {
    $arrArray['text'] = '';
}

The resulting array will look like this:

$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' => '',
    ),
);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.