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"));
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 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 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 '.
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
I have an editable page contact portion and when I edit the code of google maps I get the following error:
Parse error: syntax error, unexpected 'https' (T_STRING), expecting ',' or ';' in /home/asaav574/public_html/salumno11/app/config/Contactanos.php on line 10
Here the code:
class Contactanos {
public $emails = array ( array ( 'nombre' => 'mi nombre','email' => correo#gmail.com',
) ,) ;
public $GoogleMaps = '<iframe src="https://mapsengine.google.com/map/embed?mid=zVd_LU3lNWZM.k8sTRRp8BROU" width="600" height="400"></iframe>';
public $emailForm= 'micorreo#gmail.com';
}
I edit when I inserted the double quote but if I change the editor and I leave the single quote works correctly the issue is that my client is going to change this information and I will not leave this mistake when you make a change.
Rewrite this line:
public $emails = array ( array ( 'nombre' => 'mi nombre','email' => 'correo#gmail.com'));
An ' was missing in front of the email, resulting in a parse error in the next line.
Generally in PHP when it say you have an error at a specific line, the error is in previous line.
thanks for your answer. It verda made a mistake when changing the provisional royal mail. Here is the code:
class Contactanos {
public $emails = array (
array ( 'nombre' => 'mi nombre', 'email' => 'micorreo#gmail.com',),
array ( 'nombre' => '','email' => '',),
array ( 'nombre' => '','email' => '',),
);
public $GoogleMaps = "";
public $emailForm= 'micorreo#gmail.com';
}