sort/arrange associative array by key [duplicate] - php

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);

Related

Php combine the values of two array objects

I have a oject that contains:and I want combine the content of [0][locationName and [1][locationName into another object/variable so the content would be "Weymouth and Bournemouth". I know there are similar questions on here but my keys in the array have the same name. How would this be done?
[destination] => stdClass Object
(
[location] => Array
(
[0] => stdClass Object
(
[locationName] => Weymouth
)
[1] => stdClass Object
(
[locationName] => Bournemouth
)
)
)
Many thanks in advance for your time.
Assuming that you have the reference to $destination as an object then perhaps you can try like this:
$arr=$destination->location;
$tmp=array();
foreach( $arr as $index => $obj )$tmp[]=$obj->locationName;
$tmp=implode(' and ',$tmp ); // do something with $tmp
Assuming I recreated the original object correctly
$obj=(object)array(
'destination' => (object)array(
'location' => array(
(object)array('locationName'=>'Weymouth'),
(object)array('locationName'=>'Bournemouth')
)
)
);
The above produces a structure like this:
[destination] => stdClass Object
(
[location] => Array
(
[0] => stdClass Object
(
[locationName] => Weymouth
)
[1] => stdClass Object
(
[locationName] => Bournemouth
)
)
)
$destination=$obj->destination;
pre( $obj );
$arr=$destination->location;
$tmp=array();
foreach( $arr as $index => $obj )$tmp[]=$obj->locationName;
$tmp=implode(' and ',$tmp ); // do something with $tmp
echo $tmp; // --> outputs Weymouth and Bournemouth

how to show multidimentional array result using foreach loop in php

Array
(
[result] => Array
(
[0] => stdClass Object
(
[uniq_id] => 00fdc23c151ad0044d60
)
[1] => stdClass Object
(
[uniq_id] => 590dde424e
)
[2] => stdClass Object
(
[uniq_id] => 6f0eb3bb34
)
[3] => stdClass Object
(
[uniq_id] => eeb6c63929
)
[4] => stdClass Object
(
[uniq_id] => a72034387e
)
}
}
But I want only multiple uniq_id one by one in foreach loop
like
00fdc23c151ad0044d60
590dde424e
6f0eb3bb34
eeb6c63929
a72034387e
Its a stdClass Object array, so try this:
foreach($yourArray['result'] as $data)
{
echo $date->uniq_id.'<br>';
}
Note: stdClass Object properties can be accessed using ->

convert multidimensional array to object in php [duplicate]

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));

php array multisort by inner arrays value [duplicate]

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;
});

change the value of an array in php

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);

Categories