I am using codeigniter.
I want difference of two array as I am using array_diff function of php.
Due to associative array, I have used call_user_func_array and I got record.
$result_sun = call_user_func_array('array_merge', $data['sun_holiday']);
$result_sat = call_user_func_array('array_merge', $data['third_sat']);
But when I am going to make difference of these two array like,
$result = array_diff($result_sun,$result_sat);
It only shows the record of first array $result_sun.
$result_sun = Array
(
[0] => 2015-09-06
[1] => 2015-09-13
[2] => 2015-09-20
[3] => 2015-09-27
)
$result_sat = Array
(
[0] => 2015-09-19
)
So, why the difference is not occurring??
$result1 = array_diff($result_sun,$result_sat);
$result2 = array_diff($result_sat,$result_sun);
$result=array_merge($result1,$result2);
Compares $result_sun against one or more other arrays and returns the values in $result_sun that are not present in any of the other arrays.
so take difference of both and then merge it will be good if you put your code then we can give more accurate answer
Related
I want to get the array_diff of these two array and echo by key
$s1=1;
$s2=7;
$s3=8;
$r=array("$s1","$s2","$s3");
$rr=array("1","2","3","4","5","6","7","8","9","10");
$rrr=array_diff($r,$rr);
echo $rrr[0];
I was hoping to get a result like 2 but i got an error. Someone help out here.
The documentation for array_diff says that it
Compares array1 against one or more other arrays and returns the
values in array1 that are not present in any of the other arrays.
Your array1 is $r, and your other array is $rr.
$r is essentially equivalent to
$r = array("1","7","8");
We can clearly see that there are no values in $r that are not present in $rr. ("1", "7", "8" are all in $rr.)
Thus, you will receive a E_NOTICE when you try to access $rrr[0], because $rrr is empty.
Perhaps you meant to reverse the order of the two arguments.
$rrr = array_diff($rr, $r);
As Loop Me pointed out, array_diff does not reorder your indices.
What this means is that your array is now like this.
Array
(
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[8] => 9
[9] => 10
)
You can reorder them with array_values.
$rrr = array_values($rrr);
Demo
I am trying to compare two different arrays and get the values that do not exist in 1 of the arrays. Here are my 2 arrays:
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52)
Array ( [0] => 2fbd5868-28ec-418d-854a-0736db720c8a [1] => f4a41974-5373-4862-a5e7-9d28b8c2301f [2] => a1874f68-3da1-47c3-97ef-a68580ce2a52 [3] => 583cee91-1913-4e9d-b51d-e27083420001)
As you can see the second array has an additional value. I am trying to user array_diff like this:
$result = array_diff($array1,$array2);
print_r($result);
However the out of the array_diff is:
array()
Any ideas what is going on?
As people have suggested and i have already tested switching the arrays around, this is the output:
Array ( [0] => [1] => )
array_diff gives you the values from $array1 that are not in the other arrays. All the values of your first array are in the second. Sou change the order of your arrays and you should be fine.
See also here: http://php.net/manual/de/function.array-diff.php
The order of arguments in array_diff() is important
Returns an array containing all the entries from array1 that are not
present in any of the other arrays2
Read array_diff
$result = array_diff($array2,$array1);
Try like this
If I have an array which looks something like this:
Array
(
[0] => Array
(
[DATA] => Array
(
VALUE1 = 1
VALUE2 = 2
)
)
[1] => Array
(
[DATA] => Array
(
VALUE3 = 3
VALUE4 = 4
)
)
)
And would like to turn it into this:
Array
(
[0] => Array
(
[DATA] => Array
(
VALUE1 = 1
VALUE2 = 2
VALUE3 = 3
VALUE4 = 4
)
)
)
I basically want to merge all the identical keys which are at the same level.
What would be the best route to accomplish this?
Could the array_merge functions be of any use?
I Hope this makes any sort of sense and thanks in advance for any help i can get.
You can use array_merge_recursive to merge all the items in your original array together. And since that function takes a variable number of arguments, making it unwieldy when this number is unknown at compile time, you can use call_user_func_array for extra convenience:
$result = call_user_func_array('array_merge_recursive', $array);
The result will have the "top level" of your input pruned off (logical, since you are merging multiple items into one) but will keep all of the remaining structure.
See it in action.
How can I receive indexed array after mysql query? Or is there any method to convert $this->db->get() into mysql resource? Or convert associative array to indexed?
PHP has a function array_values() that will return an array of just the values.
http://it.php.net/manual/en/function.array-values.php
Example on converting codeigniter result_array to indexed Array:
$query = $this->db->query("SELECT `tag_id` FROM `tags`");
$arr = $query>result_array();
print_r($arr); //codeigniter default result array
//Output:
Array
(
[0] => Array
(
[tag_id] => 1
)
[1] => Array
(
[tag_id] => 3
)
)
Now If You want to convert above array to indexed Array then you have to use array_column() function which convert it associative array to indexed array by taking array key as argument see Below for example:
$query = $this->db->query("SELECT `tag_id` FROM `tags`");
$tags = $query>result_array();
$arr = array_column($tags, "tag_id");
print_r($arr); //converted indexed array
//Output:
Array
(
[0] => 1
[1] => 3
)
It looks like you might be using PHP CodeIgniter. CodeIgniter's DB implementation doesn't support indexed result arrays, you have to choose between object or associative array.
This is done to make your queries more maintainable, as returning numeric indexes are harder to debug and maintain.
Code Igniter User Guide - database results
I've Googled it for two days, and tried looking at the PHP manual, and I still can't remember that function that aligns the key values for PHP arrays.
All I'm looking for is the function that takes this:
Array
(
[0] => 1
[3] => 2
[4] => 3
[7] => 4
[9] => 5
)
And converts it into this:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Basically, the array is first sorted by key (their values attached to them stay with them), then all the keys are set to all the counting numbers, consecutively, without skipping any number (0,1,2,3,4,5,6,7,8,9...). I saw it being used with ksort() a few months ago, and can't see to remember or find this elusive function.
Well, you see, this one is hard, because the general description on the PHP array functions page does not say that this function does what you're looking for.
But you can sort the array using ksort(), and then use this: array_values() . From the page from the PHP manual:
array_values() returns all the values from the input array and indexes numerically the array.
You can use array_merge:
$array = array_merge($array);
It will reindex values with numeric keys.
Update: Using array_values as proposed in #LostInTheCode's answer is probably more descriptive.
function array_reset_index_keys($array)
{
$return = array();foreach($array as $k => $v){$return[] = $v;}return $return;
}
And then use like a regular function, should re index the array
you can also use native functions such as array_values which returns the values of an array into a single dimension array, causing it to be re indexed .