I want to do the same actions thru some variables. So I created the variables of variables. But it is throwing me error - "Invalid argument supplied for foreach()" when I am looping thru $$a. I have check the type of the variable. It is array. Then what is the error ?
$edu_data = Json::decode($model->education);
$exp_data = Json::decode($model->experience);
$avail_data = Json::decode($model->availability);
$docs_data = Json::decode($model->documents);
$model_edu = new \admin\models\ApplicantEducation();
$model_exp = new \admin\models\ApplicantExperience();
$model_avail = new \admin\models\Availability();
$model_cre = new \admin\models\Credential();
$all = array('edu_data' => 'model_edu', 'exp_data' => 'model_exp', 'avail_data' => 'model_avail', 'docs_data' => 'model_cre');
foreach ($all as $a => $s)
{
$arr = $$a;
foreach ($arr as $v)
{
$$s->applicant_id = $applicant_id;
foreach ($arr[1] as $k1 => $v1)
{
$$s->$k1 = $v[$k1];
}
$$s->save();
}
}
Your array does not contain your variables (e.g. $model_edu), but only their respective names as string values ('model_edu'). Edit: My bad, I didn't notice this is intentional.
I suggest using a function:
function process_data($model, $data, $applicant_id) {
foreach ($data as $v) {
$model->applicant_id = $applicant_id;
foreach ($data[1] as $k1 => $v1)
{
$model->$k1 = $v[$k1];
}
$model->save();
}
}
process_data($model_edu, $edu_data);
process_data($model_exp, $exp_data);
process_data($model_avail, $avail_data);
process_data($model_docs, $docs_data);
Your code will be more easily comprehendable.
Apart from that, you can debug your code like this to find out exactly where and when the error happens:
foreach ($all as $a => $s)
{
$arr = $$a;
var_dump($arr);
foreach ($arr as $v)
{
$$s->applicant_id = $applicant_id;
var_dump($arr[1]);
foreach ($arr[1] as $k1 => $v1)
{
$$s->$k1 = $v[$k1];
}
$$s->save();
}
}
See if this is the expected value and proceed from there on.
Find out if the reason is an unexpected value in one of your variables or if it is an error in the code logic.
Related
I'm building support decision system using electre method
While i compile my code i got an error
the error is : Warning Division By zero
Here is the function that i created
function normalisasi(){
$array = ratarata();
$arr = spk_rel();
$nilai = array();
$data = array();
foreach ($array as $key => $value) {
$nilai[$key]=sqrt(array_sum($value));
}
foreach($arr as $key => $value){
foreach($value as $k => $v){
$data[$key][$k] = $v / $nilai[$k];
}
}
return $data;
}
The error is in
$data[$key][$k] = $v / $nilai[$k];
could you please help me ?
You will get a error when you divide any by 0.
You should check $nilai[$k] before make a divide by 0.
if (!empty($nilai[$k])) {
$data[$key][$k] = $v / $nilai[$k];
}
You didn't specified what the logic is about, but if you're expecting some values of $nilai to be 0 and you can just skip them, there 2 ways to achieve that:
Checking for value inside loop
foreach($arr as $key => $value){
foreach($value as $k => $v){
if ($nilai[$k]) {
$data[$key][$k] = $v / $nilai[$k];
}
}
}
This expression if ($nilai[$k]) will be true when $nilai[$k] is either positive or negative and will be false when it's 0.
You can remove 0 values from $nilai array before loop
foreach ($array as $key => $value) {
$nilai[$key]=sqrt(array_sum($value));
}
$nilay = array_filter($nilay);
foreach($arr as $key => $value){
...
}
In this case array_filter called without second argument (filter callback) will just remove all 0 values from array passed as first argument.
its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO
I've a database table called menu_short & I've insert json data in m_short column:
[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5}]}]
I'm using nestable menu for parent child menu. I use this code to extract:
foreach ($menuDisplay as $menus) {
$json = $menus['m_short'];
$array = json_decode($json, true);
foreach ($array['id'] as $k => $v) {
echo $v;
}
}
I'm getting this 'Undefined index: id & Invalid argument supplied for foreach()' error. I'm sure I made some mistake.
DB Picture-
Something like this should work....
foreach ($menuDisplay as $menus){
$json = $menus['m_short'];
$arrayx = json_decode($json, true);
foreach ($arrayx as $k1 => $array) {
var_dump($array['id']);
/*foreach ($array['id'] as $k2 => $v) {
echo $v;
}*/
}
}
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
i am trying to access the value of an array that i have created, but it seems to fail.
I am looping through an array that sends VIA http, and adding the docno and entryno to new array called $ArrID, it can be added to the new array, however when i try to access the ArrID it seem to get nothing from the array itself while i am confident that ArrID contain the items
CODE
$ArrID = [];
foreach ($form_data_body as $key => $value) {
$docno = $value -> doc_no;
$entryno = $value -> entryno;
if($mysqli->query($sql) == 1){
$itemArr = array('docno' => $docno, 'entryno' => $entryno);
$ArrID[] = $itemArr;
}
}
if(count($ArrID)>0){
foreach ($ArrID as $key => $values) {
echo $values -> docno;
}
}
You are mixing with object and array
see http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/language.types.object.php
$ArrID = [];
foreach ($form_data_body as $key => $value) {
$docno = $value['doc_no'];
$entryno = $value['entryno'];
if($mysqli->query($sql) == 1){
$itemArr = array('docno' => $docno, 'entryno' => $entryno);
$ArrID[] = $itemArr;
}
}
if(count($ArrID)>0){
foreach ($ArrID as $key => $values) {
echo $values['docno'];
}
}
How are you sure that your array has the data? Make sure by echoing the size of the array and see if its greater then zero.
Try the code below and check if it works or not. There was a redundant assign and reassign code, actually you were assigning array to a variable, and variable to an array again.
$ArrID = [];
foreach ($form_data_body as $key => $value) {
$docno = $value -> doc_no;
$entryno = $value -> entryno;
if($mysqli->query($sql) == 1) {
$ArrID[] = array('docno' => $docno, 'entryno' => $entryno);
}
}
if(count($ArrID)>0) {
foreach ($ArrID as $key => $values) {
echo $values -> docno;
}
}
I'm trying to fill an array with my foreach loop, but I don't get it to work. What am I doing wrong?
$a = array();
$activities = Project::getProjectnames($_DB, $projectnaam);
if(!empty($activities)) {
foreach($activities as $k => $v) {
$a .= array_fill($v['name']);
}
}
All I get back is the string Array...
you're concatenating a string there. You need to push the item into the array.
foreach($activities as $k => $v) {
$a[] = $v['name'];
}