Find how many arrays with valid keys in multidimensional array in php - 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;

Related

Convert multidimensional array into single array with key and values?

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.

Merging two multi-dimensional arrays using key and adding values

I want to merge two array's using key(product_id) and adding that values(usage).
Array 1
Array
(
[0] => Array
(
[name] => Reschedule A Service
[usage] => 1
[product_id] => 8
)
[1] => Array
(
[name] => Adding An Image
[usage] => 1
[product_id] => 5
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 1
[product_id] => 14
)
)
Array 2
Array
(
[0] => Array
(
[name] => Adding An Image
[usage] => 1
[product_id] => 5
)
[1] => Array
(
[name] => Schedule A Service
[usage] => 3
[product_id] => 11
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 2
[product_id] => 14
)
[3] => Array
(
[name] => Sales Performance Dashboard
[usage] => 2
[product_id] => 30
)
[4] => Array
(
[name] => Quote
[usage] => 1
[product_id] => 32
)
)
I need an out put like this merging and adding usage values.
Array
(
[0] => Array
(
[name] => Adding An Image
[usage] => 2
[product_id] => 5
)
[1] => Array
(
[name] => Schedule A Service
[usage] => 3
[product_id] => 11
)
[2] => Array
(
[name] => Each Calendar Event
[usage] => 3
[product_id] => 14
)
[3] => Array
(
[name] => Sales Performance Dashboard
[usage] => 2
[product_id] => 30
)
[4] => Array
(
[name] => Quote
[usage] => 1
[product_id] => 32
)
[5] => Array
(
[name] => Reschedule A Service
[usage] => 1
[product_id] => 8
)
)
This is my code for creating arrays
foreach($query->rows as $product){
$top_products[]=array(
'name'=>$product['name'],
'usage'=>$product['pusage'],
'product_id'=>$product['product_id']
);
}
foreach($query_2->rows as $product){
$top_point_products[]=array(
'name'=>$product['name'],
'usage'=>$product['p_usage'],
'product_id'=>$product['product_id']
);
}
$first =array(
array(
"name" => "Reschedule A Service",
"usage" => 1,
"product_id" => 8
),
array(
"name" => "Adding An Image",
"usage" => 1,
"product_id" => 5
),
array(
"name" => "Each Calendar Event",
"usage" => 1,
"product_id" => 14
)
);
$second =array(
array(
"name" => "Adding An Image",
"usage" => 1,
"product_id" => 5
),
array(
"name" => "Schedule A Service",
"usage" => 3,
"product_id" => 11
),
array(
"name" => "Each Calendar Event",
"usage" => 2,
"product_id" => 14
),
array(
"name" => "Sales Performance Dashboard",
"usage" => 2,
"product_id" => 30
),
array(
"name" => "Quote",
"usage" => 1,
"product_id" => 32
)
);
$result = array_unique(array_merge($first,$second), SORT_REGULAR);
Use array_unique & array_merge
Use the array_merge function, like this:
$C = array_merge($A, $B);
print_r($C);
Read manual Array merge
try this code
<?php
$array1=array
(
0 => array(
'name' => "Reschedule A Service",
'usage' => 1,
'product_id' => 8
),
1 => Array
(
'name' => "Adding An Image",
'usage' => 1,
'product_id' => 5
),
2 => Array
(
'name' => "Each Calendar Event",
'usage' => 2,
'product_id' => 14
)
);
$array2=array
(
0 => Array
(
'name' => "Adding An Image",
'usage' => 1,
'product_id' => 5
),
1 => Array
(
'name' => "Schedule A Service",
'usage' => 3,
'product_id' => 11
),
2 => Array
(
'name' => "Each Calendar Event",
'usage' => 5,
'product_id' => 14
),
3 => Array
(
'name' => "Sales Performance Dashboard",
'usage' => 2,
'product_id' => 30
),
4 => Array
(
'name' => "Quote",
'usage' => 1,
'product_id' => 32
)
);
$product_id1=array_column($array1, 'product_id');
$product_id2=array_column($array2, 'product_id');
$new=array_intersect($product_id1,$product_id2);
foreach ($new as $key => $value) {
if(in_array($new[$key],$product_id2)){
$array2[array_search($new[$key],$product_id2)]['usage']+=$array1[$key]['usage'];
}
}
$new1=array_diff($product_id1,$product_id2);
foreach ($new1 as $key => $value) {
$array2[]=$array1[$key];
}
foreach ($array2 as $key => $value) {
echo "[".$key."]=><br>";
foreach ($value as $key1 => $value1) {
echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
echo "[".$key1."]=>".$value1."<br>";
}
echo "<br>";
}
?>
output
[0]=>
[name]=>Adding An Image
[usage]=>2
[product_id]=>5
[1]=>
[name]=>Schedule A Service
[usage]=>3
[product_id]=>11
[2]=>
[name]=>Each Calendar Event
[usage]=>7
[product_id]=>14
[3]=>
[name]=>Sales Performance Dashboard
[usage]=>2
[product_id]=>30
[4]=>
[name]=>Quote
[usage]=>1
[product_id]=>32
[5]=>
[name]=>Reschedule A Service
[usage]=>1
[product_id]=>8
Use array_merge and a simple foreach loop to check your condition and update the usagevalues.
See below
$result = array_merge($arrArray1, $arrArray2);
$result2 = array();
foreach($result as $key => $value){
if(array_key_exists($value['product_id'], $result2)){
$result2[$value['product_id']]['usage'] += $value['usage'];
} else{
$result2[$value['product_id']] = $value;
}
}
print_r($result2);
If you want to reset your resultant array indexes use array_merge again like this
$result2 = array_merge($result2);
Hope this will help

Remove array keys that aren't listed in a second array

The following is my result array
Array (
[0] => Array
(
[ProductID] => 220
[TextID] => 477
[ProductName] => Hugo Woman
[Price] => 43.91
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/874229/300_0/Hugo_Boss-Hugo_Woman-EdP_30ml_GRATIS_Nail_Polish_4ml.jpg
[CategoryID] => 1
[SubCategoryID] => 1
[View] => 0
)
[1] => Array
(
[ProductID] => 616
[TextID] => 959
[ProductName] => Hugo XY
[Price] => 44.95
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/333660/300_0/Hugo_Boss-Hugo_XY.jpg
[CategoryID] => 2
[SubCategoryID] => 2
[View] => 0
)
[2] => Array
(
[ProductID] => 650
[TextID] => 991
[ProductName] => Hugo Just Different
[Price] => 45.76
[BTW] => 21
[Stock] => 500
[BrandID] => 186
[ProductImage] => https://media.douglas-shop.com/617162/300_0/Hugo_Boss-Hugo_Just_Different.jpg
[CategoryID] => 2
[SubCategoryID] => 2
[View] => 0
)
)
I have a second array with subcategories, in which the key is referencing to the SubCategoryID:
Array
(
[1] => Array
(
[EN] => Ladies
[NL] => Dames
)
[2] => Array
(
[EN] => Men
[NL] => Heren
)
)
I want to loop through the result array and remove the keys who don't have a SubCategoryID listed in the second array. I looked at http://php.net/manual/en/function.array-filter.php, but can't figure out the best way to do this.
Thank you!
There are two solutions to the above problem, one with using simple for loop and one with using array_walk() function.
Here, $result_array is your result array and $subcategory_array is your subcategory array.
Solution(1):
$subcategory_ids = array_keys($subcategory_array);
$arrLength = count($result_array);
for($i = 0; $i < $arrLength; ++$i){
if(!in_array($result_array[$i]['SubCategoryID'], $subcategory_ids)){
unset($result_array[$i]);
}
}
// display $result_array
echo "<pre>"; print_r($result_array);
Solution(2):
$subcategory_ids = array_keys($subcategory_array);
function filter_arr($item, $key){
global $result_array, $subcategory_ids;
if(!in_array($item['SubCategoryID'], $subcategory_ids)){
unset($result_array[$key]);
}
}
array_walk($result_array, "filter_arr");
// display $result_array
echo "<pre>"; print_r($result_array);
Please try i thnik this help to you..
$array = array (
0 => array
(
'ProductID' => 220,
'TextID' => 477,
'ProductName' => 'Hugo Woman',
'Price' => 43.91,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/874229/300_0/Hugo_Boss-Hugo_Woman-EdP_30ml_GRATIS_Nail_Polish_4ml.jpg',
'CategoryID' => 1,
'SubCategoryID' => 1,
'View' => 0
),
1 => array
(
'ProductID' => 616,
'TextID' => 959,
'ProductName' => 'Hugo XY',
'Price' => 44.95,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/333660/300_0/Hugo_Boss-Hugo_XY.jpg',
'CategoryID' => 1,
'SubCategoryID' => 2,
'View' => 0
),
'2' => array
(
'ProductID' => 650,
'TextID' => 991,
'ProductName' => 'Hugo Just Different',
'Price' => 45.76,
'BTW' => 21,
'Stock' => 500,
'BrandID' => 186,
'ProductImage' => 'https://media.douglas-shop.com/617162/300_0/Hugo_Boss-Hugo_Just_Different.jpg',
'CategoryID' => 2,
'SubCategoryID' => 1,
'View' => 0
),);
$array1 = array (
1 => array
(
'EN' => 'Ladies',
'NL' => 'Dames'
),
2 => array
(
'EN' => 'Men',
'NL' => 'Heren'
),);
foreach($array as $newArray){
if (array_key_exists($newArray['SubCategoryID'], $array1)) {
echo '<pre>';
print_r($newArray);
echo '</pre>';}}

Create new array depending on key

My array is like that:
Array
(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
)
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
[2] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[3] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
I want to change that array like that depending on key['type']. If array key['type'] are same, I want to change array like that:
Array
(
[car] => Array(
[0]=>Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
),
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
),
[train]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[cruise]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
)
)
what I mean is that if key['type'] is car, I want to create car array or if the type is train I want to create train array or if the type is cruise I want to create cruise array. I don't know how to loop the array. Anyone please help me. Thanks a lot!
Here's a simple way to do it: loop over the data, and just append to the subarray matching the type value:
// starting data
$starting_array = array (
0 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 1,
'tran_name' => 'private',
'tran_image' => '1251961905A1.jpg',
'type' => 'car',
'troute_id' => 10
),
1 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 2,
'tran_name' => 'express',
'tran_image' => 'bus3.jpg',
'type' => 'car',
'troute_id' => 13
),
2 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 3,
'tran_name' => 'MyanmarTrain',
'tran_image' => 'Burma-Gorteikviaduct.jpg',
'type' => 'train',
'troute_id' => 16
),
3 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 4,
'tran_name' => 'Ayeyarwaddy Cruise',
'tran_image' => 'boat-ChutzpahToo1.jpg',
'type' => 'cruise',
'troute_id' => 22
)
);
// initialize the result array
$result = array();
// loop over the starting array
foreach($starting_array as $entry) {
// make sure the result array has a key matching this item's type
if(!array_key_exists($entry['type'], $result)) {
$result[ $entry['type'] ] = array();
}
// add this item to the result array
$result[ $entry['type'] ][] = $entry;
}
// this is just for testing, so you can verify the output matches your desired result
echo "<pre>";
var_dump($result);
echo "</pre>";
Try this:
<?php
$tempArr = Array
(
Array(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 1,
"tran_name" => "private",
"tran_image" => "1251961905A1.jpg",
"type" => "car",
"troute_id" => 10
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 2,
"tran_name" => "express",
"tran_image" => "bus3.jpg",
"type" => "car",
"troute_id" => 13
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 3,
"tran_name" => "MyanmarTrain",
"tran_image" => "Burma-Gorteikviaduct.jpg",
"type" => "train",
"troute_id" => 16
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 4,
"tran_name" => "Ayeyarwaddy Cruise",
"tran_image" => "boat-ChutzpahToo1.jpg",
"type" => "cruise",
"troute_id" => 22
)
);
$resultArr = array();
foreach($tempArr as $tempKey=>$temp)
{
if(!array_key_exists($temp['type'], $resultArr))
{
$resultArr[$temp['type']] = array();
}
$resultArr[$temp['type']][] = $temp;
}
echo '<pre>';
print_r($resultArr);
?>
This is working fine .....

how to get result in single array?

HI,
this is my array coming in a variable
Array
(
[msg] => Array
(
[0] => Array
(
[alertId] => 2416
[alerttitle] => Raven Lexy
[alertImageUrl] => photos/81951b37ad01c4aa65662956f178214eth.jpeg
[alertDescription] => (1) New Message(s)
[alertType] => New Message
[Date] => 1304679217
[count] => 1
)
)
[rehp] => Array
(
[0] => Array
(
[alertId] => 48
[alerttitle] => Artin
[alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
[alertDescription] => Reply From Artin
[alertType] => Reply To Hotpress
[count] => 1
[id] => 48
)
[1] => Array
(
[alertId] => 48
[alerttitle] => Artin
[alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
[alertDescription] => Reply From Artin
[alertType] => Reply To Hotpress
[count] => 1
[id] => 48
)
i want to convert into
Array
(
[0] => Array
(
[alertId] => 2416
[alerttitle] => Raven Lexy
[alertImageUrl] => photos/81951b37ad01c4aa65662956f178214eth.jpeg
[alertDescription] => (1) New Message(s)
[alertType] => New Message
[Date] => 1304679217
[count] => 1
)
[1] => Array
(
[alertId] => 48
[alerttitle] => Artin
[alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
[alertDescription] => Reply From Artin
[alertType] => Reply To Hotpress
[count] => 1
[id] => 48
)
[2] => Array
(
[alertId] => 48
[alerttitle] => Artin
[alertImageUrl] => photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg
[alertDescription] => Reply From Artin
[alertType] => Reply To Hotpress
[count] => 1
[id] => 48
)
)
how can i use foreach loop/for loop to get the the result.
thanks
What about
$new_array = array_merge($orig["msg"],$orig["rehp"])
Simple foreach loop and concatenating the arrays:
$result = array();
foreach($array as $a) {
$result = array_merge($result, $a);
}
This works and has been tested:
$a = Array(
"msg" => Array
(
0 => Array
(
"alertId" => 2416,
"alerttitle" => "Raven Lexy",
"alertImageUrl" => "photos/81951b37ad01c4aa65662956f178214eth.jpeg",
"alertDescription" => "(1) New Message(s)",
"alertType" => "New Message",
"Date" => 1304679217,
"count" => 1
)
),
"rehp" => Array
(
0 => Array
(
"alertId" => 48,
"alerttitle" => "Artin",
"alertImageUrl" => "photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg",
"alertDescription" => "Reply From Artin",
"alertType" => "Reply To Hotpress",
"count" => 1,
"id" => 48
),
1 => Array
(
"alertId" => 48,
"alerttitle" => "Artin",
"alertImageUrl" => "photos/95eaf8416ee68981ab944465bcdd7bffth.jpeg",
"alertDescription" => "Reply From Artin",
"alertType" => "Reply To Hotpress",
"count" => 1,
"id" => 48,
)
)
);
$b = array();
foreach ($a as $v)
{
foreach ($v as $i)
$b[] = $i;
}
print_r($b);

Categories