-3

i have an array and i want to convert this array in comma seprated string by implode function but this is not working. my code is below.

 <?php 
$relatedSlides = $result['RelatedSlideshows'];
  $relatedSlides = implode(",",$relatedSlides);
   echo $relatedSlides;
  ?>

$result['RelatedSlideshows']; is an array and it is printing perfectly. this is not an multidimensional array. this is simple array. how i do this???

Manish Jangir
  • 5,329
  • 4
  • 42
  • 75

4 Answers4

3

A simpleXML object is not an Array. It is an object that under some circumstances can be treated like an array. Manual Reference

The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.

Try:

$relatedSlides = array();
$it = $result->RelatedSlideshows->getIterator();
// Iterate over the values in the ArrayObject:
foreach ($it as $key=>$val)
{
    $relatedSlides[] = $val;
}
 $relatedSlides = implode(",",$relatedSlides);
cjds
  • 8,268
  • 10
  • 49
  • 84
garyamort
  • 81
  • 4
2
<?php
ini_set('display_errors',1);  error_reporting(E_ALL);
$relatedSlides = $result['RelatedSlideshows'];
  $relatedSlides = implode(",",$relatedSlides);
   echo $relatedSlides;
?>

Gives me:

Notice:  Undefined variable: result in ******.php on line 4

Warning:  implode() [function.implode]: Invalid arguments passed in ******.php on line 5

Hopefully there is enough implicit tips in there.

Edit:

try:

   $relatedSlides = $result['RelatedSlideshows'] -> RelatedSlideshowID;
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • But that's irrelevant since he said he has the original array in your code and you don't... Thus you get errors. – Shomz Nov 09 '11 at 17:29
  • I know... the point of my post was that he can either tell us what is $result and so on... or use the error functions and see it for himself what is not working in his code. – Esailija Nov 09 '11 at 17:31
  • actually i forgot to tell you that it is a simple xml object – Manish Jangir Nov 09 '11 at 17:37
  • implode takes array not simple xml objects :P – Esailija Nov 09 '11 at 17:52
  • then how to convert this simple xml object array into to simple array – Manish Jangir Nov 09 '11 at 17:55
  • Did you try `$relatedSlides = $result['RelatedSlideshows'] -> RelatedSlideshowID; $relatedSlides = implode(",",$relatedSlides); echo $relatedSlides;` ? – Esailija Nov 09 '11 at 17:59
  • SimpleXMLElement Object ( [RelatedSlideshowID] => Array ( [0] => 1036425 [1] => 1036436 [2] => 4077598 [3] => 1036432 [4] => 1036434 )) – Manish Jangir Nov 09 '11 at 17:59
  • yes it is giving "Warning: implode() [function.implode]: Invalid arguments passed in D:\wamp\www\viewslide.php on line 168" – Manish Jangir Nov 09 '11 at 18:01
  • ok so what is the SimpleXMLElement Object? is it `$result` or `$result['RelatedSlideshows']` ? The array is actually the `RelatedSlideshowID` property of some object.. – Esailija Nov 09 '11 at 18:03
  • when i print $result['RelatedSlideshows'] using print_r then i get SimpleXMLElement Object ( [RelatedSlideshowID] => Array ( [0] => 1036425 [1] => 1036436 [2] => 4077598 [3] => 1036432 [4] => 1036434 )) – Manish Jangir Nov 09 '11 at 18:12
  • Then the array is `$array = $result['RelatedSlideshows'] -> RelatedSlideshowID;` do a `print_r` on that `$array` – Esailija Nov 09 '11 at 18:18
  • SimpleXMLElement Object ( [@attributes] => Array ( [rank] => 1 ) [0] => 1036425 ) – Manish Jangir Nov 09 '11 at 18:21
1

Something's wrong with your original array then, check this out.

Do a var dump of $result['RelatedSlideshows'].

Shomz
  • 37,421
  • 4
  • 57
  • 85
0

I guess that's a result from a mysql_fetch_array() or mysql_fetch_assoc(). If you want to implode the array associative array from the result, try implode(",",$result);

But be sure to use mysql_fetch_assoc(), because mysql_fetch_array() returns 2 merged arrays, 1 associative and 1 numeric.

rafael.js
  • 550
  • 6
  • 16