I have two arrays like this
$arr1 = Array('fn', 'ln', 'em');
$arr2 = Array('fn'=>'xyz', 'ano' => 'abc', 'ln'=>'122', 'em' => 'a#b.com', 'db'=>'xy');
I want to create an array from arr2 with all the elements from $arr1. So the result should be like this.
$result = Array( 'fn'=>'xyz', 'ln'=>'122', 'em'='a#b.com');
Don't want to loop.
Any idea?
The order of arguments is important here
print_r(array_intersect_key($arr2, array_flip($arr1)));
You can use array_map for this.
// PHP 5.3+ only
$result = array_combine($arr1, array_map(function($a) use($arr2){
return $arr2[$a];
}, $arr1));
DEMO: http://codepad.viper-7.com/Y1aYcf
If you have PHP < 5.3, you can do some trickery with array_intersect_key and array_flip.
$result = array_intersect_key($arr2, array_flip($arr1));
DEMO: http://codepad.org/MuydURQT
You just have to loop, as in create a new array or maybe check some array set in mathematics functions. I think, maybe, insection might work.
Related
I need a function to solve this issue.
Example:
$ar1 = array("alpha" => array("A","B","C","D"), ...);
$ar2 = array("numerics" => array("1","2","3","4"), ...);
$output = merge_arrays($ar1,$ar2);
print_r($output);
Result:
array("A","B","C","D","1","2","3","4");
$output = call_user_func_array('array_merge',
array_values(array_merge_recursive($ar1,$ar2)));
print_r($output);
You can merge this 2 arrays like this:
$new_array = array_merge($ar1['alpha'], $ar2['numerics']);
In this way you will have an array like the one you describe.
You can check more information about array_merge here: http://php.net/manual/en/function.array-merge.php
I have a 2D array like
attendee_programs = [1 =>[100,101],
2 =>[100,101,102]
];
I want to get array_values() and array_unique() but only for the nested elements (sorry, not sure what the terminology is) ...I.E.
programs = [100,101,102];
Is there a php function for this? or do I need to loop through and assemble it manually?
Edit: All of the answers are very helpful and have shown me something new. Sorry I can only accept one.
You could use a clever combination of array_unique, array_reduce and array_merge to achieve this:
$a = array_unique(array_reduce($attendee_programs, 'array_merge', []));
Doing this might be end in an array with some gaps in the indizes - if you need gaples array keys, you have to add array_values at the end
$a = array_values($a);
You can use:
call_user_func_array('array_merge', array_values($attendee_programs));
to get values of nested array.
array_unique(call_user_func_array('array_merge', array_values($attendee_programs)));
to get unique values.
RecursiveIteratorIterator
RecursiveArrayIterator
Solution:
function flatten($array)
{
$rit = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
return iterator_to_array($rit, true);
}
echo '<pre>';
print_r(flatten($attendee_programs));
Result:
Array
(
[0] => 100
[1] => 101
[2] => 102
)
Yet another option:
$flat = array();
array_walk_recursive($attendee_programs, function($value) use (&$flat) {
$flat[] = $value;
});
$flat = array_unique($flat);
I have 2 arrays like so:
$array1 = array(
array("foo"=>"bar","count"=>"3"),
array("foo2"=>"bar2","count"=>"4"),
array("foo3"=>"bar3","count"=>"2")
);
$array2 = array(
array("foo4"=>"bar","count"=>"3"),
array("foo5"=>"bar2","count"=>"4"),
array("foo6"=>"bar3","count"=>"2")
);
how can i add the 3rd element of array2 into array1 so it can become like this:
$array1 = array(
array("foo"=>"bar","count"=>"3"),
array("foo2"=>"bar2","count"=>"4"),
array("foo3"=>"bar3","count"=>"2"),
array("foo6"=>"bar3","count"=>"2")
);
i have tried doing $array1 += $array2[2]; but it doesn't work. it just adds the keys from array("foo6"=>"bar3","count"=>"2") to array1 instead of adding it as an array in $array1
Could you help me out?
The [] operator appends an element to the end of an array, like this
$array1[] = $array2[2];
Just do like this:
$array1[] = $array2[2];
If you want the exact 3rd item, then you could do something like:
$array1[] = $array2[2];
If you want the last item of the array, you can use:
$array1[] = $array2[count($array2)];
try this
$array1[] = $array2[2];
array_merge() is a function in which you can copy one array to another in PHP.
http://php.net/manual/en/function.array-merge.php
How to add array items to an existing array with key => value ? actually i want to create an array of mysql rowset i.e.
$n =0;
while($row = mysql_fetch_array($rowset))
{
$array[$n] = array('name' => $row['name'], 'city' = $row['city']);
$n += 1;
}
Thanks.
Just try with:
$existingArray['newKey'] = 'new value';
Or use array_merge function:
$newArray = array_merge($existingArray, $additionalData);
http://php.net/manual/en/function.array-merge.php
That what you're looking for?
-edit-
Just to note, if conflicting results are found, the last most array entry will be used. If you array merge three arrays with id fields, only the final arrays id will be stored in the result.
You may want to look into this:
http://php.net/array_push
Should be simple enough.
For One:
$array['key'] = $value;
Merge:
$mergedArray = array_merge($array1, $array2);
(http://php.net/manual/en/function.array-merge.php)
I've an array like below :
$array = array('string'=>'hello','somethingeElse'=>'how r u', ....);
I wanna change the keys of array to numeric values (consecutive):
$array = array('1'=>'hello','2'=>'how r u','3'=>....);
any help would be appreciated ;)
You could use the array_values() function, which will basically do what you say.
array_values() returns all the values from the input array and indexes
numerically the array.
Example:
$array = array_values($array);
If you want to avoid PHP loops, you may try something like :
$newArray = array_combine(range(1, count($array)), array_values($array));