Let's say I have two arrays such as:
$arrayOne = array(1, 2, 3, 4, 5, 6, 7, 8);
$arrayTwo = array(2, 4, 6);
How do I get an $arrayThreesuch that:
$arrayThree = array(1, 3, 5, 7, 8);
<?php
$arrayOne = array(1, 2, 3, 4, 5, 6, 7, 8);
$arrayTwo = array(2, 4, 6);
$result = array_diff($arrayOne, $arrayTwo);
print_r($result);
?>
Output:
Array ( [0] => 1 [2] => 3 [4] => 5 [6] => 7 [7] => 8 )
Documentation.
Well..you can use the function array_diff() to find the difference between the arrays and display the result using print_r() function
An example would be
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
Will give output like
Array
(
[1] => blue
)
Try
<?php
$arrayOne = array(1, 2, 3, 4, 5, 6, 7, 8);
$arrayTwo = array(2, 4, 6);
$arrayThree = array_diff($arrayOne, $arrayTwo);
echo "<pre>";
print_r($arrayThree);
echo "</pre>";
?>
Related
I have a multidimensional array. I need to check if any value in this array has contain same value. If, then execute. What is the better way to check this, or the simplest way TIA
$array[] = array(5, 10, 15, 20, 25, 30);
$array[] = array(1, 2, 3, 4, 5, 6);
$array[] = array(2, 6, 8, 10, 12, 14);
Array
(
[0] => Array
(
[0] => 5
[1] => 10
[2] => 15
[3] => 20
[4] => 25
[5] => 30
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
[2] => Array
(
[0] => 2
[1] => 6
[2] => 8
[3] => 10
[4] => 12
[5] => 14
)
)
If I understood your question correctly, you are looking for a way of finding values that appears in more than one of the inner arrays..? Here are two solutions for that, using some built-in PHP array methods.
Setup
Flatten $array (initial step for both methods) using array_merge on itself
Code:
$array[] = array(5, 10, 15, 20, 25, 30);
$array[] = array(1, 2, 3, 4, 5, 6);
$array[] = array(2, 6, 8, 10, 12, 14, 5);
// 5, 10, 15, 20, 25, 30, 1, 2, 3, 4, 5, 6, 2, 6, 8, 10, 12, 14, 5
$array = call_user_func_array('array_merge', $array);
Method A
Get an array of unique values in $array (duplicates removed)
Get what was removed (= the duplicates) by comparing that array to the original $array
Make sure values appear only once in the final array
Code:
$duplicates =
array_unique(
array_diff_key(
$array,
array_unique($array)
)
);
// $duplicates = 5, 2, 6, 10
Method B
Get a list of how many times each value appears in $array
Filter that list keeping only values that appears more than once (= duplicates)
Get the keys of that list (the actual $array values)
Code:
$duplicates =
array_keys(
array_filter(
array_count_values($array),
function ($count) {
return $count > 1;
}
)
);
// $duplicates = 5, 10, 2, 6
Just loop through the array and subarray filling $isRepeated with values and frequencies of appearance. When $isRepeated[certain_value] exists means this value was found before:
$array[] = array(5, 10, 15, 20, 25, 30);
$array[] = array(1, 2, 3, 4, 5, 6);
$array[] = array(2, 6, 8, 10, 12, 14);
$isRepeated = array();
foreach($array as $subArray) {
foreach($subArray as $item) {
if (!isset($isRepeated[$item])) {
$isRepeated[$item] = 0;
} else {
$isRepeated[$item]++;
echo "\n<br>Item $item is repeated";
}
}
}
http://ideone.com/9yObII
Output:
Item 5 is repeated
Item 2 is repeated
Item 6 is repeated
Item 10 is repeated
I'm looking for sort an array WITHOUT foreach loop (direct command)...
for example :
<?php
$sortme = array( 10, 8, 17, 6, 22, 4, 3, 87, 1);
asort($sortme);
echo $sortme[0]; //Why this is not the lowest value (which is 1 on this case) ?!
//Is there any direct command sort array WITHOUT foreach loop ?
// iow...
// I need this :
// $sortme = array( 10, 8, 17, 6, 22, 4, 3, 87, 1);
// (here is the magic command) to become this :
// $sortme = array( 1, 3, 4, 6, 8, 10, 17, 22, 87);
?>
Thanks !
sounds like you just need sort()
<?php
$sortme = array( 10, 8, 17, 6, 22, 4, 3, 87, 1);
sort($sortme);
echo '<pre>';
print_r($sortme);
echo '</pre>';
echo 'First: '.$sortme[0];
?>
Result :
Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 6
[4] => 8
[5] => 10
[6] => 17
[7] => 22
[8] => 87
)
First: 1
Here's one way you could do this:
<?php
$fruits = array("3" => "lemon", "4" => "orange", "2" => "banana", "1" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
Should give you this output:
1 = apple
2 = banana
3 = lemon
4 = orange
I have 3 arrays:
array(1, 5, 1);
array(3, 2, 7, 5 ,4);
array(4, 3, 6, 5)
I want to merge them and get such result:
array(
array(1, 3, 4),
array(5, 2, 3),
array(1, 7, 6),
array(5, 5),
array(4)
);
What is the easiest way?
$arrs = array(
array(1, 5, 1),
array(3, 2, 7, 5, 4),
array(4, 3, 6, 5),
);
$result = array();
$num_arrs = count($arrs);
for($i = 0; $i < $num_arrs; $i++){
$size = count($arrs[$i]);
for($j = 0; $j < $size; $j++){
if(!isset($result[$j]))
$result[$j] = array();
$result[$j][] = $arrs[$i][$j];
}
}
Demo
Assuming you have the latest version of PHP (5.5), you can use this:
$input = array(
array(1,5,1),
array(3,2,7,5,4),
array(4,3,6,5)
);
$output = array_column($input,null);
If you don't have the latest version, see the original PHP version for a shim.
Alternatively, for this specific case (ie. a specialised shim), try this:
$input = array(...); // see code block above
$output = array();
foreach($input as $arr) foreach($arr as $i=>$v) $output[$i][] = $v;
$arrays = array(
array(1, 5, 1),
array(3, 2, 7, 5 ,4),
array(4, 3, 6, 5)
);
$combined = array();
foreach($arrays as $array) {
foreach($array as $index => $val) {
if(!isset($combined[$index])) $combined[$index] = array();
$combined[$index][] = $val;
} }
After that $combined holds what you want.
Is this what you want?
You can use array_map
<?php
$a = array( 1, 5, 1 );
$b = array( 3, 2, 7, 5 ,4 );
$c = array( 4, 3, 6, 5 );
$d = array_map( function() {
return array_filter( func_get_args() );
}, $a, $b, $c );
print_r( $d );
?>
Output
Array
(
[0] => Array
(
[0] => 1
[1] => 3
[2] => 4
)
[1] => Array
(
[0] => 5
[1] => 2
[2] => 3
)
[2] => Array
(
[0] => 1
[1] => 7
[2] => 6
)
[3] => Array
(
[1] => 5
[2] => 5
)
[4] => Array
(
[1] => 4
)
)
In PHP, I have 2 multidimensional arrays. such as:
array 1:
[[1, 2, 3], [1, 2, 3]];
Array 2:
[[4, 5, 6], [7, 8, 9]];
I need to combine these two arrays.
I need the first array's subarray values to be the keys of resulting multidimensional's subarrays and the second array's subarray values to be the values of resulting multidimensional's subarray.
I need the output like this format:
array (
0 =>
array (
1 => 4,
2 => 5,
3 => 6,
),
1 =>
array (
1 => 7,
2 => 8,
3 => 9,
),
)
Try with:
$length = sizeof($arrayA);
$output = array();
for ( $i = 0; $i < $length; ++$i ) {
$output[] = array_combine($arrayA[$i], $arrayB[$i]);
}
This can be concisely accomplished by calling array_combine() while simultaneously iterating (mapping) the two equal-length arrays with equal-length subarrays.
Code: (Demo)
$arr1 = [[1, 2, 3], [1, 2, 3]];
$arr2 = [[4, 5, 6], [7, 8, 9]];
var_export(
array_map('array_combine', $arr1, $arr2)
);
Output:
array (
0 =>
array (
1 => 4,
2 => 5,
3 => 6,
),
1 =>
array (
1 => 7,
2 => 8,
3 => 9,
),
)
Code:
$data = array("vanilla", "strawberry", "mango", "peaches");
print_r(array_slice($data, 1, 2));
Output:
Array
(
[0] => strawberry
[1] => mango
)
in my case :
$data = array("vanilla", "strawberry", "mango", "peaches");
$sub_set_data = array( "strawberry", "mango");
the Output will be the remain array:
array("vanilla", "peaches");
EDIT:
NOT array diff it look like the minus operator 7-3 = 4 or $C = $A-$B
python concept:
>>> A = [6, 7, 8, 9, 10, 11, 12]
>>> subset_of_A = [6, 9, 12];
>>> set(A) - set(subset_of_A)
set([8, 10, 11, 7])
>>>
How Can I do for this case?
Looks like you need array_diff
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
?>
Result:
Array
(
[1] => blue
)
http://www.php.net/manual/en/function.array-diff.php