How to compare two different multidimensional array? - php

I have two array as below
Array 1
Array
(
[0] => Array
(
[ps_id] => 5
[product_id] => 2
[supplier_id] => 25
[cost] => 789.00
[name] => Mahesh
)
[1] => Array
(
[ps_id] => 6
[product_id] => 2
[supplier_id] => 2
[cost] => 12345.00
[name] => mayank
)
[2] => Array
(
[ps_id] => 7
[product_id] => 2
[supplier_id] => 1
[cost] => 123456.00
[name] => abc
)
[3] => Array
(
[ps_id] => 10
[product_id] => 2
[supplier_id] => 8
[cost] => 12000.00
[name] => mayank1
)
)
Array 2
Array
(
[0] => Array
(
[suppliers] => Mahesh
[suppliers_cost] => 789.00
)
[1] => Array
(
[suppliers] => mayank
[suppliers_cost] => 12345.00
)
[2] => Array
(
[suppliers] => mayank1
[suppliers_cost] => 12000.00
)
[3] => Array
(
[suppliers] => testtetstet
[suppliers_cost] => 123123
)
)
I want to compare above array by their suppliers and name key,
Means if this both key have same values than it will store into one new array and if those key are not match then they will store new different array.
Or might be it possible that both array will have different number of keys
I had tried like below
foreach ($existsProductSupplier as $key => $value) {
if (isset($supplier_data[$key])) {
}else{
$supplier_data[$key]['suppliers']='';
$supplier_data[$key]['suppliers_cost']='';
}
}
foreach ($supplier_data as $key => $value) {
if(in_array($value['suppliers_cost'],$existsProductSupplier[$key])){
//echo "string";
// print_r($value);
}else{
echo "string";
//print_r($value);
}
}

try this code it will help you
<?php
$arr1=Array
(
0 => Array
(
"ps_id" => 5,
"product_id" => 2,
"supplier_id" => 25,
"cost" => 789.00,
"name" => "Mahesh"
),
1 => Array
(
"ps_id" => 6,
"product_id" => 2,
"supplier_id" => 2,
"cost" => 12345.00,
"name" => "mayank"
),
2 => Array
(
"ps_id" => 7,
"product_id" => 2,
"supplier_id" => 1,
"cost" => 123456.00,
"name" => "abc"
),
3 => Array
(
"ps_id" => 10,
"product_id" => 2,
"supplier_id" => 8,
"cost" => 12000.00,
"name" => "mayank1"
),
4 => Array
(
"ps_id" => 10,
"product_id" => 2,
"supplier_id" => 8,
"cost" => 12000.00,
"name" => "mayank2"
)
);
$arr2=Array
(
0 => Array
(
"suppliers" => "Mahesh",
"suppliers_cost" => 789.00
),
1 => Array
(
"suppliers" => "mayank",
"suppliers_cost" => 12345.00
),
2 => Array
(
"suppliers" => "mayank1",
"suppliers_cost" => 12000.00
),
3 => Array
(
"suppliers" => "testtetstet",
"suppliers_cost" => 123123
)
);
foreach($arr1 as $key=>$value){
if(isset($arr2[$key])){
if($value['name']==$arr2[$key]['suppliers']){
$arrnew1[]=$value['name'];
}else{
$arrnew2[]=$value['name'];
}
}else{
$arrnew2[]=$value['name'];
}
}
print_r($arrnew1);
print_r($arrnew2);

Related

merge two array with loop and key name

i have two separate arrays $first_one:
Array
(
[0] => Array
(
[_date] => 2019-10-16
[_number] => 1
[_order] => 1
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[_date] => 2020-10-11
[_number] => 2
[_order] => 2
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)
and the $second_array:
Array
(
[0] => Array
(
[date] => 2019-10-16
[number] => 1
[order] => 1
[name] => jack
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array
(
[date] => 2020-10-11
[number] => 2
[order] => 2
[name] => joey
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1433
)
)
[2] => Array
(
[date] => 2020-10-28
[number] => 3
[order] => 3
[name] => tom
[last_name] => foobar
[other_ids] => Array
(
[b_id] => 1593
)
)
)
they are very similar but they came from different api's and they are different in numbers and also some key names.
so what i'm trying to do is to count the $first_one arrays and if for that many loop through the $second_one (in this example is 2) and if the [_number] [_order] from $first_one was equal (==) to [number] [number] $second_one then take some info (for example the last name) from it and put in a new array.
so is this possible to do?
$arr1 = [ [ '_date' => '2019-10-16','_number' => 1,'_order' => 1,
'name' => 'jack','other_ids' => ['b_id' => 1253]
],
['_date' => 2020-10-11,'_number' => 2,'_order' => 2,
'name' => 'joey','other_ids' => ['b_id' => 1433]
]
];
$arr2 = [ [ 'date' => '2019-10-16','number' => '1','order' => '1',
'name' => 'jack','last_name' => 'foobar','other_ids' => ['b_id' => 1253]
],
[ 'date' => '2019-10-11','number' => '2','order' => '2',
'name' => 'joey','last_name' => 'foobar','other_ids' => ['b_id' => 1433]
],
[ 'date' => '2019-10-28', 'number' => '3', 'order' => '3',
'name' => 'tom', 'last_name' => 'foobar', 'other_ids' => ['b_id' => 1593]
],
];
// first make second array more directly searchable
// make new array with the `number` as the key
foreach( $arr2 as $a){
$arr2new[$a['number']] = $a;
}
foreach ($arr1 as $a) {
if ( array_key_exists($a['_number'], $arr2new) &&
$a['_order'] == $arr2new[$a['_order']]['order'] )
{
$merged[] = ['name'=>$a['name'], 'other_ids' => $a['other_ids']];
}
}
print_r($merged);
RESULT
Array
(
[0] => Array (
[name] => jack
[other_ids] => Array
(
[b_id] => 1253
)
)
[1] => Array (
[name] => joey
[other_ids] => Array
(
[b_id] => 1433
)
)
)

Remove parent array including the children if the children value found empty

I have this array structure.
I'm trying to remove those whole array if I found the [empevalpptwo] empty or null, so if the [empevalpptwo] is empty the whole [4] => Array should be remove.
Right now i'm just trying to get the parent index so I can just remove it by index
Is there any good solution like filtering recursively?
$kra = array_column($ppform_plan->toArray(), 'kra');
$emp_eval_pptwo = array_search('', array_column($kra, 'empevalpptwo'));
//should return 4
Array
(
[4] => Array
(
[id] => 50
[user_id] => 6282
[specific_user_id] => 6281
[eval_cat_id] => 2
[format_cat] => 1
[title] => This istesting
[desc] =>
[weight] => 50
[bsc_weight_group] =>
[bsc_rating] =>
[sequence] =>
[created_at] => 2019-05-22 10:55:23
[updated_at] => 2019-05-22 10:55:23
[kra] => Array
(
[0] => Array
(
[id] => 77
[user_id] => 6282
[bsc_id] => 50
[index] => 0
[kra_title] => ttes lang muna
[kra_desc] =>
[kra_weight] => 25
[sat] => 521
[at] => 4
[ot] => 535
[rating_per_kra] =>
[rating_per_kra_cat] =>
[net_weighting] => 5
[rank] => 1
[remarks] =>
[indicator_text] =>
[created_at] =>
[updated_at] =>
[empevalpptwo] =>
)
[1] => Array
(
[id] => 78
[user_id] => 6282
[bsc_id] => 50
[index] => 1
[kra_title] => talga e2 pa o
[kra_desc] =>
[kra_weight] => 25
[sat] => 5
[at] => 2
[ot] => 4
[rating_per_kra] =>
[rating_per_kra_cat] =>
[net_weighting] => 4
[rank] => 2
[remarks] =>
[indicator_text] =>
[created_at] =>
[updated_at] =>
[empevalpptwo] =>
)
)
)
)
Something akin to:
<?php
$items =
[
1 =>
[
'foo'=>
[
[
'bar' => '',
],
[
'bar' => '',
]
]
],
2 =>
[
'foo'=>
[
[
'bar' => 'baz'
]
]
]
];
foreach ($items as $k => $item)
if(empty(array_filter(array_column($item['foo'], 'bar'))))
unset($items[$k]);
var_export($items);
Output:
array (
2 =>
array (
'foo' =>
array (
0 =>
array (
'bar' => 'baz',
),
),
),
)
You should try this :
$data = array_map('array_filter', $data);
$data = array_filter($data);
UPDATED
and for checking inner values you can use this :
$array = array_filter($array, function($v) { return !empty($v['empevalpptwo']); });
Orginal
Array
(
[con] => Array
(
[address] => Array
(
[city] => ahmedabad
)
[personalinfo] =>
)
)
Result :
Array
(
[con] => Array
(
[address] => Array
(
[city] => ahmedabad
)
)
)

PHP: sorting multiple arrays by common value

I have multiple arrays that have a different structure but they all got a column named "round" and "eventnumber". How do I merge them all into one array where they are ordered by an array with the round and then listed by the eventnumber? (I have made the arrays shorter than they really are)
$a =
Array (
[0] => Array ( [id] => 1 [eventid] => 3 [round] => 1 [eventnumber] => 1 )
[1] => Array ( [id] => 2 [eventid] => 3 [round] => 2 [eventnumber] => 11 )
)
$b =
Array (
[0] => Array ( [id] => 1 [eventid] => 7 [round] => 1 [eventnumber] => 5 )
[1] => Array ( [id] => 2 [eventid] => 8 [round] => 1 [eventnumber] => 3 )
[2] => Array ( [id] => 3 [eventid] => 8 [round] => 2 [eventnumber] => 6 )
)
$c =
Array (
[0] => Array ( [id] => 1 [eventid] => 6 [round] => 2 [eventnumber] => 2 )
[1] => Array ( [id] => 2 [eventid] => 5 [round] => 1 [eventnumber] => 4 )
)
Desired Result:
$rounds =
Array (
[0] => Array ( [id] => 1 [eventid] => 3 [round] => 1 [eventnumber] => 1 )
[1] => Array ( [id] => 2 [eventid] => 8 [round] => 1 [eventnumber] => 3 )
[2] => Array ( [id] => 2 [eventid] => 5 [round] => 1 [eventnumber] => 4 )
[3] => Array ( [id] => 1 [eventid] => 7 [round] => 1 [eventnumber] => 5 )
)
Array (
[0] => Array ( [id] => 1 [eventid] => 6 [round] => 2 [eventnumber] => 2 )
[1] => Array ( [id] => 3 [eventid] => 8 [round] => 2 [eventnumber] => 6 )
[2] => Array ( [id] => 2 [eventid] => 3 [round] => 2 [eventnumber] => 11 )
)
I have looked for answers but cant seem to get this to work.
Thanks for taking the time to help :)
I have replaced your arrays to a,b and c to be simpler. I started by merging the arrays and then sorting them with a comparison function:
$a =
Array (
0 => Array ( "id" => 1, "eventid" => 3, "round" => 1, "eventnumber" => 1 ) ,
1 => Array ( "id" => 2, "eventid" => 3, "round" => 2, "eventnumber" => 11 )
);
$b =
Array (
0 => Array ( "id" => 1, "eventid" => 7, "round" => 1, "eventnumber" => 5 ),
1 => Array ( "id" => 2, "eventid" => 8, "round" => 1, "eventnumber" => 3 ),
2 => Array ( "id" => 3, "eventid" => 8, "round" => 2, "eventnumber" => 6 )
);
$c =
Array (
0 => Array ( "id" => 1, "eventid" => 6, "round" => 2, "eventnumber" => 2 ),
1 => Array ( "id" => 2, "eventid" => 5, "round" => 1, "eventnumber" => 4 )
);
$result = Array();
for ($i = 0; $i < 3; ++$i){
if (isset($a[$i])) {
array_push($result, $a[$i]);
}
if (isset($b[$i])) {
array_push($result, $b[$i]);
}
if (isset($c[$i])) {
array_push($result, $c[$i]);
}
}
function custom_sort($x,$y) {
if ($x['round'] == $y['round']){
return $x['eventnumber']>$y['eventnumber'];
}
return $x['round']>$y['round'];
}
usort($result, "custom_sort");
print_r($result);
To create a $rounds array that separates each round in a different array you can do:
$rounds = Array();
foreach($result as $row){
if (!isset($rounds[$row["round"]])){
$rounds[$row["round"]] = Array();
}
array_push($rounds[$row["round"]], $row);
}
print_r($rounds);
This naturally assumes the previous $results array. I didn't join all the code so that its clearer the separating part.

PHP sort subgroup only of a multidimensional array

not sure I'm missing something obvious, but I can't find in the other answers a way to sort a multidimensional array subgroups, without touching the main order.
I have an array like this
Array
(
[1] => Array
(
[name] => Apples
[type] => 1
[sales] => 10
)
[2] => Array
(
[name] => Apples
[type] => 2
[sales] => 30
)
[3] => Array
(
[name] => Apples
[type] => 3
[sales] => 20
)
[4] => Array
(
[name] => Oranges
[type] => 1
[sales] => 30
)
[5] => Array
(
[name] => Oranges
[type] => 2
[sales] => 10
)
[6] => Array
(
[name] => Oranges
[type] => 3
[sales] => 20
)
[7] => Array
(
[name] => Lemons
[type] => 1
[sales] => 10
)
[8] => Array
(
[name] => Lemons
[type] => 2
[sales] => 30
)
[9] => Array
(
[name] => Lemons
[type] => 3
[sales] => 20
)
)
What I need to do is sorting each 'name' group (Apples, Oranges and Lemons) by the key 'sales', leaving the order of keys 'name' Apples, Oranges and Lemons unaltered, so result should be like this:
Array
(
[1] => Array
(
[name] => Apples
[type] => 1
[sales] => 10
)
[2] => Array
(
[name] => Apples
[type] => 3
[sales] => 20
)
[3] => Array
(
[name] => Apples
[type] => 2
[sales] => 30
)
[4] => Array
(
[name] => Oranges
[type] => 2
[sales] => 10
)
[5] => Array
(
[name] => Oranges
[type] => 3
[sales] => 20
)
[6] => Array
(
[name] => Oranges
[type] => 1
[sales] => 30
)
[7] => Array
(
[name] => Lemons
[type] => 1
[sales] => 10
)
[8] => Array
(
[name] => Lemons
[type] => 3
[sales] => 20
)
[9] => Array
(
[name] => Lemons
[type] => 2
[sales] => 30
)
)
I can't find any way to do this, anyone can give me a hint about this?
Thanks!
#gege try this:
<?php
$arr = array(
array(
"name" => "Apples",
"type" => 1,
"sales" => 10
),
array(
"name" => "Apples",
"type" => 2,
"sales" => 30
),
array(
"name" => "Apples",
"type" => 3,
"sales" => 20
),
array(
"name" => "Oranges",
"type" => 1,
"sales" => 30
),
array(
"name" => "Oranges",
"type" => 2,
"sales" => 10
),
array(
"name" => "Oranges",
"type" => 3,
"sales" => 20
),
array(
"name" => "Lemons",
"type" => 1,
"sales" => 10
),
array(
"name" => "Lemons",
"type" => 2,
"sales" => 30
),
array(
"name" => "Lemons",
"type" => 3,
"sales" => 20
)
);
echo "<pre>";
print_r($arr); // array before
function sortArr($a, $b){
if($a["name"] == $b["name"]){
if($a["sales"] == $b["sales"]){
return 0;
}
return($a["sales"] < $b["sales"] ? -1 : 1);
}
else{
return($a["name"] < $b["name"] ? -1 : 1);
}
}
usort($arr, "sortArr");
echo "<pre>";
print_r($arr); // array after
For the sake of readability, I reduced your array (it should still work for yours though).
<?php
// The array structured as assumed according to your example
$groceries = array(
array(
"name" => "Apples",
"type" => 1,
"sales" => 10
),
array(
"name" => "Apples",
"type" => 2,
"sales" => 30
),
array(
"name" => "Apples",
"type" => 3,
"sales" => 20
),
);
/**
* See the PHP docs for more information: http://php.net/manual/en/function.usort.php
*/
$sortBySales = function ($a, $b){
return $a['sales'] - $b['sales'];
};
echo "<h1>Unsorted</h1>";
echo "<pre>";
var_dump($groceries);
echo "</pre>";
usort($groceries, $sortBySales);
echo "<h1>Sorted</h1>";
echo "<pre>";
var_dump($groceries);
echo "</pre>";
EDIT
I forgot that the array should have the names grouped as well. Now, #BunkerBoy alread posted the solution, but for the sake of completness, here is the updated sorting function:
$sortBySales = function ($a, $b){
if($a["name"] == $b["name"]){
return $a['sales'] - $b['sales'];
}else{
return($a["name"] < $b["name"] ? -1 : 1);
}
};
Note that the only difference to #BunkerBoy's function is that I reduced the comparison to a calculation.

PHP For Each Append Array

Example code array: https://eval.in/639002
A guest with (user_id = 1) bought multiple tickets (ticket_id = 1 & 2) On event (event_id = 11).
I am expecting the result format:
[ticket] => Array
(
[1] => Ticket Name, ***
[2] => Ticket Name
)
Code example below:
$data_db = array(
array(
'id' => 1,
'user_id' => 1,
'event_id' => 11,
'ticket_id' => 1,
'user_name' => 'guest 1'
),
array(
'id' => 2,
'user_id' => 2,
'event_id' => 11,
'ticket_id' => 1,
'user_name' => 'guest 2'
),
array(
'id' => 3,
'user_id' => 3,
'event_id' => 22,
'ticket_id' => 1,
'user_name' => 'guest 3'
),
array(
'id' => 4,
'user_id' => 1,
'event_id' => 11,
'ticket_id' => 2,
'user_name' => 'guest 1'
)
);
$output = [];
foreach ( $data_db as $key => $row ) {
$output[ $row['event_id'] ]['event'] = 'Event Name';
$output[ $row['event_id'] ]['attendee'][ $row['user_id'] ] = $row;
$output[ $row['event_id'] ]['attendee'][ $row['user_id'] ]['ticket'][ $row['ticket_id'] ] = 'Ticket Name';
}
print_r($output);
Current result
Array
(
[11] => Array
(
[event] => Event Name
[attendee] => Array
(
[1] => Array
(
[id] => 4
[user_id] => 1
[event_id] => 11
[ticket_id] => 2
[user_name] => guest 1
[ticket] => Array
(
[2] => Ticket Name
)
)
[2] => Array
(
[id] => 2
[user_id] => 2
[event_id] => 11
[ticket_id] => 1
[user_name] => guest 2
[ticket] => Array
(
[1] => Ticket Name ***
)
)
)
)
[22] => Array
(
[event] => Event Name
[attendee] => Array
(
[3] => Array
(
[id] => 3
[user_id] => 3
[event_id] => 22
[ticket_id] => 1
[user_name] => guest 3
[ticket] => Array
(
[1] => Ticket Name
)
)
)
)
)
First of you could start by sorting out an array of tickets of which ticket belongs to which user:
$output = [];
foreach($data_db as $key=>$row) {
// Define new $tickets Array E.g ['guest_name']=>['ticket_id']
$tickets[$row['user_name']][] = $row['ticket_id'];
$output [] = [
'id'=>$row['id'],
'user_id'=>$row['user_id'],
'event_id'=>$row['event_id'],
'user_name'=>$row['user_name'],
'tickets'=>[]
];
}
Your output of $tickets would look like:
Array
(
[guest 1] => Array
(
[0] => 1
[1] => 2
)
[guest 2] => Array
(
[0] => 1
)
[guest 3] => Array
(
[0] => 1
)
)
Then you can combine them, like this:
foreach($output as $k=>$v){
foreach($tickets as $guest=>$ticket){
if($v['user_name'] == $guest){
$output[$k]['tickets'] = $ticket;
}
}
}
Your output is:
Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[event_id] => 11
[user_name] => guest 1
[tickets] => Array
(
[0] => 1
[1] => 2
)
)
[1] => Array
(
[id] => 2
[user_id] => 2
[event_id] => 11
[user_name] => guest 2
[tickets] => Array
(
[0] => 1
)
........

Categories