2

Currently I have this:

$pattern = array('industry_id','category_id','subcategory_id');

$data = array('advert_id' => string '261501' (length=6)
  'advert_type_id' => string '7' (length=1)
  'user_id' => string '6221' (length=4)
  'industry_id' => string '17' (length=2)
  'category_id' => string '769' (length=3)
  'subcategory_id' => string '868' (length=3)
  'model' => string 'Custom Semi Drop Deck Trailer' (length=29)
  'description' => string 'Industry: Trailer );

Then:

array_intersect_key(  $data , array_flip($pattern) );

Using array_interect_key & array_flip to get the values from $data based on $pattern, I will get a result like this:

array (size=3)
  'category_id' => string '769' (length=3)
  'subcategory_id' => string '930' (length=3)
  'industry_id' => string '17' (length=2)

Unfortunately as you can see the result key sorting is not the same that I declared in $pattern. Is there a shorthand way to sort it like I declared in $pattern because after this I want to implode the array and do something like this industry_id.category_id.subcategory_id without hard coding the keys.

dops
  • 800
  • 9
  • 17
Drixson Oseña
  • 3,631
  • 3
  • 23
  • 36

2 Answers2

4

Since you already figured out array_intersect_key method which will not get you the desired key ordering of $pattern, try this instead:

// since original $pattern is not ASSOC (only vals)
// flip it so defined vals become keys
$pattern_flipped = array_flip($pattern);
$result = array();
foreach ($pattern_flipped as $k => $v) {

    if (isset($data[$k])) {
        $result[$k] = $data[$k];
    }
}
var_dump($result); // test

// can use original 0 1 2 dynamic keys for concatenation
echo $result[$pattern[0]], $result[$pattern[1]], $result[$pattern[2]], '<br>';
// or use hardcoded keys
echo $result['industry_id'], $result['category_id'], $result['subcategory_id'], '<br>';
0

You know I'm not sure how you're getting the result you describe. I've tried your code and I get

array (size=3)
   'industry_id' => string '17' (length=2)
   'category_id' => string '769' (length=3)
   'subcategory_id' => string '868' (length=3)

You could do this another way though using array_filter

$newData = array_filter($data, function($key) use ($pattern) {
    if (in_array($key, $pattern))
        return true;
}, ARRAY_FILTER_USE_KEY)
dops
  • 800
  • 9
  • 17
  • If I'm not mistaken _array_filter_ method will do the match, and preserve keys but in order of the original $data array which is not what he wants. He wants _(if I understood correctly)_ the resulting array to have the same order of keys like $pattern. A simple way check my answer with a simple loop. – Ivan Veštić Nov 23 '15 at 00:42
  • @IvanVeštić array_filter iterates through the data in order so it doesn't matter what order the pattern is in, it will always come back in the order of the original data. That being said I got the right result with the OP's method too. Your loop works too, it's a matter of preference i think (although I haven't compared process speed) – dops Nov 23 '15 at 00:48
  • @IvanVeštić sorry just looking at it, I didn't read you're comment properly, the OP has their pattern in the order of the original data. I think they want ti in the original order. – dops Nov 23 '15 at 00:52
  • he did edits to originally posted question, and modified some key names, and added note below the sample code about not getting the result in the right order. So I assumed that he wants the result to have $pattern order of keys, but of course if both $data and $pattern order match initially multiple methods can be seemingly applied here. But if $data keys order change will no longer be getting expected result if $pattern keys order matter. P.S. thanks for the feedback in any case :) – Ivan Veštić Nov 23 '15 at 01:01
  • @IvanVeštić and the same to you, I've voted for your answer, I think I'll leave mine here though if nothing else as an example for reading the question ;) – dops Nov 23 '15 at 01:11