Ranking array base on key php - php

I have an array of object "$arr" like this:
Array
(
[0] => stdClass Object
(
[1] => 1
)
[1] => stdClass Object
(
[0] => 1
)
[2] => stdClass Object
(
[4] => 2
)
[3] => stdClass Object
(
[3] => 2
)
[4] => stdClass Object
(
[2] => 3
)
)
What I want to do is rank the value based on the key. If key($arr[$idx+1]) < key($arr[$idx]) then increase value of $arr[idx] by 1. So the results would look like:
Array
(
[0] => stdClass Object
(
[1] => 2
)
[1] => stdClass Object
(
[0] => 1
)
[2] => stdClass Object
(
[4] => 5
)
[3] => stdClass Object
(
[3] => 4
)
[4] => stdClass Object
(
[2] => 3
)
)
this is what I have done so far:
$arrIdx2=[];
foreach($arrIdx as $key=>$value){
$newVal = new stdClass();
$newVal->$key = $value;
$arrIdx2[] = $newVal;
}
foreach($arrIdx2 as $key=>$value){
$i= key((array)$value);
$m = $value->$i; //get the first value
for($j = $key+1; $j< $len; $j++){
$i2 = key((array)$arrIdx2[$j]); //get the key of second value
$n = $arrIdx2[$j]->$i2;
if($n == $m){
if($i2 < $i){
$arrIdx2[$key]->$i += 1;
}
}
}
}
and this is the result I got:
Array
(
[0] => stdClass Object
(
[1] => 2
)
[1] => stdClass Object
(
[0] => 1
)
[2] => stdClass Object
(
[4] => 3
)
[3] => stdClass Object
(
[3] => 2
)
[4] => stdClass Object
(
[2] => 3
)
)
What's the best way to do this? Can anyone guide me? Thanks

Do this with associative array. I can't make an object array
right now. If you provide me the object array then i will update my answer.
Try this:
$arr = array(
array("1" => 1),
array("0" => 1),
array("4" => 2),
array("3" => 2),
array("2" => 3)
);
$out = array();
$first = key(current($arr));
$end = key(end($arr));
foreach($arr as $key => $val){
$comp = (key($val) == $end) ? $end : key($arr[$key+1]);
if($comp <= key($val))
$out[$key][key($val)] = key($val) + 1;
else
$out[$key][key($val)] = $val[key($val)];
}
echo '<pre>';
print_r($out);
Result
Array
(
[0] => Array
(
[1] => 2
)
[1] => Array
(
[0] => 1
)
[2] => Array
(
[4] => 5
)
[3] => Array
(
[3] => 4
)
[4] => Array
(
[2] => 3
)
)
I think you want this, let me know.

Related

How to merge two associative arrays

I have two associative arrays like
Array
(
[0] => Array
(
[0] => 2022-01-19
[1] => 6
)
[1] => Array
(
[0] => 2022-01-20
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-22
[1] => 2
)
)
and
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-23
[1] => 2
)
)
I need to merge them with the date and want a result array-like below
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 0
[2] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 0
[2] => 1
)
[2] => Array
(
[0] => 2022-01-19
[1] => 6
[2] => 0
)
[3] => Array
(
[0] => 2022-01-20
[1] => 1
[2] => 0
)
[4] => Array
(
[0] => 2022-01-21
[1] => 1
[2] => 1
)
[5] => Array
(
[0] => 2022-01-22
[1] => 2
[2] => 0
)
[6] => Array
(
[0] => 2022-01-23
[1] => 0
[2] => 2
)
)
I tried with the below code but not any success.
$final_array = [];
foreach($openTicket as $val){
$closeTicketNo = 0;
foreach($closeTicket as $value){
if($val[0] == $value[0]){
$closeTicketNo = $value[1];
}
}
$final_array[] = [$val[0],$val[1],$closeTicketNo];
}
I get all the elements from the $openTicket but not get all the elements from a $closeTicket to my result array $final_array
This code first finds all of the unique dates (using array_unique) from the first values in each array (array_column fetches the values and array_merge puts them into 1 array).
Then it indexes each array by the dates (using array_column again).
Finally looping through the unique dates and adding a new element to the output with the values (using ?? 0 so that if no value is present the array is still filled properly)...
$dates = array_unique(array_merge(array_column($openTicket, 0), array_column($closedTicket, 0)));
$open = array_column($openTicket, 1, 0);
$closed = array_column($closedTicket, 1, 0);
$finalArray = [];
foreach ($dates as $date) {
$finalArray[] = [$date, $open[$date] ?? 0, $closed[$date] ?? 0];
}
You can try like this
$array1 = [
['2022-01-19',6],
['2022-01-20',1],
['2022-01-21',0]
];
$array2 = [
['2022-01-17',6],
['2022-01-20',2],
['2022-01-21',1]
];
function mergeMultiple($array1,$array2){
foreach($array1 as $item1){
$mergedArray[$item1[0]] = $item1;
}
foreach($array2 as $item2){
if(isset($mergedArray[$item2[0]])){
array_push($mergedArray[$item2[0]],$item2[1]);
$mergedArray[$item2[0]] = $mergedArray[$item2[0]];
}else{
$mergedArray[$item2[0]] = $item2;
}
}
return array_values($mergedArray);
}
$mergedArray = mergeMultiple($array1,$array2);
ksort($mergedArray);
print_r($mergedArray);

Separated an array object in php into two new array object php based even and odd index

I have an array like this on php :
echo "<pre>";
print_r($header_item);
The result is :
Array
(
[0] => stdClass Object
(
[NO_INSPECTION] => 47
[ID_CONDITION] => 1
[NAMA_CONDITION] => DIRTY
)
[1] => stdClass Object
(
[NO_INSPECTION] => 47
[ID_CONDITION] => 2
[NAMA_CONDITION] => DAMAGE
)
[2] => stdClass Object
(
[NO_INSPECTION] =>
[ID_CONDITION] =>
[NAMA_CONDITION] => CLEAN
)
[3] => stdClass Object
(
[NO_INSPECTION] =>
[ID_CONDITION] =>
[NAMA_CONDITION] => OFF HIRE
)
)
I have case like this, this array will be separated into two new array object based even and odds index. So I write like this :
<?php
$odd = array();
$even = array();
$breakrow = 0;
foreach ($header_item as $v) {
if ($breakrow % 2 == 0) {
array_push($even, $v->NO_INSPECTION);
array_push($even, $v->ID_CONDITION);
array_push($even, $v->NAMA_CONDITION);
} else {
array_push($odd, $v->NO_INSPECTION);
array_push($odd, $v->ID_CONDITION);
array_push($odd, $v->NAMA_CONDITION);
}
$breakrow++;
}
?>
But I got those new arrays like this :
echo "<pre>";
print_r($even);
echo "<br>";
echo "<pre>";
print_r($odd);
Array
(
[0] => 47
[1] => 1
[2] => DIRTY
[3] =>
[4] =>
[5] => CLEAN
)
Array
(
[0] => 47
[1] => 2
[2] => DAMAGE
[3] =>
[4] =>
[5] => OFF HIRE
)
How can I make like this :
Array
(
[0] => stdClass Object
(
[NO_INSPECTION] => 47
[ID_CONDITION] => 1
[NAMA_CONDITION] => DIRTY
)
[1] => stdClass Object
(
[NO_INSPECTION] =>
[ID_CONDITION] =>
[NAMA_CONDITION] => CLEAN
)
AND :
Array
(
[0] => stdClass Object
(
[NO_INSPECTION] => 47
[ID_CONDITION] => 2
[NAMA_CONDITION] => DAMAGE
)
[1] => stdClass Object
(
[NO_INSPECTION] =>
[ID_CONDITION] =>
[NAMA_CONDITION] => OFF HIRE
)
You can achieve this using array_walk and a simple test of even vs. odd on the key.
$header_item = [
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven'
];
$odd = array();$even = array();
array_walk($header_item,function($value,$key) use(&$odd,&$even){
if ($key%2==0) {
$even[]=$value;
}
else{
$odd[]=$value;
}
});
print_r($even);
print_r($odd);
Will output
Array
(
[0] => zero
[1] => two
[2] => four
[3] => six
)
Array
(
[0] => one
[1] => three
[2] => five
[3] => seven
)
Try this one :-
$odd = array();
$even = array();
$breakrow = 0;
foreach ($header_item as $v) {
if ($breakrow % 2 == 0) {
$even[] = $v;
} else {
$odd[] = $v;
}
$breakrow++;
}
echo '<pre>'; print_r($even).'<br>';
echo '<pre>'; print_r($odd).'<br>';
output:-
Array
(
[0] => Array
(
[NO_INSPECTION] => 47
[ID_CONDITION] => 1
[NAMA_CONDITION] => DIRTY
)
[1] => Array
(
[NO_INSPECTION] =>
[ID_CONDITION] =>
[NAMA_CONDITION] => CLEAN
)
)
Array
(
[0] => Array
(
[NO_INSPECTION] => 47
[ID_CONDITION] => 2
[NAMA_CONDITION] => DAMAGE
)
[1] => Array
(
[NO_INSPECTION] =>
[ID_CONDITION] =>
[NAMA_CONDITION] => OFF HIRE
)
)

How can I merge one array with values into an array with stdClass objects?

I have two arrays and looking for the way to merge them. Standard array_merge() function don't work.
Do you know any nice solution without foreach iteration?
My first array:
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] =>
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] =>
)
)
My second array:
Array
(
[0] => 2
[1] => 7
)
And as a result I would like to get:*
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] => 7
)
)
This should work for you:
Just simply loop through both arrays with array_map() and pass the argument from array one as reference. Then you can simply assign the value to the count property.
<?php
array_map(function(&$v1, $v2){
$v1->count = $v2;
}, $arr1, $arr2);
print_r($arr1);
?>
output:
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] => 7
)
)
[akshay#localhost tmp]$ cat test.php
<?php
$first_array = array(
(object)array("field_value"=>"green","count"=>null),
(object)array("field_value"=>"yellow","count"=>null)
);
$second_array = array(2,7);
function simple_merge($arr1, $arr2)
{
return array_map(function($a,$b){ $a->count = $b; return $a; },$arr1,$arr2);
}
print_r($first_array);
print_r($second_array);
print_r(simple_merge($first_array,$second_array));
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[0] => stdClass Object
(
[field_value] => green
[count] =>
)
[1] => stdClass Object
(
[field_value] => yellow
[count] =>
)
)
Array
(
[0] => 2
[1] => 7
)
Array
(
[0] => stdClass Object
(
[field_value] => green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => yellow
[count] => 7
)
)
it is simple
code:
$i = 0;
foreach($firstarrays as $firstarr)
{
$firstarr['count'] = $secondarray[$i];
$i++;
}
Another option:
$a1 = Array(
(object) Array('field_value' => 'Green', 'count' => null),
(object) Array('field_value' => 'Yellow', 'count' => null)
);
$a2 = Array(2, 7);
for ($i=0; $i<sizeof($a1); $i++) {
$a1[$i]->count=$a2[$i];
}

array manipulation and rearranging

Here's my deal.
I have this array:
Array // called $data in my code
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
[2] => Array
(
[name] => quantity
[value] => 0
)
[3] => Array
(
[name] => var_id
[value] => 5
)
)
which I need it to be like:
Array // called $temp in my code
(
[0] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 4
)
)
[2] => Array
(
[0] => Array
(
[name] => quantity
[value] => 0
)
[1] => Array
(
[name] => var_id
[value] => 5
)
)
)
and I did it using this code I made:
$data = $_POST['data'];
$temp = array();
foreach($data as $key => $datum)
{
if($key%2 == 0)
{
$temp[$key] = array();
array_push($temp[$key], $datum, $data[$key+1]);
}
}
But I think that my code is some kinda stupid, specially if I have a huge data.
eventually what I want to do is just have each two indexes combined in one array, and I know that there should be something better than my code to do it, any suggestions?
Discover array_chunk()
$temp = array_chunk($data, 2);
$cnt = count($data);
$temp = array();
for ($i = 0; $i < $cnt; $i = $i + 2)
{
$temp[] = array($data[$i], $data[$i+1]);
}
Take a look at array_chunk.
<?php
$array = array(
array(1),
array(2),
array(3),
array(4),
);
print_r(
array_chunk($array, 2, false)
);
/*
Array
(
[0] => Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
)
[1] => Array
(
[0] => Array
(
[0] => 3
)
[1] => Array
(
[0] => 4
)
)
)
*/

Finding specific key and return value in a multidimentional array

Consider the array below:
//$allmembers
Array
(
[0] => Array
(
[id] => 7
[name] => John Smith
)
[1] => Array
(
[id] => 8
[name] => John Skeet
)
[2] => Array
(
[id] => 9
[name] => Chuck Norris
)
[3] => Array
(
[id] => 10
[name] => Bruce Lee
)
)
I have another array like this:
//$schedules
Array
(
[0] => Array
(
[id] => 24
[title] => DAMN DAMN DAMN!
[description] =>
[room] => 5022
[start] => 1362783300
[end] => 1362783300
[participants] => 7,8
[members] => Array
(
)
)
[1] => Array
(
[id] => 22
[title] => blah blah
[description] =>
[room] => 5022
[start] => 1365024780
[end] => 1365026280
[participants] => 9,10
[members] => Array
(
)
)
)
So I have to read the participants keys in the second array, and then find the name from first array and add it to the member of the second array.
I am trying the code below but I aint got any success so far:
$allmembers = $_DB->Query("SELECT id,name FROM members");
for($i = 0; $i < count($schedules); $i++)
{
$schedules[$i]["members"] = array() ;
$mems = array();
$mems = explode(',', $schedules[$i]["participants"]);
for($j = 0; $j < count($mems); $j++)
{
//How to search the first array?
}
}
given that the two arrays exist above this block as $schedules and $allmembers, the following should work.
foreach($schedules as &$event)
{
$participants = array_flip(explode(',', $event['participants']));
$addThese = array();
foreach($allmembers as $member)
{
if (isset($participants[$member['id']]))
$addThese[] = $member;
}
$event['participants'] = $addThese;
} unset($event);
print_r($schedules);

Categories