php array_combine only if keys match - php

I have
$map = array('id' => 'clmId', 'name' => 'clmName' => 'value' => 'clmValue',);
$value = array('id' => 1, 'name' => 'Foo', 'value' => 'Bar',);
and I want to get
$expected = array('clmId' => 1, 'clmName' => 'Foo', 'clmValue' => 'Bar');
of course I did $expected = array_combine($map, $value) and it works most of the time, but fails (to my surprise) for following
$map = array('id' => 'clmId', 'name' => 'clmName' => 'value' => 'clmValue',);
$value = array('name' => 'Foo', 'id' => 1, 'value' => 'Bar',);
$expected = array_combine($map, $value);
//you get
//$expected = array('clmId' => Foo, 'clmName' => 1, 'clmValue' => 'Bar');
clearly, array_combine is not meant for combining associative arrays. What can be done to achieve this ?
I am doing a primitive foreach($map as $key => $mapValue) { ... but I am guessing a smarter map/reduce or some cool array function should do it for me.
Return array()/FALSE in case $value has no corresponding key from $map

function combine_if_same_keys( $array_one, $array_two ) {
$expected = false;
ksort($array_one);
ksort($array_two);
$diff = array_diff_key($array_one, $array_two);
if( empty($diff) && count($array_one) == count($array_two) ) {
$expected = array_combine( $array_one, $array_two );
}
return $expected;
}
Returns false if the keys don't match and an array if they did match.
$map = array('id' => 'clmId', 'name' => 'clmName', 'value' => 'clmValue');
$value = array('id' => 1, 'name' => 'Foo', 'value' => 'Bar');
$value2 = array('ids' => 1, 'name' => 'Foo', 'value' => 'Bar');
var_dump( combine_if_same_keys( $map, $value ) );
var_dump( combine_if_same_keys( $map, $value2 ) );
Outputs:
array(3) { ["clmId"]=> int(1) ["clmName"]=> string(3) "Foo" ["clmValue"]=> string(3) "Bar" }
bool(false)
Edit: Benchmarking
Just read some comments on another answer which suggested that ksort() will cause a performance hit, so I did a bit of benchmarking Ran ksort() on 2 arrays with (albeit numeric keyed arrays) 10,000,000 keys each which only took 0.010757923126221 seconds on
Intel Q8200 # 2.33ghz (4CPUs), 3072MB RAM, Windows 7 x64 .
Edit (by OP): Number of keys should match as well
array_diff_key($a1, $a2) returns empty array even if count($a2) > count($a1), the array_combine returns FALSE while generating a warning.
One can suppress it by #array_combine, but I would rather put the count condition (along with the appropriate empty() test on the array_key_diff return) and then combine the array.

I think there is no builtin way
function combine_assoc($map, $values) {
$output = array();
foreach($map as $key => $values) {
if(!array_key_exists($key, $value)) return FALSE;
$output[$value] = $values[$key];
}
return $output;
}
Of course, you could simply sort your arrays by key first, but this does not take care of missing key/value pairs and has a somewhat decreased performance:
ksort($map);
ksort($value);
$output = array_combine($map, $value);
Version without foreach loop, which does check for matching keys, but I do not recommend it, since it will not perform well …
function combine_assoc_slow($map, $value) {
ksort($map);
ksort($value);
if(array_keys($map) != array_keys($value)) return FALSE;
return array_combine($map, $value);
}

So array_combine doesn't work if the order is different? Well, easiest solution for you: sorting both arrays by key!
$map = array('id' => 'clmId', 'name' => 'clmName' => 'value' => 'clmValue');
$value = array('name' => 'Foo', 'id' => 1, 'value' => 'Bar');
ksort($map);
ksort($value);
// keys are "aligned", ready to combine
$expected = array_combine($map, $value);

Related

PHP array_filter on array containing multiple arrays

I'm using array_filter in PHP to split an array containing multiple arrays when the value of a key named type matches a specific string. Here's what this looks like:
Sample Array
$arr[] = Array (
[0] => Array ( [type] => Recurring ... )
[1] => Array ( [type] => Single ... )
)
Functions
function recurring($value)
{
return ($value['type'] == 'Recurring');
}
function single($value)
{
return ($value['type'] == 'Single');
}
Split Arrays
$recurring = array_filter($arr, 'recurring');
$single = array_filter($arr, 'single');
This works, but I was curious if there was a way to simplify it so that I could create additional filtered arrays in the future without creating a new function for each.
I've started setting up a single function using a closure, but I'm not sure how to do it. Any ideas?
function key_type($value, $key, $string) {
return $key == 'type' && $value == $string;
}
$recurring = array_filter($arr,
key_type('Recurring'), ARRAY_FILTER_USE_BOTH);
$single = array_filter($pricing,
key_type('Single'), ARRAY_FILTER_USE_BOTH);
You could actually do what you proposed in your question. You just need to have the key_type() function return a callable function, which is what array_filter expects as the second parameter. You can return an anonymous function and pass the argument into the anonymous function using the use keyword as CBroe mentioned in the comments.
Here is an example:
function key_type($key) {
return function($value) use ($key) {
return $value['type'] == $key;
};
}
$arr = array(
array('type'=>'Recurring'),
array('type'=>'Single')
);
print_r(array_filter($arr, key_type('Single'), ARRAY_FILTER_USE_BOTH));
The above code will output:
Array ( [1] => Array ( [type] => Single ) )
The beauty of this method is that if you need to change the logic for all instances where you need to use your filter, you just have to change it one time in your key_type function.
An approach would be like below, however I don't like it honestly.
$array = [['type' => 'Single'], ['type' => 'Recurring']];
function key_type($value) {
global $string;
return $value['type'] == $string;
}
($string = 'Recurring') && ($recurring = array_filter($array, 'key_type'));
($string = 'Single') && ($single = array_filter($array, 'key_type'));
Another way to achieve same thing is using Anonymous functions (closures). Don't think much about being DRY it seems nice:
$array = [['type' => 'Single'], ['type' => 'Recurring']];
$recurring = array_filter($array, function($value) {
return $value['type'] == 'Recurring';
});
$single = array_filter($array, function($value) {
return $value['type'] == 'Single';
});
This task might be more about grouping than filtering -- it is difficult to discern from the limited sample data.
As a general rule, I strongly advise against using variable variables in PHP code. It is better practice to store data in arrays for accessibility reasons.
If you only have the two mentioned type values in your project data, then the conditional can be removed entirely.
Code: (Demo)
$array = [
['type' => 'Recurring', 'id' => 1],
['type' => 'Single', 'id' => 2],
['type' => 'Other', 'id' => 3],
['type' => 'Recurring', 'id' => 4],
['type' => 'Single', 'id' => 5],
];
$result = [];
foreach ($array as $row) {
if (in_array($row['type'], ['Recurring', 'Single'])) {
$result[strtolower($row['type'])][] = $row;
}
}
var_export($result);
Output:
array (
'recurring' =>
array (
0 =>
array (
'type' => 'Recurring',
'id' => 1,
),
1 =>
array (
'type' => 'Recurring',
'id' => 4,
),
),
'single' =>
array (
0 =>
array (
'type' => 'Single',
'id' => 2,
),
1 =>
array (
'type' => 'Single',
'id' => 5,
),
),
)

Find array elements that have a certain key-name prefix

I have an associative array with lots of elements and want to get a list of all elements that have a key name with a certain prefix.
Example:
$arr = array(
'store:key' => 1,
'user' => 'demo',
'store:foo' => 'bar',
'login' => true,
);
// this is where I need help with:
// the function should return only elements with a key that starts with "store:"
$res = list_values_by_key( $arr, 'store:' );
// Desired output:
$res = array(
'store:key' => 1,
'store:foo' => 'bar',
);
You could simply do :
$arr = array(
'store:key' => 1,
'user' => 'demo',
'store:foo' => 'bar',
'login' => true,
);
$arr2 = array();
foreach ($arr as $array => $value) {
if (strpos($array, 'store:') === 0) {
$arr2[$array] = $value;
}
}
var_dump($arr2);
Returns :
array (size=2)
'store:key' => int 1
'store:foo' => string 'bar' (length=3)
This should work for you:
Just grab all keys which starts with store: with preg_grep() from your array. And then do a simple array_intersect_key() call to get the intersect of both arrays.
<?php
$arr = array(
'store:key' => 1,
'user' => 'demo',
'store:foo' => 'bar',
'login' => true,
);
$result = array_intersect_key($arr, array_flip(preg_grep("/^store:/", array_keys($arr))));
print_r($result);
?>
output:
Array
(
[store:key] => 1
[store:foo] => bar
)
From php 5.6 you can use array_filter:
$return = array_filter($array, function ($e) {
return strpos($e, 'store:') === 0;
}, ARRAY_FILTER_USE_KEY);
var_dump($return);
for earlier versions, you can use
$return = array_intersect_key($array, array_flip(array_filter(array_keys($array), function ($e) {
return strpos($e, 'store:') === 0;
})));
Demo.

Get path and value of all elements in nested associative array

Consider an associative array of arbitrary form and nesting depth, for example:
$someVar = array(
'name' => 'Dotan',
'age' => 35,
'children' => array(
0 => array(
'name' => 'Meirav',
'age' => 6,
),
1 => array(
'name' => 'Maayan',
'age' => 4,
)
),
'dogs' => array('Gili', 'Gipsy')
);
I would like to convert this to an associative array of paths and values:
$someVar = array(
'name' => 'Dotan',
'age' => 35,
'children/0/name' => 'Meirav',
'children/0/age' => 6,
'children/1/name' => 'Maayan',
'children/1/age' => 4,
'dogs/0' => 'Gili',
'dogs/1' => 'Gipsy'
);
I began writing a recursive function which for array elements would recurse and for non-array elements (int, floats, bools, and strings) return an array $return['path'] and $return['value']. This got sloppy quick! Is there a better way to do this in PHP? I would assume that callables and objects would not be passed in the array, though any solution which deals with that possibility would be best. Also, I am assuming that the input array would not have the / character in an element name, but accounting for that might be prudent! Note that the input array could be nested as deep as 8 or more levels deep!
Recursion is really the only way you'll be able to handle this, but here's a simple version to start with:
function nested_values($array, $path=""){
$output = array();
foreach($array as $key => $value) {
if(is_array($value)) {
$output = array_merge($output, nested_values($value, (!empty($path)) ? $path.$key."/" : $key."/"));
}
else $output[$path.$key] = $value;
}
return $output;
}
function getRecursive($path, $node) {
if (is_array($node)) {
$ret = '';
foreach($node as $key => $val)
$ret .= getRecursive($path.'.'.$key, $val);
return $ret;
}
return $path.' => '.$node."\n";
}
$r = getRecursive('', $someVar);
print_r($r);
All yours to place it in an array.

Get key of multidimensional array?

For example, I have multidimensional array as below:
$array = array (
0 =>
array (
'id' => '9',
'gallery_id' => '2',
'picture' => '56475832.jpg'
),
1 =>
array (
'id' => '8',
'gallery_id' => '2',
'picture' => '20083622.jpg'
),
2 =>
array (
'id' => '7',
'gallery_id' => '2',
'picture' => '89001465.jpg'
),
3 =>
array (
'id' => '6',
'gallery_id' => '2',
'picture' => '47360232.jpg'
),
4 =>
array (
'id' => '5',
'gallery_id' => '2',
'picture' => '4876713.jpg'
),
5 =>
array (
'id' => '4',
'gallery_id' => '2',
'picture' => '5447392.jpg'
),
6 =>
array (
'id' => '3',
'gallery_id' => '2',
'picture' => '95117187.jpg'
)
);
How can I get key of array(0,1,2,3,4,5,6)?
I have tried a lot of examples, but nothing has worked for me.
This is quite simple, you just need to use array_keys():
$keys = array_keys($array);
See it working
EDIT For your search task, this function should do the job:
function array_search_inner ($array, $attr, $val, $strict = FALSE) {
// Error is input array is not an array
if (!is_array($array)) return FALSE;
// Loop the array
foreach ($array as $key => $inner) {
// Error if inner item is not an array (you may want to remove this line)
if (!is_array($inner)) return FALSE;
// Skip entries where search key is not present
if (!isset($inner[$attr])) continue;
if ($strict) {
// Strict typing
if ($inner[$attr] === $val) return $key;
} else {
// Loose typing
if ($inner[$attr] == $val) return $key;
}
}
// We didn't find it
return NULL;
}
// Example usage
$key = array_search_inner($array, 'id', 9);
The fourth parameter $strict, if TRUE, will use strict type comparisons. So 9 will not work, you would have to pass '9', since the values are stored as strings. Returns the key of the first occurence of a match, NULL if the value is not found, or FALSE on error. make sure to use a strict comparison on the return value, since 0, NULL and FALSE are all possible return values and they will all evaluate to 0 if using loose integer comparisons.
Try this , I think it will help you.
foreach ($array as $key=>$value)
{
echo $key.'<br/>';
echo $value['id'].'<br/>';
echo $value['gallery_id'].'<br/>';
echo $value['picture'].'<br/><br/>';
}
sometimes it is to easy to find ;)
array_keys($array);
array_keys
Probably http://php.net/manual/en/function.array-keys.php ?
Convert your double dimensional array on your own:
$tmp = null
foreach($array as $key => $value) {
$tmp[] = $key;
}
print_r($tmp);
You mean something like this:
function getKeys($array)
{
$resultArr = array();
foreach($array as $subArr) {
$resultArr = array_merge($resultArr, $subArr);
}
return array_keys($resultArr);
}

PHP: get keys of independent arrays

I have two arrays.
Example of the first array:
$arrayOne = array
(
'fruit' => array(
'apples' => array(),
'oranges' => array(),
'bananas' => array()
),
'vegetables' => array(
'tomatoes' => array(),
'carrots' => array(),
'celery' => array(),
'beets' => array
(
'bears' => array(),
'battlestar-galactica' => array()
),
),
'meat' => array(),
'other' => array()
);
2nd:
$arrayTwo = array
(
'frewt' => array(
'aplz' => array(),
'orangeez' => array(),
'bunanahs' => array()
),
'vetchteblz' => array(
'toem8ohs' => array(),
'careodds' => array(),
'sell-R-e' => array(),
'beats' => array
(
'bare z' => array(),
'tablestar-neglectia' => array()
),
),
'neat' => array(),
'mother' => array()
);
Notice that the two arrays are in the exact same "format" (same number of dimensions, number of keys, order, etc., etc.), only the names of the keys differ. (The array keys basically hold all the data.)
I have a few variables that address the keys of the first array ($arrayOne). E.g. $one would address the first dimension of the first array, so it's value (string) would be one out of 'fruit', 'vegetables', 'meat' or 'other'.
$two would be 'apples' or 'oranges' or 'bananas' or 'tomatoes' or 'carrots', etc., you get the idea. (There's vars for each dimension)
As I said, those variables only address $arrayOne. I want to be able to address the keys in the second array too, though. Meaning, by looking at the value of $one I want to be able to get the array_key of both arrays.
$arrayOne = //...
$arrayTwo = //...
function getPosition(array $arr, $key) {
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr),
RecursiveIteratorIterator::SELF_FIRST);
$pos = array();
foreach ($it as $k => $v) {
if (count($pos) - 1 > $it->getDepth()) {
array_pop($pos);
$pos[$it->getDepth()]++;
}
elseif (count($pos) - 1 < $it->getDepth()) {
array_push($pos, 0);
}
else {
$pos[$it->getDepth()]++;
}
if ($k === $key) {
return $pos;
}
}
}
function getElementKey(array $arr, array $position) {
$cur = $arr;
$curkey = null;
foreach ($position as $p) {
reset($cur);
for ($i = 0; $i < $p; $i++) {
next($cur);
}
$curkey = key($cur);
$cur = current($cur);
}
return $curkey;
}
var_dump(getPosition($arrayOne, "battlestar-galactica"));
var_dump(getElementKey($arrayTwo, array(1, 3, 1)));
gives:
array(3) {
[0]=>
int(1)
[1]=>
int(3)
[2]=>
int(1)
}
string(19) "tablestar-neglectia"
You can feed the result of getPosition to getElementKey:
getElementKey($arrayTwo, getPosition($arrayOne, "battlestar-galactica"));
Check out array_keys from both arrays and then map the positions. Like array_keys for your arrays will give you -
$arrayKeyOne = array('fruit', 'vegetables', 'meat', 'other');
$arrayKeyTwo = array('frewt', 'vetchteblz', 'meat', 'mother');
Then a $arrayKeyTwo[array_search($one, $arrayKeyOne)] shuld give you what you want. Let know if this helps.

Categories