Array slice ($data,1,2) what equivalence for my case? - php

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

Related

Edit two arrays to match same length

I have two PHP arrays, $array1 and $array2:
$array1 = [55, 23, 45, 6, 0, 12];
$array2 = ['Apple', 'Oranges', 'Pears', 'Banana', 'Mango', 'Cherry'];
Is there a way to ensure that where there is a 0 in $array1 it is deleted and a new $array3 is formed with the 0 omitted. Secondly, the corresponding index value of $array2 is also deleted and a new $array4 is created with 'Mango' omitted, in this example.
Thus, $array3 and $array4 would be:
$array3 = [55, 23, 45, 6, 12];
$array4 = ['Apple', 'Oranges', 'Pears', 'Banana', 'Cherry'];
The important point here is that two new arrays are of equal length and maintain their corresponding indexes, if that makes sense.
You simply need to parse the first array and check if there is a 0 in it. If not add it to the new array the value corresponding to the index of Arrays2.
<?php
$Arrays1 = [55, 23, 45, 6, 0, 12];
$Arrays2 = ['Apple', 'Oranges', 'Pears', 'Banana', 'Mango', 'Cherry'];
$Arrays4 = array();
$search = 55;
foreach($Arrays1 as $index => $arr1_val){
if($arr1_val !== $search){
$Arrays4[] = $Arrays2[$index];
}else{
unset($Arrays1[$index]);
}
}
print_r($Arrays4); // Array ( [0] => Oranges [1] => Pears [2] => Banana [3] => Mango [4] => Cherry )
print_r($Arrays1); // Array ( [1] => 23 [2] => 45 [3] => 6 [4] => 0 [5] => 12 )

Get Difference in Arrays

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>";
?>

How can I extract only the indexes of the array_diff function?

How can I extract only the indexes of the array_diff function?
$array1 = array("a" => "green", "red", "blue", "red", "pink");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
print_r($result);
Instead of it showing: Array ( [1] => blue [3] => pink )
I want it to display only the indexes like this: 1, 3 (maybe inside a new array called $indexesresult)
(Reason being that I am comparing an online (mysqli) array with a localhost (mysqli) array and I have to remove whitepaces before I can compare the arrays- I tried hundreds of ways around this but to no avail: array_diff does not like any type of whitespaces). With the indexesresult I can then get the original values back into the arrays to display the differences in a neat tabular format.
The array_keys would help.
$array1 = array("a" => "green", "red", "blue", "red", "pink");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
$indexesresult=array_keys($result); //<----- Here
print_r($indexesresult);
OUTPUT :
Array
(
[0] => 1
[1] => 3
)
Just try with:
print_r( array_keys($result) );

Adding an array into an array of arrays

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.

How to remove elements of an array that match elements in another array

I'd like to write a function that finds matches of all elements within a one-dimensional non-associative array and removes those elements completely from another one-dimensional non-associative array, including the index. Here's an example below.
<?php
function magicfunc($colors, $remove) {
// some magic here
}
EXAMPLE:
$colors = array(
'red',
'green',
'blue',
'purple',
'green',
'yellow',
'pink',
'orange'
);
$remove = array(
'green',
'white',
'pink'
);
magicfunc($colors, $remove);
WOULD RETURN:
Array
(
[0] => red
[1] => blue
[2] => purple
[3] => yellow
[4] => orange
)
How can I achieve this? Notice how there may be elements that are matched more than once (green), and it's also possible that there are no elements that match a particular one to be removed (white). The function should not have issues with these contingencies.
Try array_diff : http://us3.php.net/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
)

Categories