This question already has answers here:
Can't concatenate 2 arrays in PHP
(11 answers)
Closed 6 years ago.
I have two arrays, I need to merge both like layer
first array
Array (
[0]=>1
[1]=>2
[2]=>3
)
Second array
Array (
[0]=>4
[1]=>5
[2]=>6
)
How to combine it with a result of
Array (
[0]=>1
[1]=>2
[2]=>3
[3]=>4
[4]=>5
[5]=>6
)
If you don't need to do anything with indexes then the right function is array_merge so in this example:
array_merge($a, $b);
Interactive mode enabled
php > $a = [1, 2, 3];
php > $b = [4, 5, 6];
php > print_r(array_merge($a, $b));
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
Related
This question already has answers here:
Split array into a specific number of chunks
(7 answers)
Closed 9 months ago.
I am having a data in array format with key and value what I want is from first key 0 to key 6 data I want to store in a new array with index 0 same for every other key 7 to 13 key with 1 index in new array in PHP
First, as suggested above, do not upload image of your code, just paste it here.
Just use PHP array_chunk function:
$pieces = array_chunk($my_array, 6);
you can use array_chunk() function
<?php
$a = ["a","a","a","a","a","a","a","a"];
$b = array_chunk($a, 7);
echo"<pre>";print_r($b);
?>
output be like:
Array
(
[0] => Array
(
[0] => a
[1] => a
[2] => a
[3] => a
[4] => a
[5] => a
[6] => a
)
[1] => Array
(
[0] => a
)
)
This question already has answers here:
PHP: merge two arrays while keeping keys instead of reindexing?
(6 answers)
Closed 4 years ago.
I have a multiple arrays which I'd like to put into a single array in order to sort it:
$weight = array($weight);
$dev = array_combine($missing, $weight);
echo "<pre>";
print_r($dev);
echo "</pre>";
Output:
Array (
[angular] => 2
)
Array (
[android sdk] => 3
) Array (
[application] => 1
)
Now how do I turn the array above into this?
Array (
[android sdk] => 3
[angular] => 2
[application] => 1 )
I've tried the below from a solution that I've found on this site, but it returns NULL:
$weight = array($weight);
$dev = array_combine($missing, $weight);
$result = call_user_func_array("array_merge", $dev);
echo "<pre>";
print_r($result);
echo "</pre>";
EDIT
Here is my $missing array, some arrays are empty because a match hasn't been found against some keywords:
Array
(
)
Array
(
[0] => angular
)
Array
(
[0] => android sdk
)
Array
(
[0] => application
)
Array
(
)
Here are the value from $weight:
3 2 3 1 3
How can I get this?
Array (
[android sdk] => 3
[angular] => 2
[application] => 1 )
use array_merge:
$array1 = [1,2,3];
$array2 = [4,5,6];
$result = array_merge($array1, $array2);
print_r($result);
results in:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
You can use array_merge function.
Therefore, the code will be
array_merge($array1, $array2);
This question already has an answer here:
Merging 2 arrays by keys
(1 answer)
Closed 6 years ago.
I have two array $a and $b
Array
(
[0] => 230
[1] => 2
[2] => 71
)
Array
(
[0] => 1500
[1] => 3500
[2] => 5000
)
I want an array which will look like
Array
(
[230] => 1500
[2] => 3500
[71] => 5000
)
How is it possible?
The actual question should have led you to the answer. The way to combine two arrays is by using the array_combine function.
Update: here is a code sample http://codepad.org/6r5DzCmc
Use array_combine.
array_combine($a, $b);
try http://php.net/manual/en/function.array-combine.php
$a = array(230, 2, 71);
$b = array(1500, 3500, 5000);
print_r(array_combine($a, $b));
This question already has answers here:
How to "flatten" a multi-dimensional array to simple one in PHP? [duplicate]
(23 answers)
Closed 7 years ago.
How to merge only index of array itself, I have only one and I want to combine its index, I want to make 3d to 2d array. I just want to combine index of one array with in one index.This is one array,I am not asking for merge two different array.
Array
(
[0] => Array
(
[East] => 2
)
[1] => Array
(
[North] => 2
)
)
Now I want to format this array like below format.there can be nth no of indexes with in same array.
Array
(
[0] => Array
(
[East] => 2
[North] => 2
)
)
<?php
$a = array(0 => array('East' => 2), 1 => array('North' => 2));
$b[0] = $a[0];
for($i = 1; $i < count($a); $i++) {
$key = array_keys($a[$i])[0];
$b[0][$key] = $a[$i][$key];
}
print_r($b);
?>
Output:
Array
(
[0] => Array
(
[East] => 2
[North] => 2
)
)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Merging PHP array, one as Keys the other as Values?
I have the following two arrays in PHP, which represent atmospheric gases and their compositions:
Array
(
[0] => nitrogen
[1] => argon
[2] => oxygen
[3] => carbon dioxide
)
Array
(
[0] => 78
[1] => 1
[2] => 21
[3] => 0
)
Is there an elegant way of obtaining the following array, a combination of keys and values:
Array
(
"nitrogen" => 78
"argon" => 1
"oxygen" => 21
"carbon dioxide" => 0
)
The methods I can think of involve loops, I don't know if I'm complicating it. Is there a simple way?
array_combine(), simply:
$new_arr = array_combine($keys,$values);
use array_combine
$key = array('nitrogen', 'argon', 'oxygen', 'carbon dioxide');
$values = array(78, 1, 21, 0 );
$return = array_combine($key, $values);
print_r($return);
output:
Array
(
[nitrogen] => 78
[argon] => 1
[oxygen] => 21
[carbon dioxide] => 0
)
array_combine() Example:
<?php
print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
[a] => 2
[b] => 3
)
If two keys are the same, the second one prevails.
Believe it or not - array_combine() exists and is exactly what you are looking for!
http://php.net/manual/en/function.array-combine.php
array_combine — Creates an array by using one array for keys and
another for its values
Example adapted from the above link -
$a = array('Stack', 'Server', 'Super');
$b = array('Overflow', 'Fault', 'User');
$c = array_combine($a, $b);
print_r($c);
Array
(
[Stack] => Overflow
[Server] => Fault
[Super] => User
)
And there you have it - an array fit for a trilogy.