PHP内爆功能不起作用

时间:2011-11-09 17:11:54

标签: php arrays string implode

我有一个数组,我想通过implode函数以逗号分隔字符串转换此数组,但这不起作用。我的代码如下。

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

$结果[ 'RelatedSlideshows'];是一个数组,它打印得很完美。这不是一个多维数组。这是简单的数组。我是怎么做的???

4 个答案:

答案 0 :(得分:3)

simpleXML对象一个数组。在某些情况下,对象可以像数组一样对待。 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.

尝试:

$relatedSlides = array();
$it = $result->RelatedSlideshows->getIterator();
// Iterate over the values in the ArrayObject:
foreach ($it as $key=>$val)
{
    $relatedSlides[] = $val;
}
 $relatedSlides = implode(",",$relatedSlides);

答案 1 :(得分:2)

<?php
ini_set('display_errors',1);  error_reporting(E_ALL);
$relatedSlides = $result['RelatedSlideshows'];
  $relatedSlides = implode(",",$relatedSlides);
   echo $relatedSlides;
?>

给我:

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

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

希望那里有足够的隐含提示。

编辑:

尝试:

   $relatedSlides = $result['RelatedSlideshows'] -> RelatedSlideshowID;

答案 2 :(得分:1)

原始阵列出现问题,请检查this

执行$ result ['RelatedSlideshows']的var转储。

答案 3 :(得分:0)

我猜这是mysql_fetch_array()或mysql_fetch_assoc()的结果。 如果要从结果中内爆数组关联数组,请尝试implode(“,”,$ result);

但请务必使用mysql_fetch_assoc(),因为mysql_fetch_array()返回2个合并数组,1个关联数组和1个数字数据。