How to remove quotes from a PHP variable (PHP API) [duplicate] - php

This question already has answers here:
Remove double quote in json_encode()
(6 answers)
Closed last month.
I made an API in PHP and un try to remove quotes of my PHP variables.
Here is the code for my API:
for ($i = 0; $i < 48; $i += 1) {
$hourly[$i] =
array(
"seeing" => $seeing[$i]),
"transparency" => $transparency[$i],
"time" => $timeforapi[$i]
);
}
And here is the return of my API:
"hourly": [
{
"seeing": "1.2",
"transparency": "0.5",
"time": 1672074000
},
But as you can see there are quotes for seeing and transparency.
I try this to remove it but it doesn't take it away
"seeing" => str_replace('"', "", $seeing[$i]),

The values of $seeing and $tranparency are strings. Convert them to numbers before putting them into the array.
for ($i = 0; $i < 48; $i += 1) {
$hourly[$i] =
array(
"seeing" => floatval($seeing[$i])),
"transparency" => floatval($transparency[$i]),
"time" => $timeforapi[$i]
);
}

Related

How to merge 2 php arrays? [duplicate]

This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 7 months ago.
I have 2 xPath requests :
$medias = $xpath->query("//strong//a[contains(#class, 'no')]");
$links = $xpath->query("//strong//a[contains(#class, 'no')]/#href");
My goal is to have only one array that contains something like this :
0 => array:1 [▼
"title" => "A besúgó"
"link" => "xyz"
]
I tried this
$i=0;
foreach($medias as $media)
{
$tab[]['titre'] = $media->textContent;
$i++;
}
$i=0;
foreach($medias as $media)
{
$tab[]['lien'] = $media->textContent;
$i++;
}
dd($tab);
But it's ugly and it does not work. Can you help ?
It's hard to understand what you are trying to achieve, but from what I can tell it is this.
$tab = [];
for ($i = 0; $i < count( $medias ); $i++ ) {
$tab[] = [ 'titre' => $medias[$i], 'lien' => $links[$i] ];
}
you may just use array_merge() function to merging more than 1 arrays
array_merge(array1, array2, array3, ...)

Unset an array value by having array of keys, which leads to it

I have an array which looks pretty much like this:
$array = [
0 => [
'b' => [
'classname1' => 'data1',
'classname2' => 'data2' // This one must go away
],
'classname3' => 'data3',
1 => [ ... ]
],
'a' => [ ... ],
'classnameN' => 'dataN'
];
This array can have any keys (numeric or string) and can be any levels deep. The question is how to unset part of it by having an array, which values are leading to some data in the first array?
For example using $definer = [0,'b','classname2']; we must delete commented part of an array.
I have a solution on how to find, what I need to delete, but I've realised, that I don't know how to travel back and assign new value to initial array:
$array_traveler = $array;
for($i = 0; $i < count($definer) - 1; $i++) {
$array_traveler = $array_traveler[$definer[$i]];
}
unset($array_traveler[$definer[count($definer) - 1]]);
Use "pointer"
$array_traveler = &$array;
for($i = 0; $i < count($definer) - 1; $i++) {
$array_traveler = &$array_traveler[$definer[$i]];
}
unset($array_traveler[$definer[count($definer) - 1]]);

How to get number from between brackets in string in PHP [duplicate]

This question already has answers here:
PHP/REGEX: Get a string within parentheses
(2 answers)
Closed 8 years ago.
Currently I use arrays such as this one for version control of a Mysql database:
$pages_table = array (
"GUID" => array (
"type" => "CHAR(13)",
"length" => 13,
)
"Number" => array (
"type" => "TINYINT(4)",
"length" => 4,
)
"Pagename" => array (
"type" => "VARCHAR(30)",
"length" => 30,
)
It works, but I want to make it more clean, like:
$pages_table = array (
"GUID" => "CHAR(13)",
"Number" => "TINYINT(4)",
"Pagename" => "VARCHAR(30)",
);
And then if I iterate over the array, I want to set $new_length (INT) to the number between the brackets of the $new_type string:
while ($column = key($pages_table)) {
$new_type = current($pages_table);
$new_length = //Get this value from $new_type;
if ($existing_table[$column]['length'] < $new_length) {
$modify[$column] = $new_type;
}
next($pages_table);
}
Use regular expressions:
preg_match('/\(\d+\)/', $subject, $matches);
$new_length = $matches[0];
You could shorten the pattern if it is guaranteed that there are no other numbers in the string:
preg_match('/\d+/', $subject, $matches);
$new_length = $matches[0];
while ($column = key($pages_table)) {
$new_type = current($pages_table);
$hasLength = (preg_match('/\(\d+\)/', $new_type, $matches) == 1);
$new_length = intval($matches[0]);
if ($hasLength && $existing_table[$column]['length'] < $new_length) {
$modify[$column] => $new_type;
}
next($pages_table);
}
$new_length = (int) preg_replace('/\D/', '', $new_type);

Overlapping Associative Arrays

I need to overlap data from multiple associative arrays with the following considerations:
If a matching key exists, overwrite it
If a key exists but doesn't match, append new value to that element
If neither of the above, create an element to store the value
Take for example the following structures:
<?php
for ($i = 0; $i < 10; $i++) {
$table["table_$i"] = array(
"cell_0" => array(
'row' => 12,
'column' => 5
)
);
}
for ($i = 4; $i < 12; $i++) {
$table["table_$i"] = array(
"cell_0" => array(
'row' => 9,
'column' => 8
)
);
}
for ($i = 5; $i < 15; $i++) {
$table["table_$i"] = array(
"cell_1" => array(
'row' => 4,
'column' => 1
)
);
}
?>
The desired output would look like this:
{"table_0":{"cell_0":{"row":12,"column":5}},"table_1":{"cell_0":{"row":12,"column":5}},"table_2":{"cell_0":{"row":12,"column":5}},"table_3":{"cell_0":{"row":12,"column":5}},"table_4":{"cell_0":{"row":9,"column":8}},"table_5":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_6":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_7":{"cell_1":{"row":4,"column":1}},"table_8":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_9":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_10":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_11":{"cell_0":{"row":9,"column":8},"cell_1":{"row":4,"column":1}},"table_12":{"cell_1":{"row":4,"column":1}},"table_13":{"cell_1":{"row":4,"column":1}},"table_14":{"cell_1":{"row":4,"column":1}}}
Take note from the desired output that the value of cell_0 doesn't replace the value of cell_1: I couldn't get the desired output using array_merge() in this case.
Any help would be appreciated--thanks!
Check array_merge and array_unique php functions.

can this be calculated without using two for loops

I have the following structure of data in an array and I am trying to calculate the total duration:
$elements = array(
'elementfrom-work' => "09:00",
'elementto-work' => "17:00",
'elementdays-work' => "5",
'elementfrom-karate' => "18:00",
'elementto-karate' => "20:00",
'elementdays-karate' => "3",
'elementfrom-stamp' => "21:00",
'elementto-stamp' => "22:00",
//it doest have the default days 'elementdays-stamp' set
//so it will take the default 7
'element-simple1' => "4", //it will take the default 7
'element-simple2' => "8", //it will take the default 7
'element-simple3' => "1",
'elementdays-simple3' => "1", //day is set
);
I have managed to do it but my code is messy, for each item it gets the sub string and runs another for loop to check if any other elements exists when it is not simple (like days) .
I am trying to calculate for each item the total duration e.g outcome is:
Work:40
Karate:6
Stamp:7
Simple1=28
Simple2=56
Simple3=1
total duration:138
Can this be done without two for loops and how ? If it is not possible how would you calculate it.
I actually found this problem quite interesting, so you can do something like:
$elements[] = array(
'elementfrom-work' => "09:00",
'elementto-work' => "17:00",
'elementdays-work' => "7",
'elementfrom-karate' => "18:00",
'elementto-karate' => "20:00",
'elementdays-karate' => "3",
'elementfrom-stamp' => "21:00",
'elementto-stamp' => "22:00",
'a' => "21:00",
'b' => "22:00"
);
And use those two functions:
function negative($x)
{
if($x < 0)
{
return -$x;
}
return $x;
}
function isTime($string)
{
$split = explode(":", $string);
if(isset($split[1]))
{
return true;
}
return false;
}
foreach($elements as $key => $val)
{
$total = 0;
$temp = 0;
$i = 0;
foreach($val as $innerKey => $time)
{
$isTime = isTime($time);
$split = explode(":", $time);
$h = $split[0];
switch($i)
{
case 0:
$temp -= $h;
break;
case 1:
$temp += $h;
break;
case 2:
if($isTime)
{
$mult = $temp *= 7;
$unsigned = negative($mult);
$total += $unsigned;
$temp = 0;
$temp -= $h;
$i = 0;
break;
}
$mult = $temp *= $h;
$unsigned = negative($mult);
$total += $unsigned;
$temp = 0;
$i = -1;
break;
default:
break;
}
$i++;
}
echo $total;
}
Your idea of the carry is a bit funky but something like that should work.
It looks like you know the expected name of the days item... check if it's exists instead of looping, iterate the array keys, when you get to "elementfrom-work" you can check if array got key "elementdays-work" instead of re iterate to search this item in N level complexity...

Categories