0

I have Array1 that pulls all of the entries for a Wordpress custom post type, and I have Array2 from another table that shows if that post type has been seen by a specific user. If the ID of Array1 & the PageID of Array2 are the same, then add the [Started] & [Finished] fields from Array2 to Array1. Basically I want to combine them to form Array3. I have tried many different solutions with array_combine but can't get the result I'm looking for.

Array1 ( 
[0] => stdClass Object ( [ID] => 75 [post_title] => Test Training Video ) 
[1] => stdClass Object ( [ID] => 80 [post_title] => Test 2 )
[2] => stdClass Object ( [ID] => 85 [post_title] => Test 2 ) ) 

Array2 ( 
[0] => stdClass Object ( [PageID] => 75 [Started] => 1 [Finished] => 1 )
[0] => stdClass Object ( [PageID] => 80 [Started] => 1 [Finished] => 0 ) )

Array3 ( 
[0] => stdClass Object ( [ID] => 75 [post_title] => Test Training Video [Started] => 1 [Finished] => 1 ) 
[1] => stdClass Object ( [ID] => 80 [post_title] => Test 2 [Started] => 1 [Finished] => 0 )
[2] => stdClass Object ( [ID] => 85 [post_title] => Test 2 ) ) 
3
  • Include your attempts in the question and also any errors that came from those attempts. Commented Apr 21, 2015 at 8:26
  • Is it possible to do on SQL level with JOIN rather that merging arrays? It could be a better solution. Commented Apr 21, 2015 at 8:37
  • Stepashka, in most cases join will be a bad thing for wordpress and there is no perfect solution:) Commented Apr 21, 2015 at 8:54

4 Answers 4

1

Something like this?

$array3 = array();
foreach( $array1 as $arr1 )
{
   foreach( $array2 as $arr2 )
   {
      if( $arr1["ID"] == $arr2["PageID"] )
      {
         $array3[] = array( $arr1["Started"], $arr2["Finished"] );
      } 
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

There are not many other solutions. The only other way would be to remap the "PageID" keys to "ID" via an array_map call and the use array_merge_recursive() on the two arrays.
0

array_combine() is not the right function for this.

In your case, you need to use a foreach-loop to loop over the values of your first array and compare them with your second array.

You also need to rearrange array 2, so that you can easily access the values for started and finished using the pageID as key:

$pageMap = array();
foreach($array2 as $entry) {
    $pageMap[$entry['PageID']] = array('started' => $entry['Started'], 'finished' => $entry['Finished']);
}

Then you can do:

$combined_array = array();
foreach($array1 as $post) {
    if(!isset($pageMap[$post['ID']])) continue; // do nothing if there are no started/finished entries.
    $combined_array[$post['ID']] = array_merge($post, $pageMap[$post['ID']]);
}

Comments

0

You could try a first loop to make your array "ID" match and then combine both arrays

foreach( $array2 as $key=>$value )
   {
      $value['ID'] = $value['PageId'];
      unset($value['PageId']);
      $array2[$key] = $value;
   }

$array3 = array_merge_recursive($array1,$array2);

1 Comment

You could avoid the foreach() loop if you can use an SQL ALIAS on any of the two queries to make sure the "ID" key is the same on both side and then only use the array_merge_recursive
0

After trying the posted solutions I decided to make the change in my $wpdb->get_results sql statement instead of using 2 different sql statements and then combining the 2 different arrays. With help from this post - SQL query to join two tables despite the other table may not have values, I was able to use the following to get the desired result.

   $oneBigQuery = $wpdb->get_results("SELECT a.ID, a.post_title, b.PageID, b.Started, b.Finished
FROM $wpdb->posts AS a
LEFT JOIN exampleTableName AS b
    ON a.ID = b.PageID
    AND b.UserID = 3
WHERE a.post_type = 'custom-post-type'
    ");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.