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

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) );

Related

Put a named key before numeric keys in array

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

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
)

how to remove the duplicate item from the object in PHP?

here's the sample array
array(
0 => blah object
(
[bagid] => 12345
[userid] => 12345,
and so on and so forth..
)
)
this is the output when I var_dump the $data object, if i loop it through
foreach loop,it will print the bagid,userid,and etc....of the owner of the data..
now the question is, I only want to display 1 unique bagid coming from a user...
no matter how many bagid the user has, is that doable ?how?
use array_unique
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
print_r($result);
?>
output :
Array
(
[a] => green
[0] => red
[1] => blue
)
http://php.net/manual/en/function.array-unique.php

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

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

Categories