This question already has answers here:
PHP - recursive Array to Object?
(17 answers)
Closed 6 years ago.
i have seen a lot of examples but was unsuccessful to help myself. I have a multidimensional array that i want to convert to object format. I tried going levels deeper (multidimensional) but that all. I want to make it an php object. This is my scenario
// this is my array
Array
(
[_cars] => Array
(
[_car] => Array
(
[0] => Array
(
[_types] => Array
(
[_type] => Array
(
[0] => Array
(
[_cc] => 100
)
[1] => Array
(
[_cc] => 100
)
[2] => Array
(
[_cc] => 1000
)
)
)
)
)
)
)
)
// this is what i want
[_cars] => stdClass Object
(
[_car] => Array
(
[0] => stdClass Object
(
[_types] => stdClass Object
(
[_type] => Array
(
[0] => stdClass Object
(
[_cc] => 999999999999
)
[1] => stdClass Object
(
[_cc] => 999999999999
)
[2] => stdClass Object
(
[_cc] => 999999999999
)
)
)
)
)
)
Not sure if it will work but you can try
$object = json_decode(json_encode($array));
Please check this if this okay
$obj = new stdClass($array); or
$obj = json_decode(json_encode($array));
Related
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 6 years ago.
How to sort/arrange associative array by key,I have an array like this
Array
(
[0] => Array
(
[SF] => stdClass Object
(
['name']=>Miller
['age']=>25
['gender']=>M
)
[PF] => stdClass Object
(
['name']=>Dirk
['age']=>30
['gender']=>M
)
[SG] => stdClass Object
(
['name']=>Marcin
['age']=>24
['gender']=>M
)
[PG] => stdClass Object
(
['name']=>Julit
['age']=>23
['gender']=>F
)
[F] => stdClass Object
(
['name']=>Alex
['age']=>21
['gender']=>M
)
[G] => stdClass Object
(
['name']=>Andrew
['age']=>22
['gender']=>M
)
[C] => stdClass Object
(
['name']=>kevin
['age']=>26
['gender']=>M
)
[UTIL] => stdClass Object
(
['name']=>John
['age']=>24
['gender']=>M
)
)
)
I want to sort/arrange the above array like this PG,SG,SF,PF,C,G,F,UTIL
This is how my array should looks like after sort:
Array
(
[0] => Array
(
[PG] => stdClass Object
(
['name']=>Julit
['age']=>23
['gender']=>F
)
[SG] => stdClass Object
(
['name']=>Marcin
['age']=>24
['gender']=>M
)
[SF] => stdClass Object
(
['name']=>Miller
['age']=>25
['gender']=>M
)
[PF] => stdClass Object
(
['name']=>Dirk
['age']=>30
['gender']=>M
)
[C] => stdClass Object
(
['name']=>kevin
['age']=>26
['gender']=>M
)
[G] => stdClass Object
(
['name']=>Andrew
['age']=>22
['gender']=>M
)
[F] => stdClass Object
(
['name']=>Alex
['age']=>21
['gender']=>M
)
[UTIL] => stdClass Object
(
['name']=>John
['age']=>24
['gender']=>M
)
)
)
Any idea how can I achieve the same?
Being a custom order you can't use any built in PHP sort function. A simple way of doing it would be:
$sort = array('PG', 'SG', 'SF', 'PF', 'C', 'G', 'F', 'UTIL');
$result = array();
foreach ($sort as $key) {
$result[0][$key] = $myArray[0][$key];
}
var_dump($result);
I have been trying to code a mysql query in wordpress but it does not seem to work.
code:
foreach ($postList as $curpostid) {
echo "postID ".$curpostid." + ";
$sql2 = 'SELECT * FROM wp_mr_rating_item_entry WHERE post_id='.$curpostid;
$results2 = $GLOBALS['wpdb']->get_results($sql2) or die(mysql_error());
}
$postList prints as:
Array
(
[0] => 3148
[1] => 3097
[2] => 3048
[3] => 1036
)
These post ids do exist, and the echo shows them up fine. If I enter a specific value for $curpostid as one of the array entries the query works. It seems the query is not accepting the variable form the array.
I have tried making the variable as int (int)$curpostid since the field type is bigint but still not working.
Any help appreciated.
I have managed to place the output in a multidimensional array, but now I am not able to place the values in a single array:
this is the array:
Array
(
[0] => Array
(
[0] => stdClass Object
(
[rating_item_entry_id] => 1
)
[1] => stdClass Object
(
[rating_item_entry_id] => 2
)
[2] => stdClass Object
(
[rating_item_entry_id] => 3
)
[3] => stdClass Object
(
[rating_item_entry_id] => 4
)
[4] => stdClass Object
(
[rating_item_entry_id] => 5
)
[5] => stdClass Object
(
[rating_item_entry_id] => 6
)
[6] => stdClass Object
(
[rating_item_entry_id] => 7
)
[7] => stdClass Object
(
[rating_item_entry_id] => 8
)
)
[1] => Array
(
[0] => stdClass Object
(
[rating_item_entry_id] => 10
)
)
[2] => Array
(
[0] => stdClass Object
(
[rating_item_entry_id] => 11
)
)
[3] => Array
(
[0] => stdClass Object
(
[rating_item_entry_id] => 14
)
)
)
How can I place all the final rating_item_entry_id values in a single array?
Have tried with following code but I am bit confused and its not working.
foreach($results2Array as $result2) {
foreach($result2 as $res2) {
echo $res2['rating_item_entry_id'];
$ratingentries[] = $res2['rating_item_entry_id'];
}
}
Try this:
foreach ($postList as $curpostid) {
$sql2 = "SELECT * FROM wp_mr_rating_item_entry WHERE post_id='".$curpostid."'"; //change here
$results2 = $GLOBALS['wpdb']->get_results($sql2) or die(mysql_error());
}
First array:
[VariationSpecificsSet] => SimpleXMLElement Object
(
[NameValueList] => Array
(
[0] => SimpleXMLElement Object
(
[Name] => Size
[Value] => Array
(
[0] => 5FT King Size
[1] => 4FT6 Double
)
)
[1] => SimpleXMLElement Object
(
[Name] => Main Colour
[Value] => Array
(
[0] => Brown
[1] => Black
)
)
)
)
Second Array:
[Variation] => SimpleXMLElement Object
(
[StartPrice] => 14.99
[Quantity] => 12
[VariationSpecifics] => SimpleXMLElement Object
(
[NameValueList] => SimpleXMLElement Object
(
[Name] => Size
[Value] => No.10-1M
)
)
)
examine above two arrays
i want to store value NameValueList in database but the problem is sometimes it is SimpleXMLElement Object and sometimes it is Array
how can i store them ...??
You can detect is by is_array().
$myVal=$test['NameValueList'];
if(is_array($myVal) && count($myVal)>0){
foreach($myVal as $item){
echo $item->Name.":".echo $item->Value;
}
} else {
echo $myVal->Name.":".echo $myVal->Value;
}
Did you tried using json_encode like below.
You can convert the object to array.
$array=json_decode(json_encode($object),true);
This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 7 years ago.
I am trying to sort vote-list in PHP.
The list is an array, containing class-objects:
Array
(
[0] => VotedSong Object
(
[title] => SimpleXMLElement Object
(
[0] => bbh - bghs dsdw
)
[votes] => SimpleXMLElement Object
(
[0] => 6
)
[key] => SimpleXMLElement Object
(
[0] => bbh--0
)
)
[1] => VotedSong Object
(
[title] => SimpleXMLElement Object
(
[0] => aaa - bbb
)
[votes] => SimpleXMLElement Object
(
[0] => 4
)
[key] => SimpleXMLElement Object
(
[0] => aaa--0
)
)
[2] => VotedSong Object
(
[title] => SimpleXMLElement Object
(
[0] => wdewv - qwdqs
)
[votes] => SimpleXMLElement Object
(
[0] => 3
)
[key] => SimpleXMLElement Object
(
[0] => wdewv--0
)
)
[3] => VotedSong Object
(
[title] => SimpleXMLElement Object
(
[0] => Hsg and fdSv - aGamaama
)
[votes] => SimpleXMLElement Object
(
[0] => 2
)
[key] => SimpleXMLElement Object
(
[0] => hsgandfdsv--0
)
)
)
I managed to sort there by the ->key wich is working fine:
usort($votedsongs, function ($a, $b) { return $b->votes - $a->votes; });
But after this, I still need another sort-function to sort those songs that have the same amout of votes by ->title.
I already found some solutions that deal with problems alike, but those did not work for me.
Any ideas on this?
Sounds like you are wanting to sort the VotedSong objects in the array by votes and then by title (which is misspelled as titel). If so, this could work:
usort($votedsongs, function ($a, $b) {
if ($b->votes == $a->votes) {
return ($a->title < $b->title) ? -1 : 1;
}
return $b->votes - $a->votes;
});
Thanks to splash58 for this solution:
if (!($r = $b->votes - $a->votes)) $r = strcmp($b->title, $a->title); return $r;
I modified the alphabetic sort to be non-casesensitive and switched $a->title and $b->title - that's it:
usort($votedsongs, function ($a, $b) {
if (!($r = $b->votes - $a->votes)) $r = strcasecmp($b->title, $a->title); return $r;
});
i have array in this way
Array
(
[0] => stdClass Object
(
[qa_verified] => 0
)
[1] => stdClass Object
(
[qa_verified] => 1
)
[2] => stdClass Object
(
[qa_verified] => 2
)
)
i need to change into
Array
(
[0] => stdClass Object
(
[qa_verified] => invalidate
)
[1] => stdClass Object
(
[qa_verified] => approve
)
[2] => stdClass Object
(
[qa_verified] => reject
)
)
i have to change the value of qa_verified key depending on the status
0 = invalidate, 1= approve, 2=reject
i tried on array_walk, but unable to get result
any one help me on this
$lookup = array('invalidate', 'approve', 'reject');
array_walk(
$myArray,
function(&$entry) use ($lookup) {
$entry->qa_verified = $lookup[$entry->qa_verified];
}
);
var_dump($myArray);