When I run this code, it works, but it gives me NOTICE 'Undefined offset 1'. Can someone tell me how can I fix this? $this->_data is a multidimensional array.
foreach ($this->_data as $key => $values) {
$this->_param[] = explode(",", $values[1]);
}
You need to check if there is any data in $values[1]
Use this:
foreach ($this->_data as $key => $values) {
if (isset($values[1]){
$this->_param[] = explode(",", $values[1]);
}
}
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.
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.
Why am I getting always the last value from the array? I'm stuck on what I'm doing wrong.
Please help! :p
$array = array("id"=>"4", "id"=>"5", "id"=>"6");
foreach($array as $key => $value) {
$screenshots[] = $value;
}
var_dump($screenshots);
As Rizier said, your keys in an associative array cannot have the same name.
This should work:
$array = array("id1"=>"4", "id2"=>"5", "id3"=>"6");
foreach($array as $key => $value) {
$screenshots[] = $value;
}
var_dump($screenshots);
I have an array $pedidos which I fill by:
$pedido[$nombreProducto['nombre']] = $cantidad;
So, when U try to iterate over it with this loop:
foreach ($pedidos as $key => $value) {
echo $key."-->".$value;
}
I'm getting the error log message:
Invalid argument supplied for foreach()
How should I iterate over it?
Your array is called $pedido change the for each with this:
foreach ($pedido as $key => $value) {
echo $key."-->".$value;
}
In foreach you have $pedidos, but you are filling array $pedido
Why does this yield this:
foreach( $store as $key => $value){
$value = $value.".txt.gz";
}
unset($value);
print_r ($store);
Array
(
[1] => 101Phones - Product Catalog TXT
[2] => 1-800-FLORALS - Product Catalog 1
)
I am trying to get 101Phones - Product Catalog TXT.txt.gz
Thoughts on whats going on?
EDIT: Alright I found the solution...my variables in my array had values I couldn't see...doing
$output = preg_replace('/[^(\x20-\x7F)]*/','', $output);
echo($output);
Cleaned it up and made it work properly
The doc http://php.net/manual/en/control-structures.foreach.php clearly states why you have a problem:
"In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference."
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>
Referencing $value is only possible if the iterated array can be referenced (i.e. if it is a variable). The following code won't work:
<?php
/** this won't work **/
foreach (array(1, 2, 3, 4) as &$value) {
$value = $value * 2;
}
?>
Try
foreach( $store as $key => $value){
$store[$key] = $value.".txt.gz";
}
The $value variable in the array is temporary, it does not refer to the entry in the array.
If you want to change the original array entry, use a reference:
foreach ($store as $key => &$value) {
// ^ reference
$value .= '.txt.gz';
}
You are rewriting the value within the loop, and not the key reference in your array.
Try
$store[$key] = $value.".txt.gz";
pass $value as a reference:
foreach( $store as $key => &$value){
$value = $value.".txt.gz";
}
Try
$catalog = array();
foreach( $store as $key => $value){
$catalog[] = $value.".txt.gz";
}
print_r ($catalog);
OR
foreach( $store as $key => $value){
$store[$key] = $value.".txt.gz";
}
print_r ($store);
Depends on what you want to achieve
Thanks
:)
I believe this is what you want to do:
foreach( $store as $key => $value){
$store[$key] = $value.".txt.gz";
}
unset($value);
print_r ($store);
How about array map:
$func = function($value) { return $value . ".txt.gz"; };
print_r(array_map($func, $store));
foreach(array_container as & array_value)
Is the way to modify array element value inside foreach loop.