我觉得是时候问了,因为我已经在这个问题上工作了一个多小时。
我需要取一个对象并将其转换为数组 - 完成 我需要在数组的末尾添加一个新元素,如[9] =>价值 - 问题:( 我需要洗牌 - 完成
我尝试的所有东西总是像这样出现
这里是$ wlist作为原始类对象
CAPTCHA_CLASS_Wordlist Object
(
[jsObjectList] => Array
(
[0] => providers
[1] => chemical
[2] => family
[3] => supported
[4] => urban
[5] => produced
[6] => continued
[7] => stream
[8] => administrative
)
)
转换后的是
Array
(
[0] => Array
(
[0] => claim
[1] => continued
[2] => limit
[3] => platform
[4] => websites
[5] => efforts
[6] => dollars
[7] => saint
[8] => family
)
[1] => water
)
必须是[9] =>值
我甚至尝试过$ arrayname [9] = $ value和$ arrayname [0] [9]但没有成功..即使我在foreach中重新创建数组我仍然有同样的问题无法添加额外的最后的元素作为关键[9]
这是我的代码
//convert object to array
$convert = (array)$wlist;
//now add the answer to it
array_push($convert, $questanswer);
//now shuffle
$final = shuffle($convert);
$ questanswer只是一个文字,比如 - house
谢谢:)
更新:这是stdClass之后的样子
//convert object to array
$convert = (array)$wlist;
//new stdClass and load it
$nc = new stdClass;
$nc->questanswer = $questanswer;
//now add the answer to it
array_push($convert[0], $nc);
$finaloptions = shuffle($convert);
Array
(
[0] => stdClass Object
(
[questanswer] => hexagon
)
[1] => Array
(
[0] => labor
[1] => designated
[2] => shops
[3] => load
[4] => fresh
[5] => mass
[6] => patch
[7] => foot
[8] => congress
)
)
答案 0 :(得分:1)
$convert
是一个嵌套数组。在array_push
上使用 //convert object to array
$convert = (array)$wlist;
//now add the answer to it
array_push($convert[0], $questanswer);
//now shuffle
$final = shuffle($convert);
。
$nc = new StdClass...
修改强>
如果您想继续这条路线,答案是返回原始代码(例如,删除array_push($convert, $questanswer);
内容),而不是array_push($conver[0], $questanswer);
使用CAPTCHA_CLASS_Wordlist
但是,您可能想先考虑一下为什么要这样做。如果此$questanswer
是您已创建的类,则应考虑添加一个只需将<?php
class CAPTCHA_CLASS_Wordlist
{
JsObjectList = [...];
... your other code ...
public function getShuffledObjectListWithAnswer($questionAnswer)
{
$tempCopy = $this->JsObjectList;
//now add the answer to it
array_push($tempCopy, $questionAnswer);
//now shuffle
return shuffle($tempCopy);
}
}
传递给的函数,该函数将返回所需的数组。
如果这是您的班级定义文件...
<强> CAPTCHA_CLASS_Wordlist.php 强>
$wlist
现在,您可以像这样使用$final = $wlist->getShuffledObjectListWithAnswer($questanswer);
:
getShuffledObjectListWithAnswer
完成。
P.S。当然,您不必调用函数SELECT
CrmDesk.Request.*
FROM
CrmDesk.Request
INNER JOIN
CrmDesk.Customer ON CrmDesk.Request.CustomerId = CrmDesk.Customer.Id
WHERE
(CrmDesk.Customer.GroupeId = '84b4ea32-b39b-49d3-bb24-35fd933fc594')
AND (RequestStatus != 4)
AND CrmDesk.Request.CustomerId IS NOT NULL
AND RequestStatus != 4
UNION
SELECT
CrmDesk.Request.*
FROM
CrmDesk.Customer
INNER JOIN
CrmDesk.CustomerOrder ON CrmDesk.Customer.Id = CrmDesk.CustomerOrder.CustomerId
INNER JOIN
CrmDesk.Request ON CrmDesk.CustomerOrder.Id = CrmDesk.Request.OrderId
WHERE
(CrmDesk.Customer.GroupeId = '84b4ea32-b39b-49d3-bb24-35fd933fc594')
AND (RequestStatus != 4)
UNION
SELECT
*
FROM
[CrmDesk].[Request]
WHERE
UserFrom IS NOT NULL
AND RequestStatus != 4
AND OwnerId IS NOT NULL
AND UserFrom IN (SELECT Id FROM [Karbazi].[User]
WHERE Id = '84b4ea32-b39b-49d3-bb24-35fd933fc594'
AND RequestStatus != 4
UNION
SELECT Id FROM [Karbazi].[User]
WHERE ParentUserId = '84b4ea32-b39b-49d3-bb24-35fd933fc594')
OR OwnerId IN (SELECT UserId
FROM [CrmDesk].[Customer]
WHERE groupeId = '84b4ea32-39b-49d3-bb24- 35fd933fc594')
AND UserFrom IS NOT NULL
AND RequestStatus != 4
UNION
SELECT *
FROM [CrmDesk].[Request]
WHERE UserToDo IN (SELECT id
FROM [Karbazi].[User]
WHERE ParentUserId='84b4ea32-b39b-49d3-bb24-35fd933fc594'
OR id ='84b4ea32-b39b-49d3-bb24-35fd933fc594' )
AND RequestStatus != 4
。看起来这似乎是描述性的。你更了解它实际应该做的事情的人可以提出更好的东西。
答案 1 :(得分:0)
搜索要添加$questanswer
的索引。让索引为$search_index
。
$search_index = 0;
$convert = (array) $wlist;
//now add the answer to it
array_push($convert[$search_index], $questanswer);
$convert[$search_index]
是数组,您也可以使用简单语句来完成。
$convert[$search_index][] = $questanswer;
答案 2 :(得分:0)
$wlist = new stdClass();
$wlist->property = 'Here we go';
$wlist->property1 = 'Here we go1';
$wlist->property11 = 'Here we go2';
var_dump($wlist);
$questanswer = 'bla';
//convert object to array
$convert = (array)$wlist;
var_dump($convert);
//now add the answer to it
array_push($convert, $questanswer);
var_dump($convert);
//now shuffle
$final = shuffle($convert);
var_dump($convert);
exit;