How to merge multiple arrays from a single array variable ? lets say i have this in one array variable
Those are in one variable ..
$array = array(array(1), array(2));
Array
(
[0] => 1
)
Array
(
[0] => 2
)
how to end up with this
Array
(
[0] => 1
[1] => 2
)
This is the PHP equivalent of javascript Function#apply (generate an argument list from an array):
$result = call_user_func_array("array_merge", $input);
demo: http://3v4l.org/nKfjp
Since PHP 5.6 you can use variadics and argument unpacking.
$result = array_merge(...$input);
It's up to 3x times faster than call_user_func_array.
This may work:
$array1 = array("item1" => "orange", "item2" => "apple", "item3" => "grape");
$array2 = array("key1" => "peach", "key2" => "apple", "key3" => "plumb");
$array3 = array("val1" => "lemon");
$newArray = array_merge($array1, $array2, $array3);
foreach ($newArray as $key => $value) {
echo "$key - <strong>$value</strong> <br />";
}
array_merge can do the job
$array_meged = array_merge($a, $b);
after the comment
If fixed indexs you can use:
$array_meged = array_merge($a[0], $a[1]);
A more generic solution:
$array_meged=array();
foreach($a as $child){
$array_meged += $child;
}
$resultArray = array_merge ($array1, $array1);
$result = array();
foreach ($array1 as $subarray) {
$result = array_merge($result, $subarray);
}
// Here it is done
Something good to read:
http://ca2.php.net/manual/en/function.array-merge.php
Recursive:
http://ca2.php.net/manual/en/function.array-merge-recursive.php
$arr1 = array(0=>1);
$arr2 = array(0=>2);
$merged = array_merge($arr1,$arr2);
print_r($merged);
array_merge is what you need.
$arr = array_merge($arr1, $arr2);
Edit:
$arr = array_merge($arr1[0], $arr1[1]);
Related
I have two arrays like this:
$arr1 = Array ( [1] => ab [2] => cd [at] => ef )
$arr2 = Array ( [1] => gh [2] => ij [at] => kl )
I want to combine these two arrays to this array:
$arr3 = Array ( [1] => ab-gh [2] => cd-ij [at] => ef-kl )
The keys are the same in the two arrays so I imagine that it is possible to join the values of the arrays with a "-" between like above.
I have googled and tried almost anything (merge, combine, join) but I can't figure it out. Any suggestions?
If you can guarantee these two arrays will always have the same keys:
$arr3 = array();
foreach ($arr1 as $key => $val1) {
$val2 = $arr2[$key];
$arr3[$key] = $val1 . "-" . $val2;
}
Now, assuming the two have different keys and you want to do a "joined" array with only the keys that exist in both arrays:
$arr3 = array();
foreach (array_intersect(array_keys($arr1), array_keys($arr2)) asĀ $key) {
$arr3[$key] = $arr1[$key] . "-" . $arr2[$key];
}
This is similar to a SQL inner join.
If you want to do a "joined" array with the keys in either of the arrays, hyphen-joining only the ones that exist in both:
$arr3 = array();
foreach (array_merge(array_keys($arr1), array_keys($arr2)) as $key) {
if (isset($arr1[$key]) && isset($arr2[$key])) {
$arr3[$key] = $arr1[$key] . "-" . $arr2[$key];
} else {
$arr3[$key] = (isset($arr1[$key]) ? $arr1[$key] : $arr2[$key]);
}
}
This is similar to a SQL outer join.
Try all of the above online!
Let's say you've got two arrays with the same amount of values and the same keys.
This should help:
$arr1 = Array ("ab", "cd", "at" => "ef");
$arr2 = Array ("gh", "ij", "at" => "kl");
$combinedArray = array();
function combineValues(array $array1, array $array2)
{
$array3 = array();
foreach($array1 as $key => $value)
{
$array3[$key] = $value . "-" . $array2[$key];
}
return $array3;
}
$combinedArray = combineValues($arr1, $arr2);
I really hope this helps.
Regards.
Although accepted answer is perfectly ok, here is a more general solution, that can be used for any number of arrays:
$arr1 = [1 => 'ab', 2 => 'cd', 'at' => 'ef'];
$arr2 = [1 => 'gh', 2 => 'ij', 'at' => 'kl'];
$arr3 = [1 => 'mn', 2 => 'op', 'at' => 'qr'];
$arr = [$arr1, $arr2, $arr3];
$keys = array_intersect(...array_map('array_keys', $arr));
$result = array_combine($keys, array_map(function ($key) use ($arr) {
return implode('-', array_column($arr, $key));
}, $keys));
Here is the demo.
I want difference of multidimensional array with single array. I dont know whether it is possible or not. But my purpose is find diference.
My first array contain username and mobile number
array1
(
array(lokesh,9687060900),
array(mehul,9714959456),
array(atish,9913400714),
array(naitik,8735081680)
)
array2(naitik,atish)
then I want as result
result( array(lokesh,9687060900), array(mehul,9714959456) )
I know the function array_diff($a1,$a2); but this not solve my problem. Please refer me help me to find solution.
Try this-
$array1 = array(array('lokesh',9687060900),
array('mehul',9714959456),
array('atish',9913400714),
array('naitik',8735081680));
$array2 = ['naitik','atish'];
$result = [];
foreach($array1 as $val2){
if(!in_array($val2[0], $array2)){
$result[] = $val2;
}
}
echo '<pre>';
print_r($result);
Hope this will help you.
You can use array_filter or a simple foreach loop:
$arr = [ ['lokesh', 9687060900],
['mehul', 9714959456],
['atish', 9913400714],
['naitik', 8735081680] ];
$rem = ['lokesh', 'naitik'];
$result = array_filter($arr, function ($i) use ($rem) {
return !in_array($i[0], $rem); });
print_r ($result);
The solution using array_filter and in_array functions:
$array1 = [
array('lokesh', 9687060900), array('mehul', 9714959456),
array('atish', 9913400714), array('naitik', 8735081680)
];
$array2 = ['naitik', 'atish'];
$result = array_filter($array1, function($item) use($array2){
return !in_array($item[0], $array2);
});
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => lokesh
[1] => 9687060900
)
[1] => Array
(
[0] => mehul
[1] => 9714959456
)
)
The same can be achieved by using a regular foreach loop:
$result = [];
foreach ($array1 as $item) {
if (!in_array($item[0], $array2)) $result[] = $item;
}
I have two arrays, the following:
$arr1 = array("Key1"=>1, "Key2"=>2, "Key3"=>3);
My second array is the following:
$arr2 = array("Key2", "Key3");
What I would like to get is the values where Key2 and Key3 matches. I would also like those values to be returned as an array so I end up with the following:
array(2,3)
Thanks for any help.
Just use of 3 three array functions to achieve this.
$arr1 = array("Key1"=>1, "Key2"=>2, "Key3"=>3);
$arr2 = array("Key2", "Key3");
$arr3 = array_values(array_intersect_key($arr1, array_flip($arr2)));
print_r($arr3);
The output:
Array ( [0] => 2 [1] => 3 )
$arr1 = array("Key1"=>1, "Key2"=>2, "Key3"=>3);
$arr2 = array("Key2", "Key3");
$result = array();
foreach($arr1 as $key => $value) {
if(in_array($key, $arr2)) {
array_push($result, $arr1[$key]);
}
}
var_dump($result);
or as mentioned in the comments:
$arr1 = array("Key1"=>1, "Key2"=>2, "Key3"=>3);
$arr2 = array("Key2", "Key3");
$result = array_intersect_key($arr1, array_flip($arr2));
var_dump($result);
How to merge many Arrays in php
$array1="Array ( [0] => mouse ) Array ( [0] => mac ) Array ( [0] => keyboard )";
how i can array like this
Array( [0] =>mouse [1] => mac [2] =>keyboard );
depending on how your arrays are being stored, there are a couple of options.
Here are 2 examples:
<?php
$old_array = array(
array('mouse'),
array('mac'),
array('keyboard')
);
$new_array = array();
foreach($old_array as $a){
$new_array[] = $a[0];
}
echo '<pre>',print_r($new_array),'</pre>';
//// OR ////
$array1 = array('mouse');
$array2 = array('mac');
$array3 = array('keyboard');
$new_array = array_merge($array1,$array2,$array3);
echo '<pre>',print_r($new_array),'</pre>';
You should use array_merge function of PHP.
This:
<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>
will return this:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
It's as easy as a piece of cake! :)
Try it: http://www.w3schools.com/php/showphp.asp?filename=demo_func_array_merge
Tutorials:
http://www.w3schools.com/php/func_array_merge.asp
http://php.net/array_merge
So you basically have an array of arrays. You can do this in the following way:
$array = array(array(0 => 'mouse'), array(1 => 'mac'), array(2 => 'keyboard'));
$mergedArray = array();
foreach ($array as $part) {
$mergedArray = array_merge($mergedArray, $part);
}
var_dump($mergedArray);
The result is exactly what you would expect:
array(3) {
[0]=>
string(5) "mouse"
[1]=>
string(3) "mac"
[2]=>
string(8) "keyboard"
}
If you also have scalars in the big array, you can modify the loop to the following:
foreach ($array as $part) {
if (!is_array($part)) {
$mergedArray[] = $part;
} else {
$mergedArray = array_merge($mergedArray, $part);
}
}
Note: This will merge all values from all sub arrays, it's not limited to one entry per sub array.
php array_merge() function, Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
Merge N number of array using $array = array_merge( $array1 , $array2, $array3 ..... $arrayN);
$array1 = array ( '0' => 'mouse' );
$array2 = array ( '0' => 'mac' );
$array3 = array ( '0' => 'keyboard' );
$array = array_merge( $array1 , $array2, $array3);
echo "<pre>";
print_r($array);
echo "</pre>";
O/P:
Array
(
[0] => mouse
[1] => mac
[2] => keyboard
)
More info: http://us2.php.net/array_merge
I want to find the first occurrence of any of several values in an array.
$sentence = array(I, want, to, go, to, the, market);
if(in_array(array('swim','fly','go'), $sentence)) {
// Should return KEY = 3 as 'go' was found in the third key of the array
}
I'm sure this must be fairly common, how can it be done?
http://tr.php.net/manual/en/function.array-keys.php
I think more specifically you are looking for this type of functionality?
<?php
$array = array(0 => 100, "color" => "red");
print_r(array_keys($array));
$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
$array = array("color" => array("blue", "red", "green"),
"size" => array("small", "medium", "large"));
print_r(array_keys($array));
?>
The above example will output:
Array
(
[0] => 0
[1] => color
)
Array
(
[0] => 0
[1] => 3
[2] => 4
)
Array
(
[0] => color
[1] => size
)
first of all you should read manual properly of in_array function , you were passing parameters in wrong order
$sentence = array(I, want, to, go, to, the, market);
$found_key = array();
foreach($sentence as $key => $word)
{
if(in_array($word,array('swim','fly','go')))
{ $found_keys[] = $key;}
}
print_r($found_keys);
I hope this is what you need
Please use this function array_search it will return key of the element if element found in the array
Example
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
array_keys would work a treat.
$keys = array_keys($sentence,array('swim','fly','go');
Then $keys will be a list of keys where swim, fly and go appear in $sentence
Just as the code below will return a list of keys that have empty values
$array = array('one','','three');
$keys = array_keys($array,"");
function find_in_array($needles,$array) {
$pos = false;
foreach ($needles as $key => $value) {
if (in_array($value, $array)) {
if(!$pos || array_search($value, $array) < $pos) {
$pos = array_search($value, $array);
}
}
}
return $pos;
}
echo find_in_array($values,$sentence);
This is what I am using right now, I'm testing array_keys as suggested by Darian Brown