How can I unset the keys in one array where the values contained in a second array match the values in the first array?
Actual array:
$fruits = array('Banana','Cherry','Orange','Apple');
Elements I want to remove:
$remove = array('Banana','Apple');
Need to return:
$array = array('Cherry','Orange');
I know it's possible to remove each one with unset, but I'm looking to make it in one line with two array.
Thanks.
Take a look at this function
link
$arrayWithoutTheDesiredElements = array_diff($originalArr, $toRemoveArray)
EDIT:
for your case: $array = array_diff($fruits, $remove);
Related
If I have two arrays and I want to "combine" them into one array whilst maintaining each individual array and leaving the keys as they are as they are always duplicated each iteration, can I do this?:
$array1 = array('0'=>'Bob', '1'=>'Tom', '2'=>'John');
$array2 = array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan');
If I use array_merge:
$new_array = array_merge($array1, $array2);
I get:
array('0'=>'Bob','1'=>'Tom','2'=>'John','3'=>'Michelle','4'=>'Joan','5'=>'Susan')
whereas I want to get something like:
array(array('0'=>'Bob', '1'=>'Tom', '2'=>'John'),array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan'))
Create a new array and add the other arrays to that one like this:
$arr = array($array1, $array2);
$ new_array = array ($ array, $ array2);
I have a form field called $_POST['hidden-tags']
It has values entered in as follows
cars, vans, bikes, trains,
I on post im trying to split the values and save them into an array and then compare that array with another I have and only display the values that are different. I know the $arr1 has values as I have tested the data.
the code I have so far is
$arr1;
$arr2 = array();
foreach($_POST['hidden-tags'] as $value){
$arr2[] = explode(",",$value);
}
print_r($arr2);
// $tmp = array_diff_key($arr1, $arr2);
// echo $tmp;
parts of which I found here on stack
PHP explode array
As you can see I have the final bit commented out. This is so I can see the array values. If I echo the $arr2 all I see on screen is
Array()
even though I have entered cars, bikes, vans. I have not got as far as comparing the two array yet and displaying the $tmp variable
Is $_POST['hidden-tags'] a text input field? If so, you don't need to run a foreach on it to split it into an array. Just explode it and compare the generated array with the one you already have.
I don't think $_POST['hidden-tags'] is an array, but a string you want to split.
Also I think you don't want to save the result of the split action on your POST variable in the next index of $arr2, because split will return an array and this way you will have an array in an array.
$arr1;
$arr2 = array();
$arr2[] = explode(",",$_POST['hidden-tags']);
print_r($arr2);
$tmp = array_diff_key($arr1, $arr2);
echo $tmp;
i have also got a similar question. I have an array where i need to extract parts from the keys of array and combine them. can you please suggest the best way for it.
$myarray=Array(
[0]=>'unwanted text'
[1]=>'unwanted+needed part1'
[2]=>'needed part2'
[3]=>'needed part3'
[4]=>'unwanted text'
)
how can i extract only the needed parts and combine them. Thanks a lot ahead.
Not exactly sure if this does what you want, but looping and copying to a new array should basically achieve your result (once you clarify how you decide which part of the strings are needed or unwanted)
$myarray = array(…);
$new_array = array();
$unwanted = 'some_string';
foreach($myarray as $k => $v) {
$new_value = preg_replace("/^$unwanted/", '', $v); # replace unwanted parts with empty string (removes them);
if(!empty($new_value)) { # did we just remove the entry completely? if so, don't append it to the new array
$new_array[] = $v; # or $new_array[$k] if you want to keep indices.
}
}
Assuming you want to join array entries and have a string as result, use the implode function of PHP: $string = implode(' ', $new_array);
The functions you are after, depending on what you want to do, will be a combination of the following: array_splice, array_flip, array_combine.
array_splice()
allows you to extract part of an array by key offset. It'll permanently remove the elements from the source, and return these elements in a new array
array_flip()
Turns keys into values and values into keys. If you have multiple identical values, the last one has precedence.
array_combine() takes two parameters: an array of keys, and an array of values, and returns an associative array.
You'll need to provide more info as to what you want to do, though, for my answer to be more specific.
I have a comma separated string which i explode into an array. If the array is of un-known length and i want to make it into a key value pair array where each element in the array has the same key, how do i do this? i'm assuming i'd have to use array_combine? can anyone give me an example using the array bellow? :
for instance:
array([0]=>zebra, [1]=>cow, [2]=>dog, [3]=>monkey, [4]=>ape)
into:
array([animal]=>zebra, [animal]=>cow, [animal]=>dog, [animal]=>monkey, [animal]=>ape)
You can't use the same key for each element in your array. You need a unique identifier to access the value of the array. When you use animal for all, what value should be used? What you can do is to make a 2 dimensional array that you have an array inside an array:
array(
[animals] => array(
[0]=>zebra, [1]=>cow, [2]=>dog, [3]=>monkey, [4]=>ape
)
)
this can be used with $array['animals'][0]
But still you need numbers or unique identifiers to access the values of the array.
Something like this:
$string = 'zebra,cow,dog,monkey,ape';
$array = explode(',', $string);
$arrayReturn['animals'] = $array;
print_r($arrayReturn);
u cant have same key for all the values but u can do this
lets say your string is
$a = 'dog,ant,rabbit,lion';
$ar = explode(',',$a);
$yourArray = array();
foreach($ar as $animals){
$yourArray['animals']=$animals;
}
Now it doesnot matter how long your string is you will have you array as
$yourArray['animals'][0]='dog'
$yourArray['animals'][1]='ant'
....... so on ......
I have two arrays, like this:
$array1 = array(
'ADAIR',
'ADAM',
'ADAMINA',
'ADDISON',
'ADDY',
'ADELLE',
'ADEN',
'ADOLPH',
'ADRIANNA'
);
$array2 = array(
'ADAIR',
'ADAMINA',
'ADRIANNA'
);
How do I make a third array, without duplicates? We should take first array and remove from it duplicates from second array.
Use Array-diff
$array3=array_diff($array1,$array2);
Returns an array containing all the entries from array1 that are not present in any of the other arrays.
Take a look here: http://php.net/manual/en/function.array-unique.php
Combine both arrays into 1, then run them through the array-unique function
$result = array_unique($combined);
#grunk deleted a perferctly valid answer, so credits not to me:
$unique = array_unique(array_merge($array1,$array2));
codepad.org/NVkuml5g