I have two assoc array i want to creat one array out of that
E.g
a(a=>1
b=>3
f=>5
)
b(a=>4
e=>7
f=>9
)
output must be
c(
a=>1
b=>3
f=>5
a=>4
e=>7
f=>9
)
i am new in php
Use array_merge(). Your resulting array CAN NOT have more than one entry for the same key, so the second a => something will overwrite the first.
Use the + operator to return the union of two arrays.
The new array is constructed from the left argument first, so $a + $b takes the elements of $a and then merges the elements of $b with them without overwriting duplicated keys. If the keys are numeric, then the second array is just appended.
This ey difference of the + operator and the function, array_merge is that array merge overwrites duplicated keys if the latter arguments contain that key. The documentation puts it better:
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If the keys are different, then use array_merge()
<?php
$a1=array("a"=>"Horse","b"=>"Cat");
$a2=array("c"=>"Cow");
print_r(array_merge($a1,$a2));
?>
OUTPUT:
Array ( [a] => Horse [b] => Cat [c] => Cow )
If the keys are the same, then use array_merge_recursive()
<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>
OUTPUT:
Array
(
[color] => Array
(
[favorite] => Array
(
[0] => red
[1] => green
)
[0] => blue
)
[0] => 5
[1] => 10
)
Related
I want to need multiple array combaine in one array
I have array
array(
0=> test 1
)
array(
0=> test 2
)
array(
0=> test 3
)
I need expected output
`array(
0=>Test1
1=>Test2
2=>test3
)`
You can use the array_merge() for this. The array_merge() function merges one or more arrays into one array.If two or more array elements have the same key, the last one overrides the others.
Syntax:
array_merge(array ...$arrays): array
Example:
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
Result:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
You can check more here.
$a = array('test_1');
$b = array('test_2');
$c = array('test_3');
print_r(array_merge($a,$b,$c));
O/P - Array ( [0] => test_1 [1] => test_2 [2] => test_3 )
Hope you are doing well and good.
So, as per your requirement i found solution to get result.
$array1 = [0 => "Test 1"]; $array2 = [0 => "Test 2"]; $array3 = [0 => "Test 3"];
print_r(array_merge($array1,$array2,$array3));
In the above example you have to merge the n number of array with single array, so for that you need to use array function which is array_merge(array ...$array).
What is array_merge()?
The array_merge() function merges one or more arrays into one array.
Tip: You can assign one array to the function, or as many as you like.
Note: If two or more array elements have the same key, the last one overrides the others.
I have an array with some values (numeric values):
$arr1 = [1, 3, 8, 12, 23]
and I have another associative array that a key (which matches to a value of $arr1) correspond to a value. This array may contain also keys that don't match with $arr1.
$arr2 = [1 => "foo", 2 => "foo98", 3 => "foo20", 8 => "foo02", 12 => "foo39", 15 => "foo44", 23 => "foo91", 34 => "foo77"]
I want as return the values of $arr2 specifying as key the values of $arr1:
["foo", "foo20", "foo02", "foo39", "foo91"]
If possible, all this, without loops, using just PHP array native functions (so in an elegant way), or at least with the minimum number of loops possible.
Minimal loop is simple - 1. as:
foreach($arr1 as $k) {
$res[] = $arr2[$k];
}
You can do that with array_walk but I think this simple way is more readable.
If you insist you can do with array_filter + array_values + in_array as:
$res = array_values(array_filter($arr2,
function ($key) use ($arr1) { return in_array($key, $arr1);},
ARRAY_FILTER_USE_KEY
));
You can see this for more about filtering keys
To do it purely with array functions, you could do it as...
print_r(array_intersect_key($arr2, array_flip($arr1) ));
So array_flip() turns the items you want form the array into the keys for $arr1 and then uses array_intersect_key() to match the keys with the main array and this newly created array.
Gives...
Array
(
[1] => foo
[3] => foo20
[8] => foo02
[12] => foo39
[23] => foo91
)
If you don't want the keys - add array_values() around the rest of the calls...
print_r(array_values(array_intersect_key($arr2, array_flip($arr1) )));
to get
Array
(
[0] => foo
[1] => foo20
[2] => foo02
[3] => foo39
[4] => foo91
)
Although as pointed out - sometimes a simple foreach() is just as good and sometimes better.
I want to merge many different assoc arrays in one array but in the form of assoc array. Like i have different arrays like this
Array ( [0] => abc [1] => def [2] => ghi )
Array ( [0] => jkl [1] => mno [2] => pqr )
.
.
.
and want to make an array like
array
0 =>
array
0 => string 'abc'
1 => string 'def'
2 => string 'ghi'
1 =>
array
0 => string 'jkl'
1 => string 'mno'
2 => string 'pqr'
.
.
.`
.
i am getting these arrays from a csv file. Please help. Thanks
If I understand correctly, you don't want to merge the arrays... you just want to make a multidimensional array i.e. an array of arrays. See the difference here.
You are creating the initial arrays from the csv file, but I'll create them here for completeness:
$array1 = array ( "0" => "abc", "1" => "def", "2" => "ghi" );
$array2 = array ( "0" => "jkl", "1" => "mno", "2" => "pqr" );
Then all you need to do is create an array with those arrays as the values, depending on what works with the rest of your code, e.g.
$multiarray = array();
$multiarray["0"] = $array1;
$multiarray["1"] = $array2;
or
$multiarray = array ( "0" => $array1, "1" => $array2 );
If you print_r ($multiarray);, it will look like the example in your question.
By the way, the examples you have given are not associative arrays, but I've treated them as if they are in case you do actually need them.
If your arrays are just standard indexed arrays, then you don't need to specify the key, e.g.
$array1 = new array("abc", "def", "ghi");
etc
$multiarray[] = $array1;
$multiarray[] = $array2;
I provide another point of view, which is lest optimized for the task in itself as I see it but might be useful in some context.
$array = array('abc', 'def', 'ghi');
$array2 = array('jkl', 'mno', 'pqr');
function gather(... $array){return $array;}
my_print_r(gather($array, $array2));
That function uses the splat operator, which natively gathers whatever arguments are sent to the function as entries in an array, called array in that example. We can do whatever we want with array in that function, but just by retuning it, it does what you asked for.
Suppose I have an associative array with keys that are alphabetic string, and if I merge something to this array, It will merge successfully without reindexing like
$arr1 = array('john'=>'JOHN', 'marry'=>'Marry');
$arr1 = array_merge(array('78'=>'Angela'),$arr1);
print_r($arr1);
then this will correctly merge new component to array and its output will be
Array
(
[0] => Angela
[john] => JOHN
[marry] => Marry
)
But when I tried same thing like this
$arr1 = array('34'=>'JOHN', '04'=>'Marry');
$arr1 = array_merge(array('78'=>'Angela'),$arr1);
print_r($arr1);
then its output is like this
Array
(
[0] => Angela
[1] => JOHN
[04] => Marry
)
Can anyone describes this scenario.....
Also I want my array to be like this after merging..
Array
(
[78] => Angela
[34] => JOHN
[04] => Marry
)
How can I Achieve that??
as per definition array_merge will reindex numeric indexes. a string with a numeric value is also a numeric index.
To prevent this behaviour concatenate the arrays using $arr1+$arr2
You don't need to use array_merge() as you can simply add the arrays:
$arr1 = [
'10' => 'Angela',
'john' => 'JOHN',
'marry' => 'Marry',
];
$arr2 = [
'78' => 'Angela'
];
$arr3 = $arr2 + $arr1;
array_merge() - ... values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
How can I sort an array by value, but instead of changing the position of the values, change the position of the keys?
Array
(
[0] => 16
[1] => 12
[2] => 30
)
When I sort this array, I want to get the output like this:
Array
(
[1] => 16
[0] => 12
[2] => 30
)
Your starting array:
$a = [16, 12, 30];
First make a copy:
$b = $a;
Then use asort on one of them to sort it while maintaining the key association:
asort($a);
Then use array_combine with array_keys to create your result array using the the keys from the sorted array, and the values from the unsorted array.
$result = array_combine(array_keys($a), $b);