I have two arrays that i need to join and then return at the end of the function. Here is what is in both arrays is the style of print_r()
Array 1:
Array (
[0] => Main Door
[1] => Clock
[2] => Production corridor
[3] => Warehouse Corridor
[4] => Production corridor
[5] => Warehouse Corridor
[6] => Production corridor
)
Array 2:
Array (
[0] => 08:04:14
[1] => 08:04:29
[2] => 08:07:10
[3] => 08:36:34
[4] => 08:40:40
[5] => 08:58:33
[6] => 09:00:58
)
So these two arrays correspond with each other so Main Door out of the first array goes with 08:04:14 out of the second array and so on, so what would be the best way to put these two arrays in to one where they are joined like that?
if you want results like array('Clock', '08:04:29'):
array_combine($a1, $a2);
otherwise:
$new = array();
foreach($a1 as $k => $v) {
$new[$k] = array($v, $a2[$k]);
}
eg:
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
and go through the below link for more examples,
http://php.net/manual/en/function.array-merge.php
This should do:
$a1 = array(0 => 'main door', 1 => 'clock');
$a2 = array(0 => '08:04:14', 1 => '08:04:29');
$length = count($a1);
$a3 = array();
for ($i = 0; $i < $length; ++$i) {
$a3[] = array($a1[$i], $a2[$i]);
// if you want to keep the indexes the same, use $a3[$i] instead
}
var_dump($a3);
Example
Using that code you can access the data like this:
$desc = $a3[0][0]; // main door
$time = $a3[0][1]; // 08:04:14
Related
From a function I am given a multidimensional array like this:
array(
[0] => array(
[0] => 7,
[1] => 18
),
[1] => array(
[0] => 12,
[1] => 7
),
[2] => array(
[0] => 12,
[1] => 7,
[2] => 13
)
)
I need to find duplicate values in the 3 arrays within the main array. For example, if value 7 repeats in the 3 arrays, return 7.
<?php
$array = array(array(7,18), array(12,7), array(12, 7, 13));
$result = array();
$first = $array[0];
for($i=1; $i<count($array); $i++){
$result = array_intersect ($first, $array[$i]);
$first = $result;
}
print_r($result);//7
?>
use my custom function
function array_icount_values($arr,$lower=true) {
$arr2=array();
if(!is_array($arr['0'])){$arr=array($arr);}
foreach($arr as $k=> $v){
foreach($v as $v2){
if($lower==true) {$v2=strtolower($v2);}
if(!isset($arr2[$v2])){
$arr2[$v2]=1;
}else{
$arr2[$v2]++;
}
}
}
return $arr2;
}
$arr = array_icount_values($arry);
echo "<pre>";
print_r($arr);
exit;
OUPUT
Array
(
[7] => 3
[18] => 1
[12] => 2
[13] => 1
)
hope this will sure help you.
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
use this code
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
You will need to loop through the first array, and for each value in it, see if it is in_array().
$findme = array();
foreach ($array[0] as $key => $value)
{
if (in_array ($value, $array[1]) && in_array ($value, $array[2]))
{
$findme[] = $value;
}
}
// $findme will be an array containing all values that are present in all three arrays (if any).
My problem is this: I'm creating an Array in order to store these 2 types of 'refinement'. However, what is happening is as the information is collected from the database, each 'refinement' is assigned to it's own specific entry when the arrays are created within the while loop.
while($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)){
etc...
}
So for instance, the 1st array would be a reference to 'Die Hard' and the 2nd, 'Breaking Bad' and the 3rd, 'Greys Anatomy'. What i'm trying to achieve is to merge them into 1 single array.
Array
(
[genreType] => Action
[mediaType] => Film
)
Array
(
[genreType] => Action
[mediaType] => TV
)
Array
(
[genreType] => Drama
[mediaType] => TV
)
Thanks for any help.
Try looking at this, http://php.net/manual/en/function.array-merge.php
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
OUTPUT
Array (
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
Why can't you just use array_merge? from the php docs http://php.net/manual/en/function.array-merge.php
while{$row...
{
$movies[] = $row;
//OR
$tmp['genreType'] = $row['genre'];
//and the like.....
$movies[] = $tmp;
}
Resulting in
$movies = array(
array(
"title"=>"Die Hard",
"genreType"=>"Action",
"mediaType"=>"Film"
),
array(
"title"=>"some other movie",
"genreType"=>"Comedy",
"mediaType"=>"TV"
)
);
like this?
I need to combine 2 arrays under the same key by appending Array2 to Array1:
Array1
(
[0] => value1
[1] => value2
[2] => value3
[3] => value4
[4] => value5
[5] => value6
[6] => value7
)
Array2
(
[0] => add1
[1] => add2
[2] => add3
[3] => add4
[4] => add5
[5] => add6
[6] => add7
)
so that Array3 looks like this:
Array1
(
[0] => value1add1
[1] => value2add2
[2] => value3add3
[3] => value4add4
[4] => value5add5
[5] => value6add6
[6] => value7add7
)
I searched through php.net but I wasn't able to find anything. Any help would be much appreciated! Thank you in advance for your input.
array_mapdocs makes looping superfluous in this case:
$arr1 = array('value1','value2','value3');
$arr2 = array('add1','add2','add3');
$merged = array_map(function($x, $y) { return $x . $y; }, $arr1, $arr2);
If you don't have PHP5.3+ (or you don't like lambda) you'll need to define the closure in a separate function and reference that function's name instead inside your array_map call.
Just do a loop:
if (count($arr1) == count($arr2))
{
$arr3 = array();
foreach ($arr1 as $key => $val)
$arr3 = $val . $arr2[$key];
}
else
{
echo "Arrays should be of same size!";
$arr3 = array();
for ($i = 0, $i < min(count($arr1),count($arr2)); $i++)
$arr3 = $arr1[$i] . $arr2[$i];
}
(Exact implementation should depend on your input values)
Soemthing like this:
foreach($array1 as $k=> $value){
$array3[$k] = $value . $array2[$k];
}
print_r($array3);
Mind you, it will only take as many items as $array1 contains. If $array2 is larger, other values will be ignored.
i have this situation $test:
$months = Array
(
[3] => 7.56
[7] => 11.94
[1] => 6.90
[17] => 6.90
[6] => 6.90
[4] => 19.50
)
$total = Array
(
[31] => 10
[17] => 4
)
i would like to combine them in such way that ill get $x = array([17] => 6.90)
basically put together the values from $months and the keys from $total where the $months.key = $total.key
any ideas?
thanks
You should try array_intersect_keys (doc)
From the doc:
<?php
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
var_dump(array_intersect_key($array1, $array2));
?>
output
array(2) {
["blue"]=>
int(1)
["green"]=>
int(3)
}
"array_intersect_key() returns an array containing all the entries of array1 which have keys that are present in all the arguments."
So if you want always the "values" of the first array, this is solution.
$x = array();
foreach($total as $key => $value)
{
if (array_key_exists($key, $months))
{
$x[$key] = $months[$key];
}
}
foreach ($total as $k => $v) if (isset($months[$k])) $x[$k] = $months[$k];
If I'm not mistaken, this may be a job for array merge.
$x = array_merge($months,$total);
http://php.net/manual/en/function.array-merge.php
I have two arrays of animals (for example).
$array = array(
array(
'id' => 1,
'name' => 'Cat',
),
array(
'id' => 2,
'name' => 'Mouse',
)
);
$array2 = array(
array(
'id' => 2,
'age' => 321,
),
array(
'id' => 1,
'age' => 123,
)
);
How can I merge the two arrays into one by the ID?
#Andy
http://se.php.net/array_merge
That was my first thought but it doesn't quite work - however array_merge_recursive might work - too lazy to check right now.
This does what Erik suggested (id no. as array key) and merges vlaues in $array2 to $results.
$results = array();
foreach($array as $subarray)
{
$results[$subarray['id']] = array('name' => $subarray['name']);
}
foreach($array2 as $subarray)
{
if(array_key_exists($subarray['id'], $results))
{
// Loop through $subarray would go here if you have extra
$results[$subarray['id']]['age'] = $subarray['age'];
}
}
First off, why don't you use the ID as the index (or key, in the mapping-style array that php arrays are imo)?
$array = array(
1 => array(
'name' => 'Cat',
),
2 => array(
'name' => 'Mouse',
)
);
after that you'll have to foreach through one array, performing array_merge on the items of the other:
foreach($array2 as $key=>$value) {
if(!is_array($array[$key])) $array[$key] = $value;
else $array[$key] = array_merge($array[key], $value);
}
Something like that at least. Perhaps there's a better solution?
<?php
$a = array('a' => '1', 'b' => array('t' => '4', 'g' => array('e' => '8')));
$b = array('c' => '3', 'b' => array('0' => '4', 'g' => array('h' => '5', 'v' => '9')));
$c = array_merge_recursive($a, $b);
print_r($c);
?>
array_merge_recursive — Merge two or more arrays recursively
outputs:
Array
(
[a] => 1
[b] => Array
(
[t] => 4
[g] => Array
(
[e] => 8
[h] => 5
[v] => 9
)
[0] => 4
)
[c] => 3
)
#Andy
I've already looked at that and didn't see how it can help merge multidimensional arrays. Maybe you could give an example.
#kevin
That is probably what I will need to do as I think the code below will be very slow.
The actual code is a bit different because I'm using ADOdb (and ODBC for the other query) but I'll make it work and post my own answer.
This works, however I think it will be very slow as it goes through the second loop every time:
foreach($array as &$animal)
{
foreach($array2 as $animal2)
{
if($animal['id'] === $animal2['id'])
{
$animal = array_merge($animal, $animal2);
break;
}
}
}
foreach ($array as $a)
$new_array[$a['id']]['name'] = $a['name'];
foreach ($array2 as $a)
$new_array[$a['id']]['age'] = $a['age'];
and this is result:
[1] => Array
(
[name] => Cat
[age] => 123
)
[2] => Array
(
[name] => Mouse
[age] => 321
)
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
With PHP 5.3 you can do this sort of merge with array_replace_recursive()
http://www.php.net/manual/en/function.array-replace-recursive.php
You're resultant array should look like:
Array (
[0] => Array
(
[id] => 2
[name] => Cat
[age] => 321
)
[1] => Array
(
[id] => 1
[name] => Mouse
[age] => 123
)
)
Which is what I think you wanted as a result.
I would rather prefer array_splice over array_merge because of its performance issues, my solution would be:
<?php
array_splice($array1,count($array1),0,$array2);
?>
$new = array();
foreach ($array as $arr) {
$match = false;
foreach ($array2 as $arr2) {
if ($arr['id'] == $arr2['id']) {
$match = true;
$new[] = array_merge($arr, $arr2);
break;
}
}
if ( !$match ) $new[] = $arr;
}