Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I dont understand what is wrong with this array and why it does not work, when any other times it worked...
Its simple, i do a foreach loop and insert values to an array:
$insert = [];
foreach ($csv as $i=>$row) {
$insert[$i] = [
'id_customer' => $row[0],
'id_shop_group' => $row[1],
`id_shop` => $row[2],
];
}
The array generated is:
0 =>
array (size=3)
'id_customer' => string '14' (length=2)
'id_shop_group' => string '1' (length=1)
'' => string '1' (length=1)
I dont understand... I am creating my own keys, it should be added to the array, but it isnt... what is the problem?
Your mistake is in the use of the wrong '. In the third array key you used ` instead of '.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
$arr = array (
array(
'type' => 'Tea', 'amount' => $expense_tea, 'desp' => $expense_tea_desc
),
array(
'type' => 'BusFare', 'amount' => $expense_bus, 'desp' => $expense_bus_desc
),
array(
'type' => 'Food', 'amount' => $expense_food, 'desp' => $expense_food_desc
)
);
$myObj1->expensedetails = json_encode($arr);
$json1 = json_encode($myObj1);
Description
I have tried to create a nested json array using php
The Output:
{
"expensedetails": {
"[{\"type\":\"Tea\",\"amount\":\"0\",\"desp\":\"0\"},{\"type\":\"BusFare\",\"amount\":\"0\",\"desp\":\"0\"},{\"type\":\"Food\",\"amount\":\"0\",\"desp\":\"0\"}]"
}
}
Explanation
The json has been converted to string
Expected Output
{
"expensedetails":
[
{"type":"Tea","amount":"0","desp":"0"},
{"type":"BusFare","amount":"0","desp":"0"},
{"type":"Food","amount":"0","desp":"0"},
{"type":"SalaryAdvance","amount":"0","desp":"0"},
{"type":"OT","amount":"0","desp":"0"},
{"type":"IceFlakes","amount":"0","desp":"0"}
]
}
Conclusion
I need a code like in the above-expected code output
But when I tried to do nested json array
Your problem is this:
$myObj1->expensedetails = json_encode($arr);
which sets expensedetails to a string that is already encoded as JSON.
If you want it to be a nested array in the JSON, it needs to be a nested array in the PHP - not an already-encoded string. Just do this:
$myObj1->expensedetails = $arr;
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I got an error while using array_reduce(). I think syntax is correct.
This function returns sum of numbers in an array.
function sum($el1, $el2){
if(!isset($return_sum))
$return_sum = 0;
$return_sum = $return_sum + ($el1+$el2);
return $return_sum;
}
$sum = array_reduce($months_data, 'sum');
Error: array_reduce() expects parameter 2 to be a valid callback, function 'sum' not found or invalid function name.
$months_data:
Array
(
[201905] => 2
[201906] => 7
[201907] => 1
[201908] => 6
[201909] => 2
[201911] => 14
[201912] => 6
[202001] => 5
[202002] => 8
[202003] => 7
)
I'm assuming your function sum, is inside a class, i.e. its the class's method. You should try this:
$sum = array_reduce($months_data, array($this,"sum"));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new at PHP and struggling with given problem.
I have to write a function winner_generator($parameter, $random) that would pick and show random name from given array:
<p><?php echo winner_generator(array(
array('name' => 'Bob'),
array('name' => 'Donald'),
array('name' => 'Peter'),
array('name' => 'Nick')
),rand()); ?></p>
Any ideas on how I should start solving this problem? Many thanks for all your help, looking forward.
So the idea is generating a random number between 0 and the length of the array - 1 (because the index of an array starts at 0). Then you pick the candidate at whatever number the random returns. As you want to echo the winner you need to select the 'name' key from the candidate array.
<?php
function winner_generator(array $candidates){
$rand = rand(0, sizeof($candidates)-1);
return($candidates[$rand]['name']);
}
echo winner_generator(array(
array('name' => 'Bob'),
array('name' => 'Donald'),
array('name' => 'Peter'),
array('name' => 'Nick')
));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm working on this small project and I can't move on. PDO is working perfectly in other queries, no problem there - I hope.
I've checked the site and the previously asked questions did not help me
this is my code:
try { //Step 1
$stmt_dataliiParinte = $db->prepare('INSERT INTO parintei (numeParinte, prenumeParinte, adresaParinte, localitateParinte, codpostalParinte, telefonParinte, mobilParinte, emailParinte, profesiaParinte, locmuncaParinte) VALUES (:numeParinte, :prenumeParinte, :adresaParinte, :localitateParinte, :codpostalParinte, :telefonParinte, :mobilParinte, :emailParinte, :profesiaParinte, :locmuncaParinte) ON DUPLICATE KEY UPDATE emailParinte=VALUES(:emailParinte)');
print_r($stmt_dataliiParinte);
$stmt_detaliiParinte->execute(array(
':numeParinte' => $numeParinte,
':prenumeParinte' => $prenumeParinte,
':adresaParinte' => $adresaParinte,
':localitateParinte' => $localitateParinte,
':codpostalParinte' => $codpostalParinte,
':telefonParinte' => $phoneNumber,
':mobilParinte' => $phoneNumber2,
':emailParinte' => $emailParinte,
':profesiaParinte' => $profesiaParinte,
':locmuncaParinte' => $locmuncaParinte
));
$parinteID = $db->lastInsertId();
} catch(PDOException $e) {
$e->getMessage();
}
this is the print_r($stmt_dataliiParinte):
PDOStatement Object ( [queryString] => INSERT INTO parintei (numeParinte, prenumeParinte, adresaParinte, localitateParinte, codpostalParinte, telefonParinte, mobilParinte, emailParinte, profesiaParinte, locmuncaParinte) VALUES (:numeParinte, :prenumeParinte, :adresaParinte, :localitateParinte, :codpostalParinte, :telefonParinte, :mobilParinte, :emailParinte, :profesiaParinte, :locmuncaParinte) ON DUPLICATE KEY UPDATE emailParinte=VALUES(:emailParinte) )
so by the looks it's a PDOStatement object
I also tried to see if I have some variable error in the array so I also did an print_r on the execute array:
Array ( [:numeParinte] => dasda [:prenumeParinte] => dasdas [:adresaParinte] => dasdasd [:localitateParinte] => asdas [:codpostalParinte] => 23232 [:telefonParinte] => 1231231231231 [:mobilParinte] => 123123123123123 [:emailParinte] => x#x.com [:profesiaParinte] => asd [:locmuncaParinte] => dasdasdasd )
The names of the columns I've checked and double checked.
I also tried removing the ON DUPLICATE KEY UPDATE, but with the same effect.
As I said before other queries work perfectly (example):
$stmt = $db->prepare('INSERT INTO tabere (dentabara,locatie,datastart,dataend,desctabara,pageID,tip,pret) VALUES (:dentabara, :locatie, :datastart, :dataend, :desctabara, :pageID, :tip, :pret)') ;
$stmt->execute(array(
':dentabara' => $dentabara,
':locatie' => $locatie,
':datastart' => $datastart,
':dataend' => $dataend,
':desctabara' => $desctabara,
':pageID' => $paginaTabara,
':tip' => $tip,
':pret' => $pret
));
Spot the difference:
$stmt_dataliiParinte = $db->prepare(' ... snip ...');
^
$stmt_detaliiParinte->execute(array( ... snip ...));
^
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The output from the code below only shows me the last row of my data. Any ideas? I want it to show me all rows.
Here is the code:
$FinalSet = array();
while ($ResultSet = mysqli_fetch_array($Query)){
$FinalSet = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5],
);
}
echo json_encode($FinalSet);
You have to add them to your array. At the moment you are replacing the array itself.
You can do this by using
$FinalSet[] = array(
instead of
$FinalSet = array(
You can also use array_push, which does the same:
array_push($FinalSet, array(......));
You are already initialized $FinalSet = array(); and you are storing data single index so your data has been override and you found last row your data. Now you can use $FinalSet[] instead of $FinalSet. You will get all data.
Try this, You can use $FinalSet[] or array_push() to push the array content into the main array. Currently, you are jut replacing the same array respective of the loop counts. So you getting the last array value in the variable.
$FinalSet[] = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5]
);
instead of
$FinalSet = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5],
);
Try the following in the loop:
while (...) {
$FinalSet[] = array(...);
}