0

I have an array that have an index 0 and index 1 and in some cases the index 1 will not be exist. I want to bypass it and pass only the index 0 or i want to create it and add value to it.

$eq = explode("  -  ", $div->textContent) ;
$p_part_one_name = $eq[0] ;
$p_part_two_name = $eq[1] ;
Hiren Gohel
  • 4,942
  • 6
  • 29
  • 48
Doc Tor
  • 23
  • 1
  • 8

3 Answers3

0

You should check if $eq[index] exists or not before sets it as a variable

$eq = explode("  -  ", $div->textContent) ;
if(isset($eq[0]))
    $p_part_one_name = $eq[0] ;
if(isset($eq[1]))
    $p_part_two_name = $eq[1] ;
Viet Nguyen
  • 2,285
  • 2
  • 26
  • 43
0

HI you can use foreach

foreach ($arr as &$eq) {
   #Here your array value
}

you can check count of array

$result = count($eq );
if($result==2){
    $p_part_one_name = $eq[0] ;
    $p_part_two_name = $eq[1] ;
 }
else{
   $p_part_one_name = $eq[0] ;
}
Robert
  • 3,373
  • 1
  • 18
  • 34
0

You should try this:

The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

$eq = explode("  -  ", $div->textContent) ;

if (array_key_exists(1, $eq)) {
    $p_part_one_name = $eq[0] ;
    $p_part_two_name = $eq[1] ;
} else {
    $p_part_one_name = $eq[0] ;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57