I have 2 arrays.
The first one is $teach_array and the second one is $langs_array.
Their respective values are:
$teach_array : Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
$langs_array : Array ( [0] => 2 [1] => 3 )
Im trying to return a new array containing all the entries from $teach_array that are not present in $langs_array.
So the end result should be: Array ( [0] => 1 [3] => 4 [4] => 5 )
I have tried using a couple of methods including :
Option 1
$result = array_diff($teachArray, $language_1d_array);
This still returns all the values of $teach_array.
Option 2
$result = array_diff_key($teachArray, $language_1d_array);
However, this only returns Array ( [2] => 3 [3] => 4 [4] => 5 ) which is not correct.
Option 3
$result = array_values(array_diff_key($teachArray, $language_1d_array));
This returns the same result as Option 2. I also tried using only array_diff instead of array_diff_key and it returns the same result as Option 1.
I did a var_dump on both of my arrays and here is the result.
$teach_array : array(5) { [0]=> string(5) " 1 " [1]=> string(5) " 2 " [2]=> string(5) " 3 " [3]=> string(5) " 4 " [4]=> string(5) " 5 " }
$lang_array : array(2) { [0]=> string(1) "2" [1]=> string(1) "3" }
hope you have already found the solution, but just in case I want to point you on following.
Blockquote
I did a var_dump on both of my arrays and here is the result.
$teach_array : array(5) { [0]=> string(5) " 1 " [1]=> string(5) " 2 " [2]=> string(5) " 3 " [3]=> string(5) " 4 " [4]=> string(5) " 5 " }
$lang_array : array(2) { [0]=> string(1) "2" [1]=> string(1) "3" }
No single value from $teach_array matches any value of $lang_array.
Because there are differently formatted values, one array contains whitespaces before and after the value you want to match " 2 ".
var_dump($teach_array) => array(5) { [0]=> string(5) " 4 " ... }
var_dump($lang_array) => array(5) { [0]=> string(1) "2" ... }
I guess you have some whitespaces included. Please try again with:
$diff = array_diff(array_map('trim', $teach_array), $lang_array);
PHPTester just tested yours, works fine for me..?
$teachArray =[1,2,3,4,5];
$langsarray =[2,3];
$result = array_diff($teachArray,$langsarray);
print_r($result);
works and prints 1, 4, 5 for me.
BUT...here's a solution for what you're trying to acquire: the values in teacher array that are not in langs
$new_array = array();
foreach($teach_array as $item){ // Loop the teacher_array
if(!in_array($item,$langs_array)){ // If the teach_array value doesn't exist in the lang_array, add the value
$new_array[] = $item;
}
}
I'm sure theres a more elegant way, but this works:
$teach = [1, 2, 3,4, 5];
$langs = [2, 3];
$result = [];
foreach ($teach as $key => $t) {
if (!in_array($t, $langs)) {
$result[$key] = $t;
}
}
var_dump($result);
This is (basically) what you say you have. It works for me:
<?php
$fred = array(0=>1, 1=>2, 2=>3, 3=>4, 4=>5);
$bert = array(0=>2, 1=>3);
$res = array_diff($fred, $bert);
print_r($res);
Related
Why are these two strings not equal?
I tried to get the same name so I can create a file, however I cannot get two strings equal to each other, even though I think both strings have the same value.
I uploaded var_dump output
any idea how to fix it?
$selectCategory = scandir($_SERVER['DOCUMENT_ROOT'].'/database/');
$cat = explode('.',$category);
print_r($cat);
print_r($selectCategory);
if($cat[0] == $selectCategory[2]){
echo " true";
}
else{
echo "no";
}
output:
Array ( [0] => bus [1] => php )
Array ( [0] => . [1] => .. [2] => bus [3] => fruit )
no
This is var_dump output
array(2) { [0]=> string(5) " bus" [1]=> string(3) "php" }
array(4) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(3) "bus" [3]=> string(5) "fruit" }
no
As you can see from the var_dump output the items you are comparing are different lengths. There is a space and possibly a hidden character in the $cat one:
To trim all space and some other characters use this:
$cat = array_map('trim', $cat);
$selectCategory = array_map('trim', $selectCategory);
I wish to make Key and Value combining with 2 arrays, but both arrays are not equal.
$array1 = array("1","2","3","4","5");
$array2 = array("apple","banana","","dog","");
$key_value = array_combine($array1,$array2);
The output is:
array_combine(): Both parameters should have an equal number of elements
But I need to below output be like
print_r($key_value);
array(5) {
[1]=> string(5) "apple"
[2]=> string(6) "banana"
[3]=> string(8) "No Value"
[4]=> string(3) "dog"
[5]=> string(8) "No Value"
}
How can do this if null, insert "no value" text.
You can do it via foreach loop:
$res = [];
foreach($array1 as $ind=>$num){
$res[$num] = $array2[$ind] === "" ? "No Value" : $array2[$ind];
}
print_r($res);
Output:
Array
(
[1] => apple
[2] => banana
[3] => No Value
[4] => dog
[5] => No Value
)
Demo
use array_map() and array_combine()
<?php
$array1 = array("1","2","3","4","5");
$array2 = array("apple","banana","","dog","");
$array2 = array_map(function($v){
return (empty($v)) ? "No Value" : $v;
},$array2);
$key_value = array_combine($array1,$array2);
print_r($key_value);
https://3v4l.org/CY4ku
I want to search an item like "January" or "February" in an array that looks like this
Array
(
[0] => January
[1] => February
[2] => March
[3] => April
)
This what I have tried so far. But not working.
if ( in_array("January", $date_array) ) {
echo "Found item in Array";
} else {
echo "Didn't find item in Array";
}
result:
Didn't find item in Array
This is the result of var_dump()
array(4) {
[0]=>
string(9) "January
"
[1]=>
string(10) "February
"
[2]=>
string(7) "March
"
[3]=>
string(7) "April
"
}
Don't know, where line breaks are coming from, but you can remove them, for example, with array_map:
$date_array = array_map('trim', $date_array);
// and then use `in_array`
This question already has answers here:
Combine two arrays
(11 answers)
Closed 5 months ago.
how can we join this two arrays into one array
for this, I had done my code like this and got the output as shown below
$groups_array = array_map('trim',explode(',', $tt));
$tt looks like this string(5) "11:00" string(5) "10:00"
array(1) { [0]=> string(5) "11:00" } array(1) { [0]=> string(5) "10:00" }
need desired output to look like
array(1) { [0]=> string(5) "11:00",[1]=> string(5) "10:00" }
My code is here please have a look
<?php $time_booked=$this->Hospital_model->get_already_booked_time($d,$timeslot->doctor_id);
foreach($time_booked as $index1=> $t) {
$tt=$t->time;
$groups_array = array_merge(array_map('trim',explode(',', $ttt)));
} ?>
my var_dump($time_booked) looks like this
array(2) { [0]=> object(stdClass)#40 (1) { ["time"]=> string(5) "11:00" } [1]=> object(stdClass)#41 (1) { ["time"]=> string(5) "10:00" } }
What about array_merge() with array_map()
$groups_array = array_merge(array_map('trim',explode(',', $tt)));
Output:-https://eval.in/1012484
By looking your edit in your question no need to do any extra stuff, just create an array and add values to it
<?php
$groups_array = []; //create array
$time_booked=$this->Hospital_model->get_already_booked_time($d,$timeslot->doctor_id);
foreach($time_booked as $index1=> $t) {
$groups_array[] =$t->time; //add values to array
}
var_dump($groups_array);
?>
What about array_merge ? That should give you the result.
http://php.net/manual/de/function.array-merge.php
EDIT:
$tt = ['11:00'];
$tt2 = ['10:00'];
$result = array_merge($tt,$tt2);
var_dump($result);
Result is
array(2) {
[0]=>
string(5) "11:00"
[1]=>
string(5) "10:00"
}
Is that not what you meant ?
Suppose you have two array which look like
$array1 = array(0 => "10:00 am");
$array2 = array(0 => "11:00 am");
and you want to join and want output like: Array ( [0] => 10:00 am [1] => 11:00 am )
then you can use array_merge option
$array3 = array_merge($array1, $array2);
If you print print_r($array3);
output will be
Array ( [0] => 10:00 am [1] => 11:00 am )
$data1 -
Array([0] => Array([file_id_fk] => 1)
[1] => Array([file_id_fk] => 2)
[2] => Array([file_id_fk] => 5)
[3] => Array([file_id_fk] => 3)
........
[300]=>Array([file_id_fk] => 3)
$data2 -
Array([0] => Array
([file_id] => 1
[file_sender_type] => 1
[file_sender_id] => 1
[file_subject] => test 1)
......
[360]=>Array...
Here i want to compare $data1->file_id_fk with $data2->file_id and print the $data2 values that are not present in $data1.
It looks like you'll want to use array_diff(), but being that you want the extra values from $data2, you'll want to change the order around:
array_diff($data2, $data).
Read the full documentation here: http://php.net/manual/en/function.array-diff.php
Imagine yourself being the PHP Parser. How would you like to see the code you had to parse? I asked myself this question and came up with the following:
$valuesThatAreInTheFirstArrayButNotInTheSecond = array_diff($theFirstArray, $theSecondArray);
I can't make it much more.
As PHP Parser, I am well documented. There's a website on the so called internet that describes how I do and like to do things. You can find the documentation on how I want to parse array_diff() in the PHP.net Manual
Edit
Because you have an array in an array (inception?), you can use the current() function around the array. This will grab the array's current key. As there is only one key in your array, it will always pick that one.
So that will make:
$valuesThatAreInTheFirstArrayButNotInTheSecond = array_diff(current($theFirstArray), current($theSecondArray));
Showing the difference can be done like this:
if (array_key_exists('file_id', $valuesThatAreInTheFirstArrayButNotInTheSecond)) {
echo $valuesThatAreInTheFirstArrayButNotInTheSecond['file_id'];
}
This will not work on older PHP versions (I am working with 5.4 currently).
$data1ID = array_column($data1, 'file_id_fk');
$results = array_filter($data2, function($v) use ($data1ID){
return !in_array($v['file_id'], $data1ID);
});
print_r($data1);
print_r($data2);
print_r($results);
This is my input data and results:
/vhost/virtual/sandbox/public/index.php:55
array(5) {
[0] = array(1) {
[file_id_fk] = int(1) 1
}
[1] = array(1) {
[file_id_fk] = int(1) 2
}
[2] = array(1) {
[file_id_fk] = int(1) 5
}
[3] = array(1) {
[file_id_fk] = int(1) 3
}
[4] = array(1) {
[file_id_fk] = int(1) 3
}
}
/vhost/virtual/sandbox/public/index.php:56
array(3) {
[0] = array(4) {
[file_id] = int(1) 1
[file_sender_type] = int(1) 1
[file_sender_id] = int(1) 1
[file_subject] = string(6) "test 1"
}
[1] = array(4) {
[file_id] = int(1) 5
[file_sender_type] = int(1) 5
[file_sender_id] = int(1) 5
[file_subject] = string(6) "test 5"
}
[2] = array(4) {
[file_id] = int(1) 7
[file_sender_type] = int(1) 7
[file_sender_id] = int(1) 7
[file_subject] = string(6) "test 7"
}
}
/vhost/virtual/sandbox/public/index.php:57
array(1) {
[2] = array(4) {
[file_id] = int(1) 7
[file_sender_type] = int(1) 7
[file_sender_id] = int(1) 7
[file_subject] = string(6) "test 7"
}
}
finally got answer friends thank you so much
foreach($data1 as $val)
{
$data1[]=$val['file_id_fk'];
}
for($i=0 ; $i<=count($data2) ; $i++)
{
if(!in_array($data2[$i]['file_id'],$data1))
{
$data[] = $data2[$i];
}
}