I have a php var that var_dump's this which is coming from a for loop query to the database.
string(19) "2021-09-14 15:23:27" string(19) "2021-09-17 09:59:28" string(19) "2021-09-23 10:29:33" string(19) "2021-09-24 16:07:50"
I first tried to convert them all to a single array with no luck. when I covert it to an array using explodeI get this which is multiple arrays.
array(1) {
[0]=> string(19) "2021-09-14 15:23:27" }
array(1) {
[0]=> string(19) "2021-09-17 09:59:28" }
array(1) {
[0]=> string(19) "2021-09-23 10:29:33" }
array(1) {
[0]=> string(19) "2021-09-24 16:07:50" }
Ultimately what I need to do is get the strings values separately for different calculations.
for example: I would like to get the first string "2021-09-14 15:23:27" and use it for calculating time passed against another string such as "2021-09-24 16:07:50".
I know I'm missing something and it's probably very simple what is it?
here is the for loop code
$fd = new FormData("database_form");
$links = $another_dbForm->get_links('call_logs', $fd->get_field_struct('datetime'));
for($i=0; $i<count($links); $i++)
{
$fd->set_ticket_id( $links[$i] );
//get values for all logs and set var
$logDate= $fd->get_field('datetime'); //gets all datetimes in strings
var_dump($logDate);
}
I have a PHP Script which works quite fine except that I get this error Message
Undefined index: Array in [...]/exp.php on line 239
On this line there is this code:
$out_kostenstelle = $kostenstellen[$nextShift["kostenstelle"]][1].
"(".$nextShift["kostenstelle"].")";
I think the only part where an Array as an Index can occur ist the part where $nextShift["kostenstelle"] is the index for $kostenstellen.
However when I try to catch this part (it is in a loop with many hundred runs so I can not manually check it) with this code, my script never enters the part inside the if clause
if(is_array($nextShift["kostenstelle"]))
{
echo "<pre>";
var_dump($nextShift);
echo "</pre>";
die();
}
This does not make any sense to me and I tried many things. without success.
I think this might be enough of the code where the error could be but just in case here are the structure of $kostenstellen and $nextShift
Kostenstellen:
array(2) {
[100]=>
array(2) {
[0]=>
string(3) "100"
[1]=>
string(11) "Company A"
}
[200]=>
array(2) {
[0]=>
string(3) "300"
[1]=>
string(12) "Company B"
}
}
and nextShift:
array(4) {
["id"]=>
string(2) "168"
["start_unix"]=>
string(10) "1466780000"
["end_unix"]=>
string(10) "1466812400"
["kostenstelle"]=>
string(3) "100"
}
There's no way around it: the problem is that the index you're trying to use is itself an array.
When you access an array in php, $array[$index], PHP will try to stringify it if it's not already a string or numeric. Stringifying an array gives the literal "Array"; like you have here.
However, there's another possibility: that when you run your loop, the array was stringified already. It means someplace before, someone casted it to a string.
You could check if with such an if:
if(is_array($nextShift["kostenstelle"]) || $nextShift["kostenstelle"] == "Array")
{
echo "<pre>";
var_dump($nextShift);
echo "</pre>";
die();
}
<?php
$array = json_decode('{"image":"9794948495.jpg","image":"9703471814.jpg","image":"9711995133.jpg"}');
echo json_encode($array,JSON_FORCE_OBJECT);
?>
My code is like that.
And it result just like :
{"image":"9711995133.jpg"}
Could you guys help me to have :
{"image":"9794948495.jpg","image":"9703471814.jpg","image":"9711995133.jpg"}
Thank you very much.
You must declare image as an array:
{"image":["9794948495.jpg","9703471814.jpg","9711995133.jpg"]}
Your format is overwriting each time the image key and you end up with the last parsed value :
{"image":"9794948495.jpg","image":"9703471814.jpg","image":"9711995133.jpg"}
when you will var_dump the array you will have:
array(1) { ["image"]=> array(3) { [0]=> string(14) "9794948495.jpg" [1]=> string(14) "9703471814.jpg" [2]=> string(14) "9711995133.jpg" } }
thus the original: $images = array('image'=>array("9794948495.jpg","9703471814.jpg","9711995133.jpg"));
My json data is:
{"jsondata":[[1,7,16,29,41,45],[2,12,21,31,36,45]]}
There are 2 sets of numbers and each set has 6 different number. And the number of total sets can be more according to user's input. So, there may be 3 sets which means 18 numbers, etc. If I copy/paste that in to http://json.parser.online.fr/ , I see no error. So, json data is correct, I guess.
Second, here is my PHP file:
<?php
$input = file_get_contents('php://input');
$result=json_decode($input);
var_dump($result);
?>
That works fine too. I mean, when I run these lines, in the chrome's developer tool, I can see these:
object(stdClass)#1 (1) {
["jsondata"]=>
array(2) {
[0]=>
array(6) {
[0]=>
int(1)
[1]=>
int(7)
[2]=>
int(16)
[3]=>
int(29)
[4]=>
int(41)
[5]=>
int(45)
}
[1]=>
array(6) {
[0]=>
int(2)
[1]=>
int(12)
[2]=>
int(21)
[3]=>
int(31)
[4]=>
int(36)
[5]=>
int(45)
}
}
}
So far, I think, what I have to understand is json data is correct and php can respond it back. So, how can I reach this numbers and assing them into another variable in PHP? My actual aim is send them into the mysql. If I can get them, it is easy.
Some says use key/value pair thing but the problem is, my json data creates itself according to user's input. User clicks some numbers and then the number get appended into the json data. And it continues each of six clicks:
function createjson(){
var c=1;
var json='{"jsondata":[[';
$("#kup td").each(function(){
if($(this).html()!="" && $(this).html()!="SELECTED NUMBERS")
{
json+= parseInt($(this).html())+',';
if($(this).index()%5==0 && $(this).index()!=0) //sixth number selected
{
json=json.substring(0,json.length-1);
json+="],["
}
}
});
json=json.substring(0,json.length-3);
json+="]]}";
return json;
};
I thought I found my answer here but it didn't work either or I did somethings wrong. I am very newbie on these json stuffs, so please don't force me to change whole thing. There must be a way to reach this values. So, please save me guys :-)
You can use json_encode function with second parameter to convert JSON string into array:
$input = '{"jsondata":[[1,7,16,29,41,45],[2,12,21,31,36,45]]}';
$data = json_decode($input, TRUE);
var_dump($data['jsondata']);
In this case you should get associated array that simply for using
In the first HTML + PHP page I use the following command:
$_SESSION['myarray'] = $rows;
where $rows is an array.
In the next page I am trying to retrieve data from the array:
$dhoondo = $_SESSION['myarray'];
foreach ($dhoondo as list($ntag, $strTitle))
{
echo $ntag, $strTitle;
}
But it shows "Array" instead of values, whereas Var_dump($dhoondo); clearly displays the data, even $arrlength=count($dhoondo); shows the exact number of items.
What's wrong?
The
var_dump($dhoondo);
result is array(20) { [0]=> array(13) { ["ISBN"]=> string(13) "9780849329500" ["TTAG"]=> string(5) "20752" ["TITLE"]=> string(76) "CRC HANDBOOK OF HIGH RESOLUTION INFRARED .......
Just use:
$dhoondo = $_SESSION['myarray'];
foreach ($dhoondo as $row)
{
echo $row["TTAG"]." - ".$row["TITLE"];
}