49600

Find value in array and get ID in PHP

Question:

I've 2 arrays with different structures (dimension), i need to find values (post_name) of $first in $second and then get ID from $second in order to create a unique array with post_name of $first and corresponding ID founded in $second. i hope i'm clear enough...

I need to keep the same order of first Thanks

$first= Array ( [0] => 'lundi-5', [1] => 'mardi-5', [2] => 'lundi-1', [3] => 'mardi-1', ); $second=Array ( [0] => WP_Post Object ( [ID] => 2878 [post_name] => lundi-1 ) [1] => WP_Post Object ( [ID] => 3180 [post_name] => mardi-1 ) [2] => WP_Post Object ( [ID] => 3181 [post_name] => lundi-5 ) [3] => WP_Post Object ( [ID] => 3182 [post_name] => mardi-5 )

Answer1:

Just try with:

$keys = array_map(function ($post) { return $post->ID; }, array_filter($second, function ($post) use ($first) { return in_array($post->post_name, $first); }));

Output:

array(2) { [2]=> int(3181) [3]=> int(3182) }

To return whole objects, use:

$posts = array_filter($second, function ($post) use ($first) { return in_array($post->post_name, $first); });

Recommend

  • R XML - combining parent and child nodes(w same name) into data frame
  • Check if string contains another C
  • Converting Southing/Easting GPS coordinates to Latitude/Longitude
  • Macro in Outlook to delete duplicate emails-
  • Google Analytics - Using Client ID as a custom dimension
  • Numpy: How to get rid of the minima along axis=1, given the indices - in an efficient way?
  • Slowly changing dimension - What is Pure type 6 implementation
  • SQL append distinct values from two columns and two tables
  • Struct pointer casts
  • Hibernate: Inheritance and relationship mapping + generics
  • Can you use DataSet and DataTables in a Portable Class Library
  • Should I use composite primary keys in Grails?
  • conditions for accessors in Coldfusion ORM
  • in batch how do i use taskkill properly
  • Retrieving specified columns from a list of csv files to create a data data frame in R
  • Clear activity stack before launching another activity
  • Assign variable to the value in HTML
  • How to use carriage return with multiple line?
  • Jackson Parser: ignore deserializing for type mismatch
  • Refering to the class itself from within a class mehod in Objective C
  • Breeze - Deleted Items nav properties bug
  • Initializer list vs. initialization method
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • javaw.exe and eclipse startup problems
  • Can Jackson SerializationFeature be overridden per field or class?
  • Date difference with leap year
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Perl system calls when running as another user using sudo
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Comma separated Values
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • need help with bizarre java.net.HttpURLConnection behavior
  • IndexOutOfRangeException on multidimensional array despite using GetLength check