2017-08-14 66 views
-5

我试图找出如何排列内部某个对象,但很难得到明确的答案。真的很头痛。我的程序运行如下:PHP:我们可以排列一个对象吗?

欲了解更多信息:我在底部附加了$ df和$ type的原始代码。

下面是代码:

$arr1 = array('uncle1','uncle2','uncle3','uncle4','uncle5'); 
$arr2 = array('aunt1','aunt2','aunt3','aunt4','aunt5'); 
$arr3 = array('child1','child2','child3','child4','child5'); 

$un = 'uncle1'; 
$au = 'aunt1'; 
$ch = 'child1'; 

switch ($type){ 
case 'family': 

$family1 = ($df->createElement($un,$au,$ch); 

    //------- How I make an object base on many family at these arrays above, I try to foreach, but it can not run under function switch, It should has many objects like: $family1, $family2, $family3,.. with data from the array. 

$df->sendFamily(array($family)); 
} 
    //---------it should be like: $df>sendFamily(array($family1,$family2,....)) but I can not array an object 

这里$ DF的原始代码和$类型:

if (isset($_GET['msg']) && ! empty($_GET['msg'])) 
{ 
    $df = new Chatfuel(); 
    $type = strtolower($_GET['msg']); 

//----- Begin an Chatfuel 

    class Chatfuel 
{ 
protected $response = array(); 

    public function __construct($debug = FALSE) 
    { 
    if ((! $debug) && (! isset($_SERVER['HTTP_USER_AGENT']) OR  strpos($_SERVER['HTTP_USER_AGENT'], 'Apache-HttpAsyncClient') === FALSE)) { 
    exit; 
    } 
} 
public function __destruct() 
    { 
    if (count($this->response) > 0) { 
    try { 
    header('Content-Type: application/json'); 
    echo json_encode(array('messages' => $this->response)); 
    exit; 
    } catch (Exception $e) { 

    } 
} 
} 
    public function createElement($title, $image, $subTitle, $buttons) 
{ 
    if ($this->isURL($image) && is_array($buttons)) { 
    return array(
    'title'  => $title, 
    'image_url' => $image, 
    'subtitle' => $subTitle, 
    'buttons' => $buttons 
); 
} 

return FALSE; 
} 
+0

为什么不在类中返回一个返回相关对象数组的方法? –

+0

感谢Ofir。当然,我将使用foreach从数组中获取每个数据。但我卡住了,因为我不能在函数createElement中使用循环:( – Axxx

+0

)您需要修改$ df类,但是您可以。该类的代码是什么? –

回答

0

通常可以阵列的目的是通过下面的代码:

$array = array(); 
foreach ($object as $key=>$value) { 
    $array[$key] = $value; 
} 

但可悲的是我根本不理解你的代码示例。什么是$ df,$ type是从哪里来的?

+0

感谢这么多的病人我添加更多的信息作为你提到: - $ DF:是一个目的是使里面的内容的标签,因为它从一个图书馆需要和运行良好 - $键入:是一个对象在用户输入时得到一个事件,它从一个库中运行并且运行良好。 – Axxx

相关问题