compare two associative array and display the difference - php

I have the following Two array results coming from MySQL query result.
Array One (Orignal-Data):
Array
(
[w_a] => Array
(
[0] => Array
(
[cod] => CRR
[pr] => LL
[aid] => VM2254
[gender] => m
[title] =>
)
)
[w_a_ml] => Array
(
)
[w_a_ol] => Array
(
)
[w_a_rl] => Array
(
[0] => Array
(
[rol] => 1
)
)
)
Array Two (Changed-Data)
Array
(
[w_a] => Array
(
[0] => Array
(
[cod] => CRR
[pr] => LL
[aid] => VM2254
[gender] => f
[title] => Mr
)
)
[w_a_ml] => Array
(
[0] => Array
(
[wl] => 255
[care] => Sahan
[heigh] =>
[adam] =>
[instance] => Look
)
)
[w_a_ol] => Array
(
)
[w_a_rl] => Array
(
[0] => Array
(
[rol] => 1
)
)
)
What I wan to achieve from the above two array is to compare and show the result in table format or Inside Form To each Field (Original and New-Change) like below.
FiledsN Original New Change
title Empty Mr
Wl Empty 255
Care Empty Sahan
gender m f
instance Empty Look
I've tried making the array a single array using this function:
function array_single($arr) {
if (!is_array($arr)) {
return FALSE;
}
$res = array();
foreach ($arr as $keys => $values) {
if (is_array($values)) {
$res = array_merge($res, array_single($values));
} else {
$res[$keys] = $values;
}
}
return $res;
}
And then comparing using this:
function diff($new,$old) {
$del=array_diff_assoc($old,$new);
$add=array_diff_assoc($new,$old);
return $diff=array("old"=>$del, "new"=>$add);
}

Related

Find Key By Value In Array Inside Array Using Php

Hey Dear I am Making Referal System With 11 Level Of Referal So Ho Can I Show All Referals And There Level I Have Tried
Array
(
[L1] => Array
(
[0] => TL422632
[1] => TL626461
)
[L2] => Array
(
[0] => TL4321
[1] => TL191123
)
[L3] => Array
(
[0] => TL555938
)
[L4] => Array
(
[0] => TL197752
)
[L5] => Array
(
[0] => TL835309
)
[L6] => Array
(
[0] => TL495903
)
[L7] => Array
(
[0] => TL207447
)
[L8] => Array
(
[0] => TL427427
)
[L9] => Array
(
[0] => TL288884
)
[L10] => Array
(
[0] => TL251399
)
[L11] => Array
(
[0] => TL284394
)
)
But I Don't Know Ho To Get L Number By Value For Example I Have TL284394 And I Want To Know Level So It Will Show L11 How Can I Do It
you can use two foreach loop inside each other and array_search() like this:
$a = array("L1" => [ 0 => "TL422632" ],"L2"=> [ 0 => "TL422635" ]);
$x = "TL422635";
foreach($a as $key=>$value){
if($x === $value){
return $key;
}else{
foreach($value as $k=>$v){
if($x === $v){
return array_search([$k=>$v],$a);
}
}
}
}

Sequence issue with nested foreach loop php

I have two array. array one and array two. I want to merge these array into single array with key. My output result is valid but sequence is not correct.
Array one
Array
(
[0] => test-685f1e7bc357187e449479d627100102
[1] => test-685f1e7bc357187e449479d627d29390
)
Array two
Array
(
[0] => DF955298-A664-4FA7-9586-FCD4CF977777
[1] => DF955298-A664-4FA7-9586-FCD4CF988888
)
Expected Result
Array
(
[0] => Array
(
[key] => test-685f1e7bc357187e449479d627100102
[uuid] => DF955298-A664-4FA7-9586-FCD4CF977777
)
[1] => Array
(
[key] => test-685f1e7bc357187e449479d627d29390
[uuid] => DF955298-A664-4FA7-9586-FCD4CF988888
)
)
My code is for that result is mentioned below:
$record = array();
foreach ($keys_array as $key => $all_key) {
foreach ($uuid_array as $uuid_key => $all_uuid) {
$record[$key]['key'] = $all_key;
$record[$uuid_key]['uuid'] = $all_uuid;
}
}
My output sequence is not valid. Where is the problem
Array
(
[0] => Array
(
[key] => test-685f1e7bc357187e449479d627100102
[uuid] => DF955298-A664-4FA7-9586-FCD4CF977777
)
[1] => Array
(
[uuid] => test-685f1e7bc357187e449479d627d29390
[key] => DF955298-A664-4FA7-9586-FCD4CF988888
)
)
Simple solution:
$record = array();
foreach ($keys_array as $key => $all_key) {
$record[] = [
'key' => $all_key,
// get value under the same key from `$uuid_array`
'uuid' => $uuid_array[$key],
];
}

PHP How to restructure an array?

I have an array that I'd like to restructure. I want to group items by turn. I can figure out how to extract data from the array using foreach($arr['history'] as $obj) my issue is with populating a new array using a loop.
Currently it looks like this:
Array (
[history] => Array (
[id] => 23452435
[legend] => Array (
[0] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => foo
)
)
[1] => Array (
[player] => me
[turn] => 1
[card] => Array (
[name] => bar
)
)
[2] => Array (
[player] => opponent
[turn] => 1
[card] => Array (
[name] => derp
)
)
[3] => Array (
[player] => opponent
[turn] => 2
[card] => Array (
[name] => hoo
)
)
)
))
I want it to look like the following, but I can't figure out how to automatically create and populate this structure. This is an array with a sub-array for each turn, containing an array for me and opponent
Array (
[0] => Array (
[me] => Array (
[0] => foo
[1] => bar
)
[opponent] = Array (
[0] => derp
)
)
[1] => Array (
[me] => Array ()
[opponent] => Array (
[0] => hoo
)
))
Thanks.
Edit:
This is what I needed. Thanks for the answers.
$result = [];
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $list) {
$result[$list['turn']][$list['player']][] = $list['card']['name'];
}
}
Try this:
$result = [];
foreach ($data['history']['legend'] as $list) {
$result[$list['turn']-1][$list['player']][] = $list['card']['name'];
}
Fiddle it! http://ideone.com/BtKOKJ
You can just start adding data to the new array. PHP is extremely forgiving.
$historyByTurns = array();
foreach ($arr['history'] as $historyItem) {
foreach ($historyItem['legend'] as $legendItem) {
$turn = $legendItem['turn'];
$player = $legendItem['player'];
if (!array_key_exists($turn, $historyByTurns)) {
$historyByTurns[$turn] = array();
}
if (!array_key_exists($player, $historyByTurns[$turn])) {
$historyByTurns[$turn][$player] = array();
}
foreach ($legendItem as $card) {
$historyByTurns[$turn][$player][] = $card['name'];
}
}
}
You will have to test it, as I have no way to do that ATM.

Bulid for loop based on given array

I am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_nameā€¦
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}

How to combine inner array values in one multidimensional array?

Hi I have one multidimensional array, I retrieved the data using mysql PDO and formatted it this way
array('id'=>$row['empname'],'data'=>array(array($row['salestat'],$row['salecount'])))
Array
(
[0] => Array
(
[id] => 'employee1'
[data] => Array
(
0 => 'Sold'
1 => 1
)
)
[1] => Array
(
[id] => 'employee2'
[data] => Array
(
0 => 'On Process'
1 => 4
)
)
[2] => Array
(
[id] => 'employee2'
[data] => Array
(
0 => 'Sold'
1 => 2
)
)
)
I am trying to make my output like this, trying to format this so I can use it for drilldown series in highcharts, thank you
Array
(
[0] => Array
(
[id] => 'employee1'
[data] => Array
(
0 => 'Sold'
1 => 1
)
)
[1] => Array
(
[id] => 'employee2'
[data] => Array
[0]
(
0 => 'On Process'
1 => 4
)
[1]
(
0 => 'Sold'
1 => 2
)
)
)
Firstly, you can not use a field inside an array twice with the same key like salescount or salestat. But you can do something like this.
function wrapSameId( $employeeArray ) {
//create the new array.
$newEmployeeArray = array();
//loop the old array.
foreach ($employeeArray as $key => $value) {
$exists = 0;
$i=0;
$id = $value['id'];
$data = $value['data'];
//see if you can find the current id inside the newly created array if you do, only push the new data in.
foreach ($newEmployeeArray as $key2 => $value2) {
if( $id == $value2['id'] ) { $newEmployeeArray[$key2]['data'][] = $data;$exists = 1;var_dump($value2['data']); }
$i++;
}
//if we didnt find the id inside the newly created array, we push the id and the new data inside it now.
if( $exists == 0 ){
$newEmployeeArray[$i]['id'] = $id;
$newEmployeeArray[$i]['data'][] = $data;
}
}
return $newEmployeeArray;
}
This function should return a new Array but if in the old array there were multiple arrays with the same id, in this one we should have:
Array
(
[0] => Array
(
[id] => employee1
[data] => Array
(
[0] => Array
(
[salescount] => 4
[salesstat] => Sold
)
)
)
[1] => Array
(
[id] => employee2
[data] => Array
(
[0] => Array
(
[salescount] => 2
[salesstat] => In Progress
)
[1] => Array
(
[salescount] => 5
[salesstat] => Sold
)
)
)
)
You can use it like this:
$employees = wrapSameId( $PDOArray );
where $PDOArray is your initial Array. Hope this is what you were looking for.
Why not index your output array by id?
Loop over your initial array and add the salecount, salestat pair to an employee's data array.
$outputArray = array();
//Loop over each entry pulled from you database...
foreach ($employeeArray as $employeeInfo)
{
//If no index for the current employee exists, create an empty array
//The only reason I'm including this is because I'm not sure how array_merge handles nulls -- just to be safe
if (!$outputArray[$employeeInfo['id']])
{
$outputArray[$employeeInfo['id']] = array();
}
//Merge the existing array for that employee with the new data for the same employee
$outputArray[$employeeInfo['id']] = array_merge($outputArray[$employeeInfo['id']], $employeeInfo['data']);
}
print_r($outputArray);
It's easier to visualize the output...
Array
(
[employee1] => Array
(
0 => 1
1 => 'Sold'
)
[employee2] => Array
(
0 => 4
1 => 'On Process'
2 => 2
3 => 'Sold'
)
)
If you're not satisfied with your output being indexed by id, loop over it again and format it as you want.
$otherOutputArray = array();
foreach ($outputArray as $key => $value)
{
$otherOutputArray[] = array('id' => $key, 'data' => $value);
}
That gives you exactly what you want.

Categories