php - Different array structures and breaking of foreach -


i generate xml file dynamic content 2 array types images: first array type when there more 1 images, example:

array(1) {   ["images"]=>   array(3) {     [0]=>     array(2) {       ["url"]=>       string(57) "http://example.net/image1.jpg"       ["default"]=>       bool(true)     }     [1]=>     array(2) {       ["url"]=>       string(57) "http://example.net/image2.jpg"       ["default"]=>       bool(false)     }     [2]=>     array(2) {       ["url"]=>       string(57) "http://example.net/image3.jpg"       ["default"]=>       bool(false)     }   } } 

second type when had 1 image, example:

array(1) {   ["images"]=>   array(2) {     ["url"]=>     string(57) "http://example.net/image111.jpg"     ["default"]=>     bool(true)   } } 

how can make second type use type of first array, because when looping these arrays foreach there problem content. there function fix or something?!

edit: foreach , works:

$productimages = $product->appendchild($this->_xmldoc->createelement('productimages')); if(isset($productinfo['productimages'])) {     foreach($productinfo['productimages']['images'] $image) {         if(!is_array($image) && is_string($image)) {             $productimage = $productimages->appendchild($this->_xmldoc->createelement('productimage'));             $productimage->appendchild($this->_xmldoc->createelement('imagepath', $image));         }         if(isset($image['url']) && is_array($image)) {             $productimage = $productimages->appendchild($this->_xmldoc->createelement('productimage'));             $productimage->appendchild($this->_xmldoc->createelement('imagepath', $image['url']));         }     } } 

this how foreach looks 20 minutes after question. i'll vote first answer, because similar work around :)

cheers, georgi!

i've encountered same issue before using soap call. don't need change structure of array. can change behaviour of foreach loop instead, using is_array. checks if variable array or not. example, using arrays:

$first_array = array(     "images" => array(         array("url" => "http://example.net/image1.jpg", "default" => true),         array("url" => "http://example.net/image2.jpg", "default" => false),         array("url" => "http://example.net/image3.jpg", "default" => false)     ) );  $second_array = array(     "images" => array(         "url" => "http://example.net/image111.jpg",         "default" => true     ) );  echo "results on first array:<br />";  foreach ($first_array["images"] $element) {     if (is_array($element)) {         foreach ($element $value) {             echo $value . "<br />";         }     } else {         echo $element . "<br />";     } }  echo "<br />"; echo "result on second array:<br />";  foreach ($second_array["images"] $element) {     if (is_array($element)) {         foreach ($element $value) {             echo $value . "<br />";         }     } else {         echo $element . "<br />";     } } 

phpfiddle link: http://phpfiddle.org/main/code/4y8j-4jd1


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -