adding new key value is not working - php

This is my json link which stored in variable named $onceBooking.
I want to count total booking days. If booking array is empty then add total_days = 0 but but if it is not empty then i am counting the unique booking_slots and adding them into main array. I am getting the correct count but when i am adding this into main it showing me this error.
$data = json_decode($onceBooking);
for ($i = 0; $i < count($data); $i++) {
if (count($data[$i]->bookings) == 0) {
$data[$i]->total_days = 0;
} else {
$bookings = $data[$i]->bookings;
for ($j = 0; $j < count($bookings); $j++) {
$booking_slots = $bookings[$j]->booking_slots;
$final_array = array();
$uniquekeys = array();
foreach ($booking_slots as $key => $data) {
if (!in_array($data->date, $uniquekeys)) {
$uniquekeys[] = $data->date;
$final_array[$key] = $data;
}
}
}
$data[$i]->total_days = count($final_array);
}
}
return $data;

Change your call to json_decode() to return an array by adding true as the 2nd argument.
// change this
$data = json_decode($onceBooking);
// to this
$data = json_decode($onceBooking, true);

Related

Problem with php array function displaying only the first item in the array ignoring the rest

I am trying to make a function to display client's items in an array, however only the first item id is shown in the array. What's wrong with my code? I guess I'm doing something wrong with the array.
Example of sItem value: https://pastebin.pl/view/e492ffa1
$items = array();
$client_item = bin2hex($u->sItem);
$x = 0;
for ($i = 0; $i < 78; $i++) {
$item = hexdec(reverse(substr($client_item, $x, 8)));
if ($item != 0) {
$ii = $db->get_object("SELECT * FROM Clients.dbo.ITEM WHERE Num =" . $item);
if (is_object($ii)) {
$items[] = array("ItemID" => $ii->Num, "ItemSlot" => $i);
}
}
$x += 16;
}
The code above will show just the first item in the array. All others are not shown.
var items = [{"ItemID":310511133,"ItemSlot":0}]
I am not sure what I'm doing wrong. If I remove $x += 16; then it will simply add more entries in the array with the first item id. I want more entries but not with the same item id obviously. :)
var items = [{"ItemID":310511133,"ItemSlot":0},{"ItemID":310511133,"ItemSlot":1},{"ItemID":310511133,"ItemSlot":2},{"ItemID":310511133,"ItemSlot":3},{"ItemID":310511133,"ItemSlot":4}....
This is the reverse function:
function reverse($str)
{
$len = strlen($str);
$i = $len - 2;
$ret = NULL;
while (0 <= $i) {
$ret .= substr($str, $i, 2);
$i -= 2;
}
return $ret;
}
Database Query not returning any object SELECT * FROM Clients.dbo.ITEM WHERE Num = $item
can you confirm values exists in database
you can echo $item value and see manually in database are those values exists.

Index in for loop not returning last index

I have an image gallery and each is saved by index.. gallery1: /image here.. gallery2: / image here.. etc..
I'm using an index with multiple for loops to return the images and by column because it's either Masonry or rectangular. I am getting the return fine except for the very last index.
private function rectangle($items, $columns, $contents = array()) {
$thumbs = array('talent_thumbnail','360x207');
$arr = array();
$col = round(count($items) / $columns);
$perCol = floor(count($items) / $columns);
$extra = count($items) % $columns;
$ind = 1;
$length = count($items);
$arr = array();
for($i = 0; $i < $columns && $ind < $length; $i++) {
$temp = array();
for($j = 0; $j < $perCol; $j++) {
$obj = new JObject();
$obj->image = $items['gallery' . $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
}
if ($extra > 0) {
$obj = new JObject();
$obj->image = $items['gallery'. $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
$extra--;
}
$arr[] = $temp;
}
}
I know it can't be that hard but I'm not that good at it right yet.
Any help is so much welcome.
Thank You.
You set the variable $ind to 1, count the length of the array, and then initialize the for loop with the condition $ind < $length.
When $ind reaches the index needed to access the last item, the loop is not run again, since $ind is now equal to $length, not smaller.
You can fix this by changing the condition in your for-loop to "less or equal":
$i < $columns && $ind <= $length
This runs the loop once more when $ind has reached the last index.
Probably the problem is at this line:
$ind = 1;
In PHP non-associative arrays have the indexes starting to 0 instead of 1:
$arr = array('a', 'b', 'c');
print_r($arr);
// output:
Array ( [0] => a [1] => b [2] => c )
So, try to change that line in your code to:
$ind = 0;
Complete code:
private function rectangle($items, $columns, $contents = array()) {
$thumbs = array('talent_thumbnail','360x207');
$arr = array();
$col = round(count($items) / $columns);
$perCol = floor(count($items) / $columns);
$extra = count($items) % $columns;
$ind = 0;
$length = count($items);
$arr = array();
for($i = 0; $i < $columns && $ind < $length; $i++) {
$temp = array();
for($j = 0; $j < $perCol; $j++) {
$obj = new JObject();
$obj->image = $items['gallery' . $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
}
if ($extra > 0) {
$obj = new JObject();
$obj->image = $items['gallery'. $ind]['photo'];
$obj->alt_text = $items['gallery'. $ind]['alt_text'];
$temp[] = $obj;
$ind++;
$extra--;
}
$arr[] = $temp;
}
}

form two arrays in a for loop checking for ignition state change condition

I need a logic which checks for ignition state change and if the ignition is on push that index into start array and if state then changes to off push into stop array so that I can consider one start array index to stop array index as one trip. I had a logic but this pushes all the start values and all the stop values I only need state change index
$isIgniOn = false; $startArray = array(); $stopArray = array();
for ($i = 0; $i < count($reportData); $i++) {
if ($reportData[$i]['ignition_status'] == 1) {
$startArray[] = $i;
$isIgniOn = true;
} else {
if ($isIgniOn) {
$isIgniOn = false;
$stopArray[] = $i;
} else {
$startArray[] = $i;
}
}
}
The problem is, you are not actually checking to see if the state has changed and need to add some extra logic in your program:
$isIgniOn = false; $startArray = array(); $stopArray = array();
for ($i = 0; $i < count($reportData); $i++) {
if ($reportData[$i]['ignition_status'] == 1 && !$isIgniOn) {
$startArray[] = $i;
$isIgniOn = true;
} elseif ($reportData[$i]['ignition_status'] == 0 && $isIgniOn) {
{
$isIgniOn = false;
$stopArray[] = $i;
}
}
}

check and display multiple values in for loop

I have for loop in which I have to check that the array that I am retriving should not empty. And if empty then don't display that field.
$checkboxes = array();
$text = array();
$Enhanced = array();
$Search = array();
$Landing = array();
$spotlights="";
for ($i = 1; $i <= 20; $i++) {
$checkboxes[$i] = $_POST[$i];
$text[$i] = $_POST[$i.'t'];
}
for ($p = 1; $p <= 20; $p++) {
$checkboxes[$p] = $_POST[$p];
$Enhanced[$p] = $_POST['Enhanced'.$p];
}
for ($q = 1; $q <= 20; $q++) {
$checkboxes[$q] = $_POST[$q];
$Search[$q] = $_POST['Search'.$q];
}
for ($r = 1; $r <= 20; $r++) {
$checkboxes[$r] = $_POST[$r];
$Landing[$r] = $_POST['Landing'.$r];
}
for ($j = 1; $j <= 20; $j++) {
if($checkboxes[$j]!="")
{
$spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong><br>".$Enhanced[$j]."<br>".$Search[$j]."<br>".$Landing[$j]."<br>".$text[$j]."<br><br>";
}
}
echo $spotlights;
In above code, $Enhanced[$j], $Search[$j], $Landing[$j] may or may not be empty as these are checkboxes. If any of them is empty i don't want to print that. In above case if any of three field empty then it display <br/> which i don't want.
If I check them one by one or combination of them then my code will be longer.
How do I check in smarter way? How should I write the line of $spotlights=... so that it will display only non empty variables($Enhanced[$j], $Search[$j], $Landing[$j])?
I have tried bolow which is not working.
using functions:
for ($j = 1; $j <= 20; $j++) {
if($checkboxes[$j]!="")
{
$spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong>".enhanced($j).search($j).landing($j)."<br>".$text[$j]."<br><br>";
}
}
function enhanced($j) {
if($Enhanced[$j]!="")
{
return "<br/>".$Enhanced[$j];
}
}
function search($j) {
if($Search[$j]!="")
{
return "<br/>".$Search[$j];
}
}
function landing($j) {
if($Landing[$j]!="")
{
return "<br/>".$Landing[$j];
}
}
above code not displaying values of $Enhanced[$j], $Search[$j], $Landing[$j]
checking values independently
for ($j = 1; $j <= 20; $j++) {
if($checkboxes[$j]!="")
{
$spotlights .= $spotlights."<strong>".$checkboxes[$j]."</strong>";
if($Enhanced[$j]!="")
{
$spotlights .= "<br>".$Enhanced[$j];
}
if($Search[$j]!="")
{
$spotlights .= "<br>".$Search[$j];
}
if($Landing[$j]!="")
{
$spotlights .= "<br>".$Landing[$j];
}
$spotlights .= "<br>".$text[$j]."<br><br>";
}
values are getting repeated by using above code.
Use array_filter.
Something like that:
$checkboxes = array_filter($checkboxes);
foreach($checkboxes as $checkbox)
{
$j = array_search($checkbox, $checkboxes)
$spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong><br>".$Enhanced[$j]."<br>".$Search[$j]."<br>".$Landing[$j]."<br>".$text[$j]."<br><br>";
echo $spotlights;
}
array_filter will remove all NULL, false or '' from array, but will keep the position, so if you have in checkbox[3] something different, in checkboxes after array_filter will be on position 3. doesn't matter if on position 2 was NULL.
I don't think checking them individually will be a lot of trouble, as there are only three of them, but if you want to keep it one line, the line where you define $spotlights with this:
$spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong>"
.($Enhanced[$j] != "" ? "<br>".$Enhanced[$j] : "")
.($Search[$j] != "" ? "<br>".$Search[$j] : "")
.($Landing[$j] != "" ? "<br>".$Landing[$j] : "")
."<br>".$text[$j]."<br><br>";
Hope this helped.

PHP List All Combination of Elements of Array

I have an array:
$arr=array("A","B","C");
I want to make its all of combination as:
array("A")
array("B")
array("C")
array("A","B")
array("A","C")
array("B","C")
array("A","B","C")
i want to make an process all of this combinations but i don't want generate all combinations, store them in an array and apply function to them. Because this requires a lot of memory with large combinations. I have 40 items for this process (I have long time but i don't have enough memory).
I want to have a function like this:
function ProcessArrayCombinations($array){
foreach($array as $v){
//generate and process next combination of array
print_r($nextcombination);
}
}
Thank you.
This code recognizes the combinations as binary numbers, using the fact that there is a formula which states that the sum of all combinations possible from n elements is 2^n. Knowing its binary logarithm is integer, we can define a model where each possible binary number constructed from n digits is a set of combinations. Code is untested, if there are typos, please, let me know in comments.
function ProcessArrayCombinations($array) {
$status = array();
foreach ($array as $element) {
$status[] = false;
}
$elementCount = count($status);
$trues = 0;
while ($trues < $elementCount) {
$index = 0;
$stop = false;
while ((!$stop) && ($index < count($status)) && ($status[$index])) {
$status[$index] = false;
$trues--;
$index++;
}
$status[$index] = true;
$trues++;
//Found a new combination
//We should print elements from $array located at indexes fulfilling
//the criteria that the element having the same index in $status is true:
//for ($i = 0; $i < count($status); $i++) {
// if ($status[$i}) {
// print
// } else {
// don't print
// }
//}
}
}
I edited and used your function as below. Thank you again Lajos.
function ProcessArrayCombinations($array) {
$status = array();
foreach ($array as $element) {
$status[] = false;
}
$elementCount = count($status);
$trues = 0;
while ($trues < $elementCount) {
$index = 0;
$stop = false;
while ((!$stop) && ($index < count($status)) && ($status[$index])) {
$status[$index] = false;
$trues--;
$index++;
}
$status[$index] = true;
$trues++;
//Found a new combination
//We should print elements from $array located at indexes fulfilling
//the criteria that the element having the same index in $status is true:
for ($i = 0; $i < count($status); $i++) {
if ($status[$i]) {
echo $array[$i];
}
}
echo '<br/>';
}
}

Categories