php - converting object to array then adding element to end of array then shuffle -


i figured time ask because have been working on over hour.

i need take object , convert array - done need add new element end of array [9] => value - issue :( need shuffle array - done

everything try comes out

here $wlist original class object

 captcha_class_wordlist object  ( [jsobjectlist] => array     (         [0] => providers         [1] => chemical         [2] => family         [3] => supported         [4] => urban         [5] => produced         [6] => continued         [7] => stream         [8] => administrative     )   ) 

which after conversion this

array ( [0] => array     (         [0] => claim         [1] => continued         [2] => limit         [3] => platform         [4] => websites         [5] => efforts         [6] => dollars         [7] => saint         [8] => family     )  [1] => water ) 

it has [9] => value

i have tried $arrayname[9] = $value , $arrayname[0][9] without success.. if recreate array inside foreach still have same problem not being able add element @ end key [9]

here code

      //convert object array      $convert = (array)$wlist;       //now add answer      array_push($convert, $questanswer);       //now shuffle       $final = shuffle($convert); 

$questanswer text word - house

thanks :)

update: here looks after stdclass

         //convert object array      $convert = (array)$wlist;       //new stdclass , load      $nc = new stdclass;      $nc->questanswer = $questanswer;       //now add answer      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     )  ) 

$convert nested array. use array_push on it's first element.

  //convert object array  $convert = (array)$wlist;   //now add answer  array_push($convert[0], $questanswer);   //now shuffle  $final = shuffle($convert); 

edit

if want keep going route, answer return original code (eg, remove $nc = new stdclass... stuff) except instead of array_push($convert, $questanswer); use array_push($conver[0], $questanswer);

however, might want think why you're doing in first place. if captcha_class_wordlist class you've created, should consider adding function pass $questanswer , returns desired array.

if class definition file...

captcha_class_wordlist.php

<?php class captcha_class_wordlist {     jsobjectlist = [...];      ... other code ...      public function getshuffledobjectlistwithanswer($questionanswer)     {         $tempcopy = $this->jsobjectlist;           //now add answer          array_push($tempcopy, $questionanswer);           //now shuffle          return shuffle($tempcopy);     } } 

now, can use $wlist this:

$final = $wlist->getshuffledobjectlistwithanswer($questanswer); 

done.

p.s. of course, wouldn't have call funciton getshuffledobjectlistwithanswer. seems descriptive. know better it's supposed doing come better.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -