Saltern Saltern - 1 month ago 19
PHP Question

php :select column in array with Undefined index error

i want get column in php array
$slider is this :

array(6) {
[0]=>
array(1) {
["image"]=>
string(32) "themes/public/slideshow/ads1.jpg"
}
[1]=>
array(1) {
["title1"]=>
string(1) "."
}
[2]=>
array(1) {
["image"]=>
string(32) "themes/public/slideshow/ads2.jpg"
}
[3]=>
array(1) {
["title1"]=>
string(1) "."
}
[4]=>
array(1) {
["image"]=>
string(32) "themes/public/slideshow/ads3.jpg"
}
[5]=>
array(1) {
["title1"]=>
string(1) "."
}
}


when i want get column of this array php has error
Undefined index
!
this error is exist in server but isnt exist in localhost!

foreach ($slider as $slide)
{

$s = $slide['image'];

?>


<div>
<img u="image" src2="<?php echo $url . "/$s" ?> " />
</div>

<?php

}

?>


Undefined index: image in this line :
$s = $slide['image'];

Answer

The error you get is pretty normal, due to the way you $slider is built!

When you do foreach ($slider as $slide) you get successive $slide which are alternatively containing image or title1: in other words one of two members don't contain the image index you're looking for.

In fact, here is how you should build your array:

array(6) {
  [0]=>
  array(2) {
    ["image"]=>
    string(32) "themes/public/slideshow/ads1.jpg"
    ["title1"]=>
    string(1) "."
  }
  [1]=>
  array(2) {
    ["image"]=>
    string(32) "themes/public/slideshow/ads2.jpg"
    ["title1"]=>
    string(1) "."
  }
  [2]=>
  array(2) {
    ["image"]=>
    string(32) "themes/public/slideshow/ads3.jpg"
    ["title1"]=>
    string(1) "."
  }
}

Then you may work as you expected...