Multidimensional array, find item and move to the top? - php

I'm trying to do some kind of function that will find (in the following array) the object with the id of 2, and move it to the top of the array. Here's the original array:
Array
(
[0] => stdClass Object
(
[id] => 177
[startdate] => 2014-08-02
)
[1] => stdClass Object
(
[id] => 178
[startdate] => 2014-08-02
)
[2] => stdClass Object
(
[id] => 2
[startdate] => 2014-07-28
)
[3] => stdClass Object
(
[id] => 82
[startdate] => 2014-07-28
)
[4] => stdClass Object
(
[id] => 199
[startdate] => 2013-10-10
)
)
And here is what I'd like it to output (with the moved array item):
Array
(
[0] => stdClass Object
(
[id] => 2
[startdate] => 2014-07-28
)
[1] => stdClass Object
(
[id] => 177
[startdate] => 2014-08-02
)
[2] => stdClass Object
(
[id] => 178
[startdate] => 2014-08-02
)
[3] => stdClass Object
(
[id] => 82
[startdate] => 2014-07-28
)
[4] => stdClass Object
(
[id] => 199
[startdate] => 2013-10-10
)
)
Any help would be appreciated.

function customShift($array, $id){
foreach($array as $key => $val){ // loop all elements
if($val->id == $id){ // check for id $id
unset($array[$key]); // unset the $array with id $id
array_unshift($array, $val); // unshift the array with $val to push in the beginning of array
return $array; // return new $array
}
}
}
print_r(customShift($data, 2));

Related

usort not working for laravel multidimensional arrays

I have an array
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[1] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[2] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
)
)
I want to rearrange this array by status desc order
I try
usort($usersdetailsA, function($a, $b) {
return $a->status <=> $b->status;
});
I got an error like
usort() expects parameter 1 to be array, object given
I tried
$usersdetailsA = $this->$usersdetailsA->getValues();
the i got an error like
Undefined property:
TCG\Voyager\Http\Controllers\Users::$[{"id":79,"name":"shelin","status":0},{"id":80,"name":"shanu","status":"2"},{"id":81,"name":"linto","status":"2"},{"id":82,"name":"joseph","status":0}]
Expected output
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[id] => 80
[name] => shanu
[status] => 2
)
[1] => stdClass Object
(
[id] => 81
[name] => linto
[status] => 2
)
[2] => stdClass Object
(
[id] => 79
[name] => shelin
[status] => 0
)
[3] => stdClass Object
(
[id] => 82
[name] => joseph
[status] => 0
)
Any help would be appreciated.Thanks in advance
You can sort a collection with sort methods by a given key:
$sorted = $collection->sortByDesc('status');

Replace the key in the array in php codeigniter

I have a string which i am spitting using explode functionality. Code Snippet is shown below
$string = "2017167637/ 43/ 42/ 38/ 46/ 41/ 40/ 39";
$tags = (explode("/",$string));
print_r($tags);
foreach ($tags as $key)
$invoicedata[] = (object)$key;
echo '<pre>'; print_r($invoicedata); exit;
The Output what i am getting right now is
Array ( [0] => 2017167637 [1] => 43 [2] => 42 [3] => 38 [4] => 46 [5] => 41 [6] => 40 [7] => 39 )
Array
(
[0] => stdClass Object
(
[scalar] => 2017167637
)
[1] => stdClass Object
(
[scalar] => 43
)
[2] => stdClass Object
(
[scalar] => 42
)
[3] => stdClass Object
(
[scalar] => 38
)
[4] => stdClass Object
(
[scalar] => 46
)
[5] => stdClass Object
(
[scalar] => 41
)
[6] => stdClass Object
(
[scalar] => 40
)
[7] => stdClass Object
(
[scalar] => 39
)
But the actual output what i need is in place of [scalar] i need [invoice]. How to do it? Any help appreciated.
Actual output what i need is shown below
Array ( [0] => 2017167637 [1] => 43 [2] => 42 [3] => 38 [4] => 46 [5] => 41 [6] => 40 [7] => 39 )
Array
(
[0] => stdClass Object
(
[invoice] => 2017167637
)
[1] => stdClass Object
(
[invoice] => 43
)
[2] => stdClass Object
(
[invoice] => 42
)
[3] => stdClass Object
(
[invoice] => 38
)
[4] => stdClass Object
(
[invoice] => 46
)
[5] => stdClass Object
(
[invoice] => 41
)
[6] => stdClass Object
(
[invoice] => 40
)
[7] => stdClass Object
(
[invoice] => 39
)
)
I tried with adding $invoice['invoice'] but it did not worked so how to get the desired output any help appreciated.
Here is working code. Try this.
$string = "2017167637/ 43/ 42/ 38/ 46/ 41/ 40/ 39";
$tags = (explode("/",$string));
print_r($tags);
foreach ($tags as $key=>$value)
{
$invoicedata[$key] = new stdClass();
$invoicedata[$key]->invoice = $value;
}
echo '<pre>'; print_r($invoicedata); exit;

merge two multidimensional array based on same ID

I am facing one issue when merging two multidimensional arrays based on the same ID.
In the example below I created two arrays - Array1 and Array2. Both arrays contain objects that have an ID property. Based on the ID property, the arrays should be merged and get the result array:
Array1
Array
(
[0] => stdClass Object
(
[claimtotal] =>
[total] => 4
[ID] => 3
)
[1] => stdClass Object
(
[claimtotal] => 20
[total] => 1
[ID] => 4
)
)
Array2
Array
(
[0] => stdClass Object
(
[ID] =>2
[name] => test1
)
[1] => stdClass Object
(
[ID] => 3
[name] => test2
)
[2] => stdClass Object
(
[ID] =>4
[name] => test3
)
[3] => stdClass Object
(
[ID] => 5
[name] => test4
)
)
Result_array
Array
(
[0] => stdClass Object
(
[ID] =>2
[name] => test1
[claimtotal] =>
[total] =>
)
[1] => stdClass Object
(
[ID] => 3
[name] => test2
[claimtotal] =>
[total] => 4
)
[2] => stdClass Object
(
[ID] =>4
[name] => test3
[claimtotal] => 20
[total] => 1
)
[3] => stdClass Object
(
[ID] => 5
[name] => test4
[claimtotal] =>
[total] =>
)
)
How can I achieve this?
if these are simple objects without methods go:
foreach($firstArray as $key => $firstObject){
foreach($secondArray as $secondObject){
if($firstObject['id'] === $secondObject['id']){
$firstArray[$key] = (object) array_merge((array) $firstObject, (array) $secondObject);
}
}
}
looks messy but does the job without introducing another loop to go through object properties.

Pull data from multidimension array if a string value is defined

I want to pull the string value of [totalPrice] , [boardType] , [roomCategory] if the [hotelCode] value is known.
The array print_r is (there only one hotel,)
Array (
[0] => stdClass Object (
[processId] => H5-84752260
[hotelCode] => GRSCDS
[availabilityStatus] => InstantConfirmation
[totalPrice] => 40 [totalTax] => 0
[totalSalePrice] => 0
[currency] => EUR
[boardType] => Room and Breakfast (American Buffet Breakfast)
[rooms] => Array (
[0] => stdClass Object (
[roomCategory] => Twin Room
[paxes] => Array (
[0] => stdClass Object (
[paxType] => Adult
[age] => 30 )
[1] => stdClass Object (
[paxType] => Adult
[age] => 30 ) )
[totalRoomRate] => 40
[ratesPerNight] => Array (
[0] => stdClass Object (
[date] => 2015-03-11 [amount] => 40 ) ) ) ) ) )
You can iterate array, check item with if condition and grab values you need. Like this:
<?php
foreach ($array as $item) {
if ($item->hotelCode === 'YOUR_CODE') {
// get your data here using $item->totalPrice, etc
break;
}
}

Append Object from one Object Array to Another Object Array

I have two Object arrays. "ID" from array 1 corresponds to the same as "media_id" in array 2 I need to add the "album_ids" of array 2 to array 1.
Object 1;
Array ( [0] => stdClass Object ( [ID] => 2482 [post_author] => 6 [post_date] => 2014-07-31 07:59:26) [1] => stdClass Object ( [ID] => 2483 [post_author] => 6 [post_date] => 2014-07-31 07:59:28)
Object 2=
Array ( [0] => stdClass Object ( [album_id] => 52 [media_id] => 2482 ) [1] => stdClass Object ( [album_id] => 92 [media_id] => 2483 )
I need the end result to be:
Array ( [0] => stdClass Object ( [ID] => 2482 [post_author] => 6 [post_date] => 2014-07-31 07:59:26 [album_id] => 52) [1] => stdClass Object ( [ID] => 2483 [post_author] => 6 [post_date] => 2014-07-31 07:59:28 [album_id] => 92)
Thank you for the help!!
As per your question, lets suppose you have two arrays of objects - array1 and array2.
aarry1 = Array( [0] => stdClass Object ( [ID] => 2482 [post_author] => 6 [post_date] => 2014-07-31 07:59:26) [1] => stdClass Object ( [ID] => 2483 [post_author] => 6 [post_date] => 2014-07-31 07:59:28)
array2= Array ( [0] => stdClass Object ( [album_id] => 52 [media_id] => 2482 ) [1] => stdClass Object ( [album_id] => 92 [media_id] => 2483 )
Now try out this code.
$albumids = array();
foreach ( $array2 as $key => $val) {
$albumids[$val->media_id] = $val->album_id;
}
if(!empty($albumids)) {
foreach ( $array1 as $key => $val) {
if(isset($albumids[$val->ID])) {
$val->album_id = $albumids[$val->ID];
}
}
}
print_r($array1);
You would get the expected result
Edit: I had to correct the following line from:
$val->albumid = $albumids[$val->id];
to
$val->album_id = $albumids[$val->ID];

Categories