Convert multidimensional array into single array with key and values? - php

This is my array,
array
(
[0] => Array
(
[Invoice_id] => 1
[Customer_Name] => Abcd Ltd
[Order_Created] => 2018-02-07
[Order_Delivery_Date] => 2018-02-17
[State_Code] => 35
[CGST] => 212.5
[SGST] => 212.5
[IGST] => 0
[Total_Amount] => 8925
)
[1] => Array
(
[Invoice_id] => 2
[Customer_Name] => Johnson and Sons
[Order_Created] => 2018-02-07
[Order_Delivery_Date] => 2018-02-17
[State_Code] => 35
[CGST] => 2975
[SGST] => 2975
[IGST] => 0
[Total_Amount] => 124950
)
)
How to convert this array like below,
array
(
array("invoice_id" => "1", "customer_name" => "Abcd Ltd", "order_created" => 2018-02-07, "delivery_date" => 2018-02-17, "state_code" => 35, "cgst" =>212.5, "sgst" =>212.5, "igst" =>0, "total_amount" =>8925),
array("invoice_id" => "2", "customer_name" => "Johnson and Sons", "order_created" => 2018-02-07, "delivery_date" => 2018-02-17, "state_code" => 35, "cgst" =>2975, "sgst" =>2975, "igst" =>512.5, "total_amount" =>124950)
);

You already have it in the format you want, they are just in different ways of displaying arrays, except that your arrays are indexed by keys, if you really want to unindex it use this $newArray = array_values($array)

You could try this to change keys of your array :
foreach ($array as $k => $item) {
foreach ($item as $key => $value) {
unset($array[$k][$key]) ; // remove old key
$array[$k][strtolower($key)] = $value ; // add new one
}
}
Then "Invoice_id" keys will be "invoice_id", and so on.

Related

How can I filter arrayTwo based on values in arrayOne using PHP?

I'm sorry for not using the correct terminology. I'm very much a 'weekend warrior' when it comes to programming, but trying to get better.
I have two indexed arrays with associative arrays as the values. ArrayOne has a value for [uid_apps] that I want to use as a filter for ArrayTwo so I can create ArrayThree. The thrid array will only include array items from ArrayTwo that match the [uid_apps] value in ArrayOne.
I looked ar array_combine() and array_intersect(), but I didn't see a clear path to success. I also messed around with array_filter(), but wasn't able to make it work.
Below are samples of arrayOne, arrayTwo, and the desired arrayThree. Any help you can offer is greatly appreciated.
ArrayOne
[0] => Array
(
[uid_appMembership] => 3
[uid_apps] => 1
[uid_main] => 3
[privileges] => 555
)
[1] => Array
(
[uid_appMembership] => 4
[uid_apps] => 3
[uid_main] => 3
[privileges] => 555
)
ArrayTwo
[0] => Array
(
[uid_apps] => 1
[name_apps] => GHS Walk Through Evaluation
[site_apps] => ghs_001
[team_apps] => ghs_admin
[admin_uid] => 2
[dir_apps] => ghs_walk-through-evaluation
)
[1] => Array
(
[uid_apps] => 2
[name_apps] => CTE Work Based Learning Solution
[site_apps] => do_000
[team_apps] => do_cte
[admin_uid] => 3
[dir_apps] => do_cte-wbl
)
[2] => Array
(
[uid_apps] => 3
[name_apps] => GHS Parking Permit Solution
[site_apps] => ghs_001
[team_apps] => ghs_parking
[admin_uid] => 3
[dir_apps] => ghs_parking-permits
)
[3] => Array
(
[uid_apps] => 4
[name_apps] => GHS F-List
[site_apps] => ghs_001
[team_apps] => ghs_counseling
[admin_uid] => 3
[dir_apps] => ghs_flist
)
Desired ArrayThree I'd like to use arrayOne and arrayTwo to create this array.
[0] => Array
(
[uid_apps] => 1
[name_apps] => GHS Walk Through Evaluation
[site_apps] => ghs_001
[team_apps] => ghs_admin
[admin_uid] => 2
[dir_apps] => ghs_walk-through-evaluation
)
[1] => Array
(
[uid_apps] => 4
[name_apps] => GHS F-List
[site_apps] => ghs_001
[team_apps] => ghs_counseling
[admin_uid] => 3
[dir_apps] => ghs_flist
)
loop through array and check uid_apps value is available in other array or not if available than add it in third array.check using in array.
$arr1 = array(
array(
'uid_appMembership' => 3,
'uid_apps' => 1,
'uid_main' => 3,
'privileges' => 555
),
array(
'uid_appMembership' => 4,
'uid_apps' => 3,
'uid_main' => 3,
'privileges' => 555
)
);
$arr2 = array(
array(
'uid_apps' => 1,
'name_apps' => 'GHS Walk Through Evaluation',
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_admin',
'admin_uid' => 2,
'dir_apps' => 'ghs_walk-through-evaluation'
),array(
'uid_apps' => 2,
'name_apps' => 'CTE Work Based Learning Solution',
'site_apps' => 'do_000',
'team_apps' => 'do_cte',
'admin_uid' => 3,
'dir_apps' => 'do_cte-wbl'
),array(
'uid_apps' => 3,
'name_apps' => 'GHS Parking Permit Solution' ,
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_parking',
'admin_uid' => 3,
'dir_apps' => 'ghs_parking-permits'
),array(
'uid_apps' => 4,
'name_apps' => 'GHS F-List',
'site_apps' => 'ghs_001',
'team_apps' => 'ghs_counseling',
'admin_uid' => 3,
'dir_apps' => 'ghs_flist'
)
);
$arr3 = array();
foreach ($arr2 as $key => $value) {
$res = chk_val($arr1,$value['uid_apps']);
if($res == true){
array_push($arr3,$arr2[$key]);
}
}
function chk_val($arr,$val){
foreach ($arr as $key => $value) {
if(in_array($val,$value)){
return true;
}else{
return false;
}
}
}
Working example : http://phpfiddle.org/main/code/sdri-fbpk
here is how you can get desired array
$dataa = array();
foreach ($names as $key => $name) {
foreach($ips as $key2=>$ip){
if($name['uid_apps'] == $ip['uid_apps']){
$dataa[] = $name;
}
}
}
print_r($dataa);

How to Remove value from an array using another array with multiple values?

I have two array Array1 and Array2 and I need to remove Array2 value from Array1 I show my both the Array here.
In Array1 I have utype_id is 11 and 14 and I need to remove this id record from Array2 so how can I do it can you please help me?
Array1(
[0] => stdClass Object
(
[id] => 22
[accessid] => 2
[utype_id] => 11
[discount] => 3434
[published] => 1
)
[1] => stdClass Object
(
[id] => 23
[accessid] => 2
[utype_id] => 14
[discount] => 2
[published] => 1
)
)
Array2
(
[0] => stdClass Object
(
[id] => 9
[type_name] => Admin
[description] => admin
[published] => 0
)
[1] => stdClass Object
(
[id] => 10
[type_name] => Senior sales
[description] => senior sales
[published] => 0
)
[2] => stdClass Object
(
[id] => 11
[type_name] => junior sales
[description] => junior
[published] => 1
)
[3] => stdClass Object
(
[id] => 14
[type_name] => dealer
[description] => dealer
[published] => 0
)
[4] => stdClass Object
(
[id] => 15
[type_name] => fgdg
[description] => dfg
[published] => 1
)
[5] => stdClass Object
(
[id] => 16
[type_name] => fgdfg
[description] => fgdfg
[published] => 0
)
)
I didn't get any solution for this. I need only 9,10,15,16 Record id from Array2.
Just for entertainment purposes (and I was feeling a bit left out :( ). Index both arrays by the ID (need php 7+ for array_column() to support objects as input) and then array_diff_key() to remove any from the second array...
print_r(array_diff_key(array_column($array2, null, "id"),
array_column($array1, null, "utype_id")));
I would like to say that a foreach() solution is faster than this, just wanted to join in and post some original content.
First, extract utype_ids from first array, make them keys to speed up search:
$utype_ids = [];
foreach ($array1 as $item) {
$utype_ids[$item->utype_id] = 1;
}
Then, filter second array using $utype_ids:
$filtered_array = array_filter(
$array2,
function($v) use ($utype_ids) {
return !isset($utype_ids[$v->id]);
}
);
Demo: https://3v4l.org/i2heV
Use nested loops to perform the qualifying checks. Use a break as a matter of best practice to avoid unnecessary iterations.
Code: (Demo)
$blacklist = [
(object)["id" => 22,"accessid" => 2, "utype_id" => 11, "discount" => 3434, "published" => 1],
(object)["id" => 23,"accessid" => 2, "utype_id" => 14, "discount" => 2, "published" => 1]
];
$rows = [
(object)["id" => 9, "type_name" => "Admin", "description" => "admin", "published" => 0],
(object)["id" => 10, "type_name" => "Senior sales", "description" => "senior sales", "published" => 0],
(object)["id" => 11, "type_name" => "junior sales", "description" => "junior sales", "published" => 1],
(object)["id" => 14, "type_name" => "dealer", "description" => "dealer", "published" => 0],
(object)["id" => 15, "type_name" => "fgdg", "description" => "dfg", "published" => 1],
(object)["id" => 16, "type_name" => "fgdfg", "description" => "fgdfg", "published" => 0]
];
foreach ($blacklist as $disqualifier) { // iterate the blacklist
foreach ($rows as $index => $row) { // iterate the list to be checked
if ($row->id === $disqualifier->utype_id) { // if row should be disqualified
unset($rows[$index]); // remove the row
break; // stop checking the $rows for this $disqualifier
}
}
}
var_export($rows);
...if you need the output to be reindexed, you can call array_values($rows).
If these arrays of objects are coming from a database table, you should be improving your query to do this filtration process in advance.
You can use..
$arr1ids = array();
foreach($array1 as $val1){
$arr1ids[] = $val1->utype_id;
}
$resArr = array();
foreach($array2 as $val2){
if(!in_array($val2->utype_id,$arr1ids)){
$resArr[] = $val2;
}
}
print_r($resArr);

Transform multiple php arrays into one array

I need to create a set of arrays that look like this:
Array ([ID] => 55 [status] => u [resvdate] => 07/16/2018 [price] => 119.00 [source] => C)
Array ([ID] => 56 [status] => u [resvdate] => 07/17/2018 [price] => 119.00 [source] => C)
Array ([ID] => 57 [status] => u [resvdate] => 07/18/2018 [price] => 119.00 [source] => C)
from five arrays that look like this:
Array ( [resvdate1] => 07/16/2018 [resvdate2] => 07/17/2018 [resvdate3] => 07/18/2018 )
Array ( [resvdateid1] => 55 [resvdateid2] => 56 [resvdateid3] => 57 )
Array ( [resvprice1] => 119.00 [resvprice2] => 119.00 [resvprice3] => 119.00 )
Array ( [pricesource1] => C [pricesource2] => C [pricesource3] => C )
Array ( [rowstatus1] => u [rowstatus2] => u [rowstatus3] => u )
Do I just need to loop through each array and pick off the values or is there a more elegant way to do this?
Here's a go at it:
$data = [
[
"resvdate1" => "07/16/2018",
"resvdate2" => "07/17/2018",
"resvdate3" => "07/18/2018"
],
[
"resvdateid1" => 55,
"resvdateid2" => 56,
"resvdateid3" => 57
],
[
"resvprice1" => "119.00",
"resvprice2" => "119.00",
"resvprice3" => "119.00"
],
[
"pricesource1" => "C",
"pricesource2" => "C",
"pricesource3" => "C"
],
[
"rowstatus1" => "u",
"rowstatus2" => "u",
"rowstatus3" => "u"
]
];
$keys = [
"resvdate" => "resvdate",
"resvdateid" => "id",
"resvprice" => "price",
"pricesource" => "source",
"rowstatus" => "status"
];
$result = [];
foreach ($data as $a) {
$i = 0;
foreach ($a as $k => $v) {
$result[$i++][$keys[preg_replace("/\d/", "", $k)]] = $v;
}
}
print_r($result);
Output
Array
(
[0] => Array
(
[resvdate] => 07/16/2018
[id] => 55
[price] => 119.00
[source] => C
[status] => u
)
[1] => Array
(
[resvdate] => 07/17/2018
[id] => 56
[price] => 119.00
[source] => C
[status] => u
)
[2] => Array
(
[resvdate] => 07/18/2018
[id] => 57
[price] => 119.00
[source] => C
[status] => u
)
)
Explanation
This is basically a column to row mapping involving a little key adjustment along the way.

Find how many arrays with valid keys in multidimensional array in php

I need to know how many arrays have valid keys, how many arrays with valid keys in multidimensional array. Let me explain:
Input:
Array
(
[65] => Array
(
[1] => Array
(
[0] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 525
[trackposition] => 1
[tracklocation] => SIDE A
[tracknumber] => 1
[trackname] => I love u
)
[1] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 526
[trackposition] => 1
[tracklocation] => SIDE A
[tracknumber] => 2
[trackname] => Sun is yellow
)
)
[2] => Array
(
[0] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 527
[trackposition] => 2
[tracklocation] => SIDE B
[tracknumber] => 1
[trackname] => Car red
)
[1] => Array
(
[mediumid] => 65
[mediumname] => VINYL
[trackid] => 528
[trackposition] => 2
[tracklocation] => SIDE B
[tracknumber] => 2
[trackname] => Lady in red
)
)
)
[769] => Array
(
[] => Array
(
[0] => Array
(
[mediumid] => 769
[mediumname] => DVD
[trackid] =>
[trackposition] =>
[tracklocation] =>
[tracknumber] =>
[trackname] =>
)
)
)
)
The mediums[65] next array contains 2 valid keys (1 and 2). The mediums[769] next array contains no valid keys
Therefore only mediums[65] contains valid keys, so total of arrays with valid keys = 1.
I need to find that total. How ?
I've try using array_keys and array_filter, with no success (or either i'm doing it wrong)
PHP code demo
<?php
$array=Array
(
65 => Array
(
1 => Array
(
0 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 525,
"trackposition" => 1,
"tracklocation" => "SIDE A",
"tracknumber" => 1,
"trackname" => "I love u"
),
1 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 526,
"trackposition" => 1,
"tracklocation" => "SIDE A",
"tracknumber" => 2,
"trackname" =>"Sun is yellow"
)
),
2 => Array
(
0 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 527,
"trackposition" => 2,
"tracklocation" => "SIDE B",
"tracknumber" => 1,
"trackname" => "Car red"
),
1 => Array
(
"mediumid" => 65,
"mediumname" => "VINYL",
"trackid" => 528,
"trackposition" => 2,
"tracklocation" => "SIDE B",
"tracknumber" => 2,
"trackname" => "Lady in red"
)
)
),
769 => Array
(
"" => Array
(
0 => Array
(
"mediumid" => 769,
"mediumname" => "DVD",
"trackid" => "",
"trackposition" => "",
"tracklocation" => "",
"tracknumber" =>"",
"trackname" => ""
)
)
)
);
$counter=0;
$trackedNull=false;
foreach($array as $key => $value)
{
$keys=array_keys($array[$key]);
foreach($keys as $arraykey)
{
if($arraykey=="")
{
$trackedNull=true;
break;
}
}
if($trackedNull==true)
{
$trackedNull=false;
}
else
{
$counter++;
}
}
echo $counter;

Searching for a value inside an array and getting the array

I am trying to use an array to search for a value that is inside of an array and then take the full array that the value is in and add it to an array. Below is the array to get the value from:
Array (
[0] => Array ( [ID] => 138 [dimmer] => 5 [order] => 1 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[1] => Array ( [ID] => 139 [dimmer] => 6 [order] => 1 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[2] => Array ( [ID] => 140 [dimmer] => 7 [order] => 2 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
[3] => Array ( [ID] => 141 [dimmer] => 8 [order] => 2 [double] => 0 [location1] => DSR [location2] => Stage Pockets )
)
I am trying to get the value of dimmer with the search function below:
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, search($subarray, $key, $value));
}
return $results;
}
Below it uses the $chan value which is an integer to use the function above to search an array. The foreach is then supposed to go through the array that is $patch and select the arrays out of the array above (only returns an empty array), although it doesn't do that unless you change $patch_single['dimmer'] with a string such as "7".
$patch = search($patch, 'Channel', $chan);
foreach ($patch as $patch_single) {
print_r($patch_single);
$dim_single = intval($patch_single['dimmer']);
echo $dim_single;
$dimmers = search($dimmers, 'dimmer', $dim_single);
}
The array that is being used to get $patch_single['dimmer'] is, when inside the foreach:
Array ( [ID] => 241 [Channel] => 100 [dimmer] => 7 )
Array ( [ID] => 242 [Channel] => 100 [dimmer] => 25 )
Thank you for your advice.
Hm, i see that you have 2 dimensional array. Why you just don't use this?
foreach($array as $row) {
if ($row['dimmer'] == $myValue) { $newArray[] = $row; }
}
Try this :
$arr = Array (Array ( "ID" => 138, "dimmer" => 5, "order" => 1, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 139, "dimmer" => 6, "order" => 1, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 140, "dimmer" => 7, "order" => 2, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ),
Array ( "ID" => 141, "dimmer" => 8, "order" => 2, "double" => 0, "location1" => "DSR", "location2" => "Stage Pockets" ));
$arr = array_filter($arr, function($ar) {
return ($ar['dimmer'] == '7' );
});
echo "<pre>";
print_r($arr);
Output :
Array
(
[2] => Array
(
[ID] => 140
[dimmer] => 7
[order] => 2
[double] => 0
[location1] => DSR
[location2] => Stage Pockets
)
)
ref: http://php.net/manual/en/function.array-filter.php

Categories