Check if value exists in php multi-dimensional array before adding - php

I have a separate set of functions that grabs a "Date" and a "Time" from my application and puts the date into as a key, and the time as a multi-dimensional value.
For example purposes:
$alldatetimes = array(
'date1' => array('13:00','14:30','14:30','14:30','15:00'),
'date2' => array('09:00','10:00','10:30','10:30','12:00')
);
foreach ($alldatetimes as $date => $times) {
echo '<h1>This Exports:</h1>';
echo '<h2>'.$date.'</h2><br>';
foreach ($times as $time) {
echo $time.'<br>';
}
}
This exports:
date1
13:00
14:30
14:30
14:30
15:00
date2
09:00
10:00
10:30
10:30
12:00
I'm trying to control if the time is put into the array so only one value each is in the array (I don't want 3 instances of 14:30 for that date).
Based on other posts here I tried to build something like this to identify if the value was there, but I can't figure out how to tie it all together:
function searchForId($id, $array) {
foreach ($array as $date => $times) {
foreach ($times as $time) {
if ($time === $id) {
return $time;
}
}
}
return null;
}
Any ideas?
Update: Here is how the array is initially being created - this probably can be more efficient:
while ($schedule_q -> have_posts() ) : $schedule_q->the_post();
$alldatetimes [get_the_date()][] = get_the_time();
endwhile;

You can add a array_unique() call over each sub-array before you loop over your results to ensure it's all unique:
foreach ($alldatetimes as &$row) {
$row = array_unique($row);
}
Output:
<h1>This Exports:</h1>
<h2>date1</h2><br>
13:00<br>
14:30<br>
15:00<br>
<h1>This Exports:</h1>
<h2>date2</h2><br>
09:00<br>
10:00<br>
10:30<br>
12:00<br>

It is not shown in your question, but how about modifying your function that builds the date/time array to use the times as keys instead of values? Using something like
$alldatetimes[$date][$time]++
in that function would give you an array with one value for each time that would be the number of occurrences of that date/time combination, like this:
$alldatetimes = array(
'date1' => array('13:00' => 1,'14:30' => 3,'15:00' => 1),
'date2' => array('09:00' => 1,'10:00' => 1,'10:30' => 2,'12:00' => 1)
);
Then you could change your code that prints them out to use the key.
foreach ($times as $time => $count) {
echo $time.'<br>';
}

You can write a recursive function
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}

Related

PHP array filter by pre text from value

I have an array like below
Array
(
[0] => country-indonesia
[1] => country-myanmar
[2] => access-is_airport
[3] => heritage-is_seagypsy
)
From that array I want to make separate array only for [country] ,[access], [heritage]
So for that I have to check array value by text before '-'. I am not sure how to do it. so i can't apply code here. I just have the array in PHP
A modified answer, if you want to get the specific types only.
<?php
$arr = [
'country-indonesia',
'country-myanmar',
'access-is_airport',
'heritage-is_seagypsy',
];
$new_array = [];
$types = ['country', 'heritage', 'access'];
foreach ($arr as $element) {
$fac = explode('-', $element);
foreach ($types as $type) {
if ($fac[0] === $type) {
$new_array[$type][] = $fac[1];
}
}
}
$country = $new_array['country'];
$access = $new_array['access'];
$heritage = $new_array['heritage'];
var_dump($new_array);
A simple and easy solution in 3 lines of code using array_walk
<?php
$arr = [
'country-indonesia',
'country-myanmar',
'access-is_airport',
'heritage-is_seagypsy',
];
$new_array = [];
array_walk($arr, function($item) use (&$new_array){
//if(false === strpos($item, '-')) return;
list($key,$value) = explode('-', $item, 2);
$new_array[$key][] = $value;
});
print_r($new_array);
Gives this output:
Array
(
[country] => Array
(
[0] => indonesia
[1] => myanmar
)
[access] => Array
(
[0] => is_airport
)
[heritage] => Array
(
[0] => is_seagypsy
)
)
If you don't want empty and duplicate entries:
<?php
$arr = [
'country-indonesia',
'country-myanmar',
'access-is_airport',
'heritage-is_seagypsy',
];
$new_array = [];
array_walk($arr, function($item) use (&$new_array){
if(false === strpos($item, '-')) return;
list($key,$value) = explode('-', $item, 2);
if(empty($value) || array_key_exists($key, $new_array) && in_array($value, $new_array[$key])) return;
$new_array[$key][] = $value;
});
print_r($new_array);
you can do it by using explode and in_array functions
<?php
$arr = ["country-indonesia","country-myanmar","access-is_airport","heritage-is_seagypsy"];
$newArr = array();
foreach($arr as $k=> $val){
$valArr = explode("-", $val);
if(!in_array($valArr[0], $newArr)){
$newArr[] = $valArr[0];
}
}
print_r($newArr);
?>
live demo
You need PHP's strpos() function.
Just loop through every element of the array and try something like:
if( strpos($array[$i], "heritage") != false )
{
// Found heritage, do something with it
}
(Rough example written from my cellphone while feeding baby, may have typos but it's the basics of what you need)
Read further here: http://php.net/manual/en/function.strpos.php
//first lets set a variable equal to our array for ease in working with i.e
// also create a new empty array to hold our filtered values
$countryArray = array();
$accessArray = array();
$heritageArray = array();
$oldArray = Array(country-indonesia, country-myanmar, access-is_airport, heritage-is_seagypsy);
//Next loop through our array i.e
for($x = 0; $x < count($oldArray); $x++){
// now filter through the array contents
$currentValue = $oldArray[$x];
// check whether the current index has any of the strings in it [country] ,[access], [heritage] using the method : strpos()
if(strpos($currentValue,'country')){
//if this particular value contains the keyword push it into our new country array //using the array_push() function.
array_push($countryArray,$currentValue);
}elseif(strpos($currentValue,'access')){
// else check for the access string in our current value
// once it's found the current value will be pushed to the $accessArray
array_push($accessArray,$currentValue);
}elseif(strpos($currentValue,'heritage')){
// check for the last string value i.e access. If found this too should be pushed to //the new heritage array i.e
array_push($heritageArray,$currentValue);
}else{
// do nothing
}
}
//I believe that should work: cheers hope

strtotime in php not giving exact time

I want to compare time from db with current time. strtotime for storing time in db is working fine. but when i use strtotime to get current time it is not giving me exact time. i checked that from an online website. here is my code:
This part store time in db:
public function Add($data)
{
foreach ($data['check'] as $key => $value) {
foreach ($value['in'] as $k => $v) {
if(!empty($value['in'][$k])) {
$allocate = array(
'check_in' => strtotime(date('h:i A', strtotime($value['in'][$k]))),
'check_out' =>strtotime(date('h:i A', strtotime($value['out'][$k]))),
//'check_in' => strtotime($value['in'][$k]),
//'check_out' =>strtotime($value['out'][$k]),
'Days_id' => $key,
'User_id' => $data['Users']
);
$this->db->insert('assgin_days',$allocate);
}
}
}
}
This part compare time from db with current time:
public function checkuserlogin()
{
$string = exec('getmac');
$mac = substr($string, 0, 17);
$day_of_week = date('N');
date_default_timezone_set("Asia/Karachi");
$time = strtotime(date('h:i A'));
$timee = date('h:i A');
$id=$this->user_model->userInfo("id");
$data=$this->db->query("SELECT admin.first_name, days.d_name FROM assgin_days INNER JOIN admin ON admin.id= assgin_days.User_id inner join days ON days.D_id= assgin_days.Days_id where assgin_days.User_id = $id and assgin_days.Days_id =$day_of_week and assgin_days.check_in =<$time and assgin_days.check_out >=$time and assgin_days.is_delete=0")->result_array();
}
need to use date('G:i', strtotime($value['in'][$k])), instead of date('H:i', strtotime($value['in'][$k])), it will change the date into 24 hrs formats then it can easily store and comparable.
public function Add($data){
foreach ($data['check'] as $key => $value) {
foreach ($value['in'] as $k => $v) {
if(!empty($value['in'][$k]))
{
$allocate = array(
'check_in' => date('G:i', strtotime($value['in'][$k])),
'check_out' =>date('G:i', strtotime($value['out'][$k])),
'Days_id' => $key,
'User_id' => $data['Users']);
$this->db->insert('assgin_days',$allocate);
}
}
}
}

Using PHP to loop JSON and generate a new format of nested JSON

In PHP, how do I loop through the following JSON Object's date key, if the date value are the same then merge the time:
[
{
date: "27-06-2017",
time: "1430"
},
{
date: "27-06-2017",
time: "1500"
},
{
date: "28-06-2017",
time: "0930"
},
{
date: "28-06-2017",
time: "0915"
}
]
Result should looks like this:
[
{
date: "27-06-2017",
time: [{"1430","1500"}]
}, {
date: "28-06-2017",
time: [{"0930, 0915"}]
}
]
Should I create an empty array, then the looping through the JSON and recreate a new JSON? Is there any better way or any solution to reference?
Thank you!
This solution is a little overhead, but if you are sure that your data is sequential and sorted, you can use array_count_values and array_map:
$count = array_count_values(array_column($array, 'date'));
$times = array_column($array, 'time');
$grouped = array_map(function ($date, $count) use (&$times) {
return [
'date' => $date,
'time' => array_splice($times, 0, $count)
];
}, array_keys($count), $count);
Here is working demo.
Another Idea to do this is
<?php
$string = '[{"date": "27-06-2017","time": "1430"},{"date": "27-06-2017","time": "1500"},{"date": "28-06-2017","time": "0930"},{"date": "28-06-2017","time": "0915"}]';
$arrs = json_decode($string);
$main = array();
$temp = array();
foreach($arrs as $arr){
if(in_array($arr->date,$temp)){
$main[$arr->date]['time'][] = $arr->time;
}else{
$main[$arr->date]['date'] = $arr->date;
$main[$arr->date]['time'][] = $arr->time;
$temp[] = $arr->date;
}
}
echo json_encode($main);
?>
live demo : https://eval.in/787695
Please try this:
$a = [] // Your input array
$op= [];//output array
foreach($a as $key => $val){
$key = $val['date'];
$op[$key][] = $val['time'];
}
$op2 = [];
foreach($op as $key => $val){
$op2[] = ['date' => $key, 'time' => $val];
}
// create the "final" array
$final = [];
// loop the JSON (assigned to $l)
foreach($l as $o) {
// assign $final[{date}] = to be a new object if it doesn't already exist
if(empty($final[$o->date])) {
$final[$o->date] = (object) ['date' => $o->date, 'time' => [$o->time]];
}
// ... otherwise, if it does exist, just append this time to the array
else {
$final[$o->date]->time[] = $o->time;
}
}
// to get you back to a zero-indexed array
$final = array_values($final);
The "final" array is created with date based indexes initially so that you can determine whether they've been set or not to allow you to manipulate the correct "time" arrays.
They're just removed at the end by dropping $final into array_values() to get the zero-indexed array you're after.
json_encode($final) will give you want you want as a JSON:
[{"date":"27-06-2017","time":["1430","1500"]},{"date":"28-06-2017","time":["0930","0915"]}]
Yet another solution :
$d = [];
$dizi = array_map(function ($value) use (&$d) {
if (array_key_exists($value->date, $d)) {
array_push($d[$value->date]['time'], $value->time);
} else {
$d[$value->date] = [
'date' => $value->date,
'time' => [$value->time]
];
}
}, $array);
echo json_encode(array_values($d));

Find ARRAY by to its values

I have a multi-dimensional array like so:
$a = array(
'potatoe'=> array(
'weight'=>24,
'label'=>'kon',
'year'=>2001,
),
'apple'=> array(
'weight'=>55,
'label'=>'krakat',
'year'=>1992,
)
);
I am looking for a way to searching the fruit name (with its values) when I only know weight is 55 and year is 1992. How to do that?
Something like this perhaps
foreach ($a as $key => $value) {
if ($value['weight'] == 55 && $value['year'] == 1992) {
echo $key;
}
}
Outputs
apple
You will have to iterate over it using foreach and test every element.
function findFruit($array,$weight, $year){
foreach($array as $key => $fruit){
if($fruit['weight'] == $weight && $fruit['year'] == $year ){
return $key;
}
}
}
then, just use the function:
$theFruit = $a[findFruit($a,55,1992)];

Increment value in array php

I am trying to make a function that grabs all the days that have events that are in a database for a certain user. For instance if there were two events on Jan 23, 2013 it would add Jan 23, 2013 to the array. I got it to work so it adds all the days (without adding the same day twice) but now I want to be able to say how many dates are on each day. So on Jan 23, 2013 it would say they have two events in that day.
I hope this makes sense... I have some code for further aid.
PHP Function (grabbing each day that has events)
//gets upcoming days that have events for a user
public function get_upcoming_days_with_events() {
$return_array = array();
$date_in=null;
$user_id = $this->session->userdata('id');
$query =$this->db->select()->from('events')->where('user_id', $user_id)->get();
foreach ($query->result_array() as $event => $row) {
$date = strtotime($row['date_due']);
if (sizeof($return_array) != 0) {
foreach ($return_array as $date_in_array => $row) {
$d = $row['full_date'];
if (date('Y-m-d', $date) == $d) {
//date is already in array
//increment the number of assignments on this day
$row['number_of_assignments'] += 1;
$date_in = true;
} else{
$date_in = false;
}
}
}
if ($date_in == false) {
$return_array[] = array(
'day' => date('d', $date),
'month' => date('m', $date),
'full_date' => date('Y-m-d', $date),
'number_of_assignments' => 1
);
}
}
return $return_array;
}
So with this I want to be able to increment $return_array['number_of_assignments'] if my function notices that a certain day has more than one event.
Let me know if you need any more info...
Thanks! :)
We can save the info in return_array by index of date, if the date info have not been set into return_array, we make an empty info. Each time, we simply increase number_of_assignments.
public function get_upcoming_days_with_events()
{
$return_array = array();
$user_id = $this->session->userdata('id');
$query =$this->db->select()->from('events')->where('user_id', $user_id)->get();
foreach ($query->result_array() as $event => $row)
{
$date = strtotime($row['date_due']);
$date_key = date('Y-m-d', $date);
if (!isset($return_array[$date_key]))
{
$new_item = array(
'day' => date('d', $date),
'month' => date('m', $date),
'full_date' => $date_key,
'number_of_assignments' => 0,
);
$return_array[$date_key] = $new_item;
}
$return_array[$date_key]['number_of_assignments']++;
}
$return_array = array_values($return_array);
return $return_array;
}
Function: array_count_values() can help you with this, it gives total number of occurance of a value in an array.
For example:
$a = array("apple", "banana", "apple");
var_dump(array_count_values($a));
Will output
array(
[apple] => 2,
[banana] => 1
);
So instead of trying to filter out duplicate events, add them all on the array and then use array_count_values() at last to know their occurenses.

Categories