3

I have 2 blocks of code....

// 1st block

<div id="a1">
<?php

if (is_array($new_array) || is_object($new_array))
{
  foreach ($new_array as $name => $val)
   {
     echo $name . " : " . $val[0] . " , " . $val[1]. " , " . $val[2];
   }
}
unset($new_array);
?>
</div>

2nd block

<div id="a2">
    <?php

    if (is_array($new_array) || is_object($new_array))
    {
      foreach ($new_array as $name => $val)
       {
         echo $name . " : " . $val[0] . " , " . $val[1]. " , " . $val[2];
       }
    }
    unset($new_array);
    ?>
    </div>

Either 1st or 2nd Block will give empty results in a day. Means if Today , 1st block will give empty result & tomorrow 2nd Block will give empty result.... Alternatively....

Issue :

Today, Value is empty for 2nd block , it gave Notice: Undefined variable: new_array error , so I initialized this before 2nd block of code :

$new_array=''; 

it worked fine.... but tomorrow 2nd block code will give this result :

Warning: Illegal string offset , Fatal error: Uncaught Error: Cannot use string offset as an array

So i need to remove this code : $new_array=''; before 2nd block & i need to place before 1st block.....

5 Answers 5

7

What about to use if (isset($new_array)) {...} or to initialize it like array, $new_array = [];

You can add isset to other checks, like this:

if (isset($new_array) && (is_array($new_array) || is_object($new_array))) { .... }

More info about isset()

Sign up to request clarification or add additional context in comments.

6 Comments

i tried $new_array = []; but it give this error : Notice: Undefined variable: new_array
Where you place $new_array = []; ?
use isset($new_array), bro! @MorganFreeFarm give a simple solution.
i am already using this : if (is_array($new_array) || is_object($new_array)) , how i can use if (isset) ? i tried if (isset(if (is_array($new_array) || is_object($new_array))) , but it gave error....
@MorganFreeFarm sorry , i can't unset() , otherwise i for today , i will get same result as yesterday.....
|
0

You can use isset() function to check if $new_array is set or not. Edit your code as below

// 1st Block

<div id="a1">
<?php
  if(isset($new_array)){
    if (is_array($new_array) || is_object($new_array))
    {
       foreach ($new_array as $name => $val)
       {
          echo $name . " : " . $val[0] . " , " . $val[1]. " , " . $val[2];
       }
    }
  }
  unset($new_array);
?>
</div>

// 2nd Block

<div id="a2">
<?php
  if(isset($new_array)){
    if (is_array($new_array) || is_object($new_array))
    {
       foreach ($new_array as $name => $val)
       {
          echo $name . " : " . $val[0] . " , " . $val[1]. " , " . $val[2];
       }
    }
  }
  unset($new_array);
?>
</div>

Comments

0

You are looking for isset()/empty() and perhaps a short if/else.

Example:

<div id="a1">
<?php

if (is_array($new_array) || is_object($new_array))
{
  foreach ($new_array as $name => $val)
   {
     echo $name . " : " . $val[0] . " , " . $val[1]. " , " . $val[2];
   }
}
unset($new_array);
?>
</div>

Let's say $new_array is not set/not an array/not and object. Your if statement will not continue, because it will receive false.

In case you wish to always execute your code you could do something like this: (see the notes for explanation).

<div id="a1">
<?php

$new_array = []; // [] = short for array();. $new_array will now always be an array.

// You could also do something like this:
$new_array = (! is_array($new_array)) ? $new_array = [] : $new_array; // Here you say $new_array is always set with his code and else it will be an empty array.


if (is_array($new_array)) // 
{
  foreach ($new_array as $name => $val)
   {
     echo $name . " : " . $val[0] . " , " . $val[1]. " , " . $val[2];
   }
}
unset($new_array);
?>
</div>

Comments

0

You can check first if array exists or not. If it exists then check if it is empty(if it is an array) or count the number of elements in it is greater than zero (in case of object array)

<div id="a1">
<?php
if (is_array($new_array) || is_object($new_array)) {
    if (isset($new_array)) {
        if (empty($new_array) || count($new_array) > 0) {
            foreach ($new_array as $name => $val) {
                echo $name . " : " . $val[0] . " , " . $val[1] . " , " . $val[2];
            }
        }
    }
}
unset($new_array);
?>
</div>

Comments

-1

data type mismatch in php 7 result in this error. instead of

$new_array=''; 

Initialize like

$new_array=array(); 

Once you unset it destroys that array. So intialize that before first block code

1 Comment

Thanks for answer, i also tried that...... but still it dont work for me as ` new_array` acts as variable. not array.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.