Need to compare two arrays
Working example
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
Array1 Output:
Array ( [a] => green [0] => red [1] => blue )
When I do Like this
$array1 = array();
while($fetch = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$array1[] = $fetch['color'];
}
I get this output:
Array ([0] => gren [1] => red [2] blue
How do I add the "a" to the array and make the first color be number zero?
This adding the "a" but it gets the zero number
array_unshift($array1,"a");
LIKE
Array ( [0] => a [1] => green
I want this
Array ( [a] => green [0]
I'm not sure why you want to do this, but here's how:
$array1 = array();
while ($fetch = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
if (empty($array1)) {
$array1['a'] = $fetch['color'];
} else {
$array1[] = $fetch['color'];
}
}
$arr = array(0 => 'green', 1 => 'red', 2 => 'blue');
$res = array_merge(array('a' => current($arr)), array_slice($arr, 1));
You can do with array_merge and array_shift function:
$arr = array(0 => 'green', 1 => 'red', 2 => 'blue');
$new = array_merge(array('a' => array_shift($arr)), $arr);
Demo: http://codepad.org/osifrZKZ
Related
This question already has answers here:
Merge two arrays as key value pairs in PHP
(3 answers)
Closed 9 months ago.
I have the following array:
Array
(
[0] => James
[1] => Mike
[2] => Liam
[3] => Shantel
[4] => Harry
)
Array
(
[0] => Green
[1] => Blue
[2] => Yellow
[3] => Purple
[4] => Red
)
How can I get these two arrays into a JSON object?
So this should be the expected output:
{"James":"Green","Mike":"Blue","Liam":"Yellow","Shantel":"Purple"}
This is what I tried doing but I'm getting a totally different output:
$final = array();
$names = ['James', 'Mike', 'Liam', 'Shantel', 'Harry']
$colors = ['Green', 'Blue', 'Yellow', 'Purple', 'Red']
for ($i = 0; $i <= 4; $i++) {
$final[] = array_push($final, $names[$i], $colors[$i]);
}
What am I doing wrong here?
You want to set a specific key of $final to a specific value. So instead of using array_push (or $final[]), which just adds a value to an indexed array, you want to define the key/value of the associated array $final like:
$final[$names[$i]] = $colors[$i];
$final = array();
$names = ['James', 'Mike', 'Liam', 'Shantel', 'Harry'];
$colors = ['Green', 'Blue', 'Yellow', 'Purple', 'Red'];
foreach($names as $i => $key) {
$final[$key] = $colors[$i];
}
Working example at https://3v4l.org/cgHtD
Try array_combine()
$a = ["James", "Mike", "Liam", "Shantel", "Harry"];
$b = ["Green", "Blue", "Yellow", "Purple", "Red"];
$c = array_combine($a, $b);
print_r($c);
echo json_encode($c);
output:
Array
(
[James] => Green
[Mike] => Blue
[Liam] => Yellow
[Shantel] => Purple
[Harry] => Red
)
{"James":"Green","Mike":"Blue","Liam":"Yellow","Shantel":"Purple","Harry":"Red"}
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
Ideally, I'd like the ability to add a 3rd array into an array of 2 arrays. I've tried array_push, array_merge, and array_merge_recursive. Here is the relevant code:
$array1 = array("color" => "red", "shape" => "triangle");
$array2 = array("color" => "green", "shape" => "trapezoid");
$array3 = array("color" => "blue", "shape" => "square");
$result = array($array1, $array2);
$result = array_merge($result, $array3);
print_r($result);
This current code returns: Array ( [0] => Array ( [color] => red [shape] => triangle ) [1] => Array ( [color] => green [shape] => trapezoid ) [color] => blue [shape] => square )
The problem with it is I need it to number the 3rd array as well. So, [0], [1], and [2]
You're merging an array of strings ($array3) with an array of arrays ($result).
To achieve the result you want, you should either do
$result = array($array1, $array2, $array3);
or use array_push() instead of array_merge()
$result = array($array1, $array2);
array_push($result, $array3);
$array1 = array("color" => "red", "shape" => "triangle");
$array2 = array("color" => "green", "shape" => "trapezoid");
$array3 = array("color" => "blue", "shape" => "square");
$result = array($array1, $array2);
array_push($result, $array3);
array_push is the way to go because you will add the new array to the array of arrays. The issue with array_merge is that it takes the contents of $array3 (not the array itself) and adds them to $result.
When you said that you previously tried array_push I'm guessing that you used it incorrectly like this: $result = array_push($result, $array3); which will overwrite the result you're looking for with the length of the created array, rather than the array you're creating.
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).
I would like to find out a duplicate value in the all nested arrays within an array.
At the moment my array is something like that.
Array $bigarray = Array (
[431] => Array (
[0] => orange
[1] => apple
[2] => pine
)
[440] => Array (
[0] => orange
[1] => lilly
)
[444] => Array (
[0] => orange
[1] => pine
)
)
I would like to extract only orange which is in all
arrays('431','440','444').
Woudl you give me some idea...?
Thanks in advance.
You can use array_intersect():
$intersected = null;
foreach ($bigarray as $arr) {
$intersected = $intersected ? array_intersect($arr, $intersected) : $arr;
if (!$intersected) {
break; // no reason to continue
}
}
print_r($intersected);
Array
(
[0] => orange
)
$inAllChunks = call_user_func_array('array_intersect',(array_values($bigarray)));
var_dump($inAllChunks);
$output = null;
foreach ( $bigarray as $array ) {
if ( is_null($output) ) {
$output = $array;
continue;
}
$output = array_intersect($output, $array);
if ( empty($output) ) {
break;
// there are no common elements in the array
}
}
var_dump$(output);
From the documentation.
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
http://www.php.net/manual/en/function.array-intersect.php