Looking up keys for a multi-dimensional array - php

I have an array with a key and 3 values (day, start_time, end_time). I want to keep adding certain entries into this array while making sure each entry is unique. That means that every time I try to add an item into the array, I want to make sure it does not already exist in it. If it does exist, I want to be able to find the key that indicates that entry.
For example, this is the pre-existing array:
$array [0][0] = Monday
$array [0][1] = 2
$array [0][2] = 4
$array [1][0] = Tuesday
$array [1][1] = 3
$array [1][2] = 5
If I try to insert (Wednesday, 3, 5), then it should make the entry in the index 2.
If I try to insert (Monday, 2, 4), I need to be able to know that it is already in there and is indexed by 0.
How do I go about doing this?

I agree with the other answers here — it might be better to restructure your array so that there is no need to worry about duplication at all.
If you want to keep your current structure, however: use array_search.
$array = ...
$unique_check = array_search(array('Monday', 2, 4), $array);
if ( $unique_check === false )
// add to array
else
// $unique_check = the array key at which the existing matching element is located

Why not organize the array this way?
$array [Monday][0] = 2
$array [Monday][1] = 4
$array [Tuesday][0] = 3
$array [Tuesday][1] = 5

Related

Compare the difference of two array index by index

I want to know how can I compare two arrays if there's a differences between their values in each indexes. I have this two arrays for example.
$arr1 = ["0"=>"A", "1"=>"B", "2"=>"C", "3"=>"A"]..
$arr2 = ["0"=>"A", "1"=>"C", "2"=>"C", "3"=>"A"]..
The result that I want to get would be 1 because only index 1 is not equal with the index 1 of the second array.
I tried using array_diff but the result is always 0. I want to compare each array by indexes and values and return the number of differences on each.
Thank you
$arr1 = ["0"=>"A", "1"=>"B", "2"=>"C", "3"=>"A"];
$arr2 = ["0"=>"A", "1"=>"C", "2"=>"C", "3"=>"A"];
print_r(array_diff_assoc($arr1, $arr2)); // output: [1 => "B"]
Is this what you want? If you only need an index, you can do this
print_r(array_keys(array_diff_assoc($arr1, $arr2))); // output: [1]

PHP result from two arrays pairing dates where key is "Y"

I have two array
$array1 = array("2017-12-08","2017-12-09","2017-12-10","2017-12-11","2017-12-12","2017-12-13","2017-12-14","2017-12-15","2017-12-16","2017-12-17","2017-12-18","2017-12-19","2017-12-20","2017-12-21","2017-12-22");
$array2 = array("Y","Y","Y","Y","Y","N","N","N","Y","Y","Y","N","N","Y","Y");
I need the first and last date where the key="Y". Like this:
$result = array("2017-12-08 - 2017-12-12", "2017-12-16 - 2017-12-18", "2017-12-21 - 2017-12-22");
How i can make this?
I have tried with array_combine but after that I do not know how to list the dates pairing.
Moved from answer
Yes this is a good idea, Justinas, but the result is not exactly what i need. Your result give me the first 2017-12-08 and the last 2017-12-22. But i need from all pairs the first and end date, like:
"2017-12-08 - 2017-12-12"
"2017-12-16 - 2017-12-18"
"2017-12-21 - 2017-12-22"
Because i need to insert in the db like
INSERT INTO $table start='firstDate' AND end='lastDate'.
And in this example 3 lines.
First find only array keys that contains value as Y:
$yes = array_filter($array2, function ($yn) {return $yn == 'Y'});
Now do $result = array_intersect_key($array1, $yes) to get dates that has Y in array2.
Finally get first (current($result)) and last end($result) dates.
Example

Remove element and reindex

the logic is to get last element from the elemnt after particular interval when all the elemnts are been removed. suppose there are five users and every secound user is been eliminated , then i have to find the last remaining user.
$foo = array(
'0'=>'1',
'1'=>'2',
'2'=>'3',
'3'=>'4',
'4'=>'5',
'5'=>'6'
);
now remove element indexed at 2 and reindex the array in below format.
$foo = array(
'0'=>'4',
'1'=>'5',
'2'=>'6',
'3'=>'1',
'4'=>'2',
);
You can use unset(), but you'll also need to call array_values() to force a re-index. For example:
unset($foo[2]);
$foo = array_values($foo);
The original question is a bit unclear. I understand you want to remove index X, and place all items after index X as first items in the array.
$index2remove = 2;
$newArray1 = array_slice($foo, $index2remove+1); // Get items after the selected index
$newArray2 = array_slice($foo, 0, $index2remove); // get everything before the selected index
$newArray = array_merge($newArray1, $newArray2); // and combine them
Or shorter and a bit less memory consuming (but harder to read):
$index2remove = 2;
$newArray = array_merge(
array_slice($foo, $index2remove+1), // add last items first
array_slice($foo, 0, $index2remove) // add first items last
);
You do NOT need to unset value 2 in my code, you simple slice it out. We do that with the -1 in the 2nd splice function.
If you want, you can replace $newArray = array_merge() with $foo = array_merge(), but ONLY in the second, if you dont need to save the original array.
Edit: Changed small error, thank you plain jane
Try this of which the output is given below
$foo = array('0'=>'1','1'=>'2','2'=>'3','3'=>'4','4'=>'5','5'=>'6');
//need to input this as the index of the element to be removed
$remove_index = "2";
unset($foo[$remove_index]);
$slice1 = array_slice($foo, 0, $remove_index);
$slice2 = array_slice($foo, $remove_index);
$final_output = array_merge($slice2, $slice1);
Output
Array
(
[0] => 4
[1] => 5
[2] => 6
[3] => 1
[4] => 2
)

PHP calculating the intersect of arrays

Array declaration: $uids = array();
Next these arrays may or may not be created:
$uids['locations'];
$uids['ages'];
$uids['genders'];
If at least 2 of them are created I want to calculate the intersect. If all 3 are created I want the intersect of all 3.
So, I may want to calculate the intersect of $uids['locations'] and $uids['ages'] or the intersect of $uids['ages'] and $uids['genders'], etc.
If put all 3 arrays in array_intersect then I get errors if one of them is not an array. I'm not sure how to handle this without an awful lot of if:else statements and think there is a better way.
If you know that you don't have more array keys than the ones specified, you can use this:
$intersect = array();
if (count($uids) > 1) {
$intersect = call_user_func_array('array_intersect', $uids);
}
Otherwise you could try this one:
$_uids = array_intersect_key($uids, array(
'locations' => 1,
'ages' => 1,
'genders' => 1,
));
if (count($uids) > 1) {
$intersect = call_user_func_array('array_intersect', $_uids);
}

How to sort an array in php

i want the same value has the same index
for example
1 2 2 3 5
after sort:
array(
0=>1
1=>2
1=>2
3=>3
4=>5);
but we can not set duplicate index in the array of php.
There's a sort function in php! ( I answer the topic and not the body, didn't quite follow you there, but here's how you sort in php )
Example
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}
?>
Duplicates
In the above example duplicates will just have their own indexes so the array:
5 4 5 1 3 1 2
Will look like this
1 1 2 3 4 5 5
This might not be what you are looking for, what you want is another type of dataset than just a simple array, maybe you want a hashtable or just a linked list on each row.
If you are okay with it, you can remove the duplicates by using array_unique
$newArray=array_unique($arr);
Which would lead to having an array looking like this
1 2 3 4 5
You're right, you can't have duplicate values at the same index in an array - each index in an array has exactly one value.
As to the title of the question, to sort an array in PHP, use sort.
If this doesn't answer what you're trying to ask, you might want to edit your question to make it a bit clearer (the body of the question doesn't seem particularly related to the question title).
Post OP's edit:
You cannot store multiple values at the same key, your output array (array(0=>1, 1=>2, 1=>2, 3=>3, 4=>5);) doesn't really make sense (the key 1 does map to the value 2) in the sorted array. Are you trying to store counts of occurrences of numbers?
e.g. given the input:
1, 2, 2, 3, 5
get the output:
array(1=>1, 2=>2, 3=>1, 5=>1); // there is 1 "1", there are 2 "2"s etc.
Try to use this.
sort($array);
You are looking for array_unique:
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
Takes an input array and returns a new array without duplicate values.

Categories