php array count values in 4 deep array - php

Im trying to count how many times a delivery date is in my array but i seem to only be able to count the first level.
array (size=48)
'2000-01-01' =>
array (size=2)
'date' => string '2000-01-01' (length=10)
0 =>
array (size=2)
'van' => string '0' (length=1)
0 =>
array (size=619)
'drop' => string '0' (length=1)
0 =>
array (size=29)
'id' => string '18137' (length=5)
'order_number' => string '13550' (length=5)
'reference' => string '' (length=0)
'delivery_date' => string '2000-01-01' (length=10)
I've tried:
$counts = array_count_values(array_flip(array_column($output, 'delivery_date')));
and
$array = array_map(function($element){
return $element['delivery_date'];
}, $output);
$array2 = (array_count_values($array));
print_r($array2);
in the end i either end up with a array to string error or the value 1.
how Would i go about counting these?
Thanks.

You could make use of array_walk_recursive and increment an array value every time the delivery_date key is present in the array at any level:
$counts = [];
array_walk_recursive(
$output,
static function ($value, string $key) use (&$counts): void {
if ($key === 'delivery_date') {
$counts[$value] = ($counts[$value] ?? 0) + 1;
}
}
);

Related

String to Nested Array

Hi i would like to ask if there is any good way to make Nested Array from few strings like in example but when i add NEW STRING it should append
it looks like some kind of tree
String
TEXT1|||TEXT2|||TEXT3 ....
into
[TEXT1 => [TEXT2 => [TEXT3] ] ]
new String
TEXT1|||AAA222|||AAA333
mew array with old one
[TEXT1 => [TEXT2 => [TEXT3 => null], AAA222 => [AAA333 => null] ] ]
string is generated from this array indexes are levels in "tree"
array (size=5)
0 =>
array (size=2)
'a' => string 'Motoryzacja' (length=11)
'b' => string '' (length=0)
1 =>
array (size=2)
'a' => string 'Części samochodowe' (length=20)
'b' => string '' (length=0)
2 =>
array (size=2)
'a' => string 'Części karoserii' (length=18)
'b' => string '' (length=0)
3 =>
array (size=2)
'a' => string 'Błotniki' (length=9)
'b' => string '' (length=0)
4 =>
array (size=2)
'a' => string 'Maski' (length=5)
'b' => string '' (length=0)
This is what I came up with:
//recursive function to build the array
function buildArray(Array $input, $output=[]){
$len = count($input);
if ($len > 0){
$key = array_shift($input);
//if there is more in the array, then we need to continue building our array
if (($len - 1) > 0){
$output[$key] = buildArray($input,$output);
}
else {
$output[$key] = NULL;
}
}
return $output;
}
//converts string input with ||| delimiter into nested Array
function stringToArray(String $input){
$arr = explode('|||', $input);
$output = buildArray($arr);
return $output;
}
$arr = stringToArray("TEXT1|||TEXT2|||TEXT3");
$arr2 = stringToArray("TEXT1|||AAA222|||AAA333");
var_dump(array_merge_recursive($arr,$arr2));

Convert array index to custom value?

I have an array $indexedarray
printr($indexedarray) gives something like this
array (size=3)
0 => string 'Homes' (length=5)
1 => string 'Apartments' (length=10)
2 => string 'Villas' (length=6)
I want to change this arrays index also same as value, like
array (size=3)
'Homes' => string 'Homes' (length=5)
'Apartments' => string 'Apartments' (length=10)
'Villas' => string 'Villas' (length=6)
is it posssible??
You can use array_combine:
$indexedarray= ['Homes', 'Apartments', 'Villas'];
print_r(array_combine($indexedarray, $indexedarray));
Gives:
Array
(
[Homes] => Homes
[Apartments] => Apartments
[Villas] => Villas
)
But be aware that your duplicate values will be dropped. Keys will be unique!
Try This :
$myArray = [
0 => 'Homes',
1 => 'Apartments',
2 => 'Villas' ];
$newArray = [];
foreach($myArray as $key => $value){
$newArray[$value] = $value;
}
var_dump($newArray);

Php Convert Multidimensional array to string

Below is my function:
public function getChildrenId(){
$child_id = array($this->db->query("SELECT customer_id
FROM " . DB_PREFIX . "customer
WHERE parent IN ( " .(int)$this->customer->getId().") "));
foreach($child_id as $id =>$value) {
$conv = json_decode(json_encode($value), true);
$final = array_slice($conv,2);
foreach ($final as $gchildren => $key) {
sort($key);
$gr = array_slice($key,0,$this->INF);
}
}
return $gr;
}
It outputs:
array (size=3)
0 =>
array (size=1)
'customer_id' => string '2' (length=1)
1 =>
array (size=1)
'customer_id' => string '4' (length=1)
2 =>
array (size=1)
'customer_id' => string '7' (length=1)
I am trying to get the values of the nested arrays. When I use foreach I only get data from array[0]. I also tried slicing the parent array and still didn't get it right, it outputs array,array,array.
I would like to extract these arrays values to a new array that I can use to query the database. final_array = array (2,4,7).
Thank you in advance!
If your array looks like this, then the foreach should create the array your looking for.
array (size=3)
0 =>
array (size=1)
'customer_id' => string '2' (length=1)
1 =>
array (size=1)
'customer_id' => string '4' (length=1)
2 =>
array (size=1)
'customer_id' => string '7' (length=1)
The following php will output array(2,4,7);
<?php
$aNewArray = array();
foreach($aArray as $aArray){
$aNewArray[] = $aArray['customer_id'];
}
var_dump($aNewArray);
?>
You dont need a multidimensional array for this though.

Check if array contains child array and merge in parent array

I want to do :
Check if array contains array.
If parent array contains child array then interchange the key and value of child array.
Update all keys of child array to '1'. i.e. value of child array after interchange key and value
Delete child array and merge its element to parent array.
Example:
Generated array:
array
'first_name' => string 'sushil' (length=6)
'last_name' => string 'asfasfaf' (length=8)
'gen' => string 'Male' (length=4)
'language' => string 'PHP' (length=3)
'biodata' => string 'sfsafsaf hdffd ' (length=15)
'hobbies' =>
array
0 => string 'gaming' (length=6)
1 => string 'football' (length=8)
2 => string 'cricket' (length=7)
'academic_qualification' => string 'Bachelor' (length=8)
I want to modify as above step:
//finds if child array exists. If exists interchange key and value and update value to '1'.
array
'gaming' => int 1
'football' => int 1
'cricket' => int 1
And finally unset original child array and merge modified child array's element to parent array.
My expected array form:
array
'first_name' => string 'sushil' (length=6)
'last_name' => string 'asfasfaf' (length=8)
'gen' => string 'Male' (length=4)
'language' => string 'PHP' (length=3)
'biodata' => string 'sfsafsaf hdffd ' (length=15)
'academic_qualification' => string 'Bachelor' (length=8)
'gaming' => int 1
'football' => int 1
'cricket' => int 1
I tried like following but its not working:
$submited_data = $_POST;
var_dump($submited_data);
foreach($submited_data as $value){
if(is_array($value)) { //checks if array contains array.
$a = array_flip($value); //then interchange
var_dump($a);
foreach($a as $key=>$b){
$a[$key] = 1; //update all value to '1'.
}
array_push($submited_data,$a); // here is the problem I cannot proceed to furthur step. Please help me. How to merge modified child array to parent array.
var_dump($a);
}
}
Thank You.
Ive just rewritten your code a bit. Please tell me if anything goes wrong!
<?php
$submited_data = $_POST;
foreach($submited_data as $key => $data)
{
if(is_array($data))
{
foreach($data as $sub_data)
{
$submited_data[$sub_data] = 1;
}
unset($submitted_data[$key]);
}
}
suppose $array is the array with child array,
$mergedArray = [];
array_walk_recursive($array, function($a,$b) use (&$mergedArray) { $mergedArray[$b] = $a; });
echo '<pre>';
print_r($mergedArray);
echo '</pre>';

Find arrays that share a common value from collection of arrays. May be 0 or more matches

I have the following data structure:
array (size=3)
0 =>
array (size=4)
0 => string 'apple' (length=5)
1 => string 'colophon' (length=8)
2 => string 'byo-fusion-drive' (length=16)
3 => string 'scroll-targeting' (length=16)
1 =>
array (size=3)
0 => string 'apply' (length=5)
1 => string 'exploring-web-typography' (length=24)
2 => string 'on-performance-content-management' (length=33)
2 =>
array (size=3)
0 => string 'macbook' (length=7)
1 => string 'colophon' (length=8)
2 => string 'nifty-minidrive' (length=15)
I'm trying to find out which, if any, arrays in my collection of arrays share a common value.
E.g: Arrays 0 and 2 share the string "colophon".
I've tried using array_intersect but this, of course, returns NULL since array 1 has no values in common with the others.
Also, it's possible (likely even) that in any given collection there will be no common value. The collection of arrays will always contain at least two arrays. There could be any number of additional arrays in the collection.
With the data described above, the end result should be something like this:
array (size=2)
0 =>
array (size=4)
0 => string 'apple' (length=5)
1 => string 'colophon' (length=8)
2 => string 'byo-fusion-drive' (length=16)
3 => string 'scroll-targeting' (length=16)
1 =>
array (size=3)
0 => string 'macbook' (length=7)
1 => string 'colophon' (length=8)
2 => string 'nifty-minidrive' (length=15)
I.e: With array 1 (from the original) being removed as it shares no common value.
I'm sure there's a simple way to do this, but I have been trying for 8+ hours now and have decided to ask for help.
Anyone?
You'll need to iterate it with a nested loop. like so:
$array = array(
array(
"apple",
"colophon",
"byo-fusion-drive",
"scroll-targeting"
),
array(
"apply",
"exploring-web-typography",
"on-performance-content-management"
),
array(
"macbook",
"colophon",
"nifty-minidrive"
)
);
for ($i = 0; $i < count($array); $i++) {
for ($j = $i+1; $j < count($array); $j++) {
var_dump(array_intersect($array[$i], $array[$j]));
}
}
Which outputs:
array (size=0)
empty
array (size=1)
1 => string 'colophon' (length=8)
array (size=0)
empty
A simple modification to the loop gives the expected behavior:
$result = array();
for ($i = 0; $i < count($array); $i++) { //Start from the first array, and continue up to all of them.
for ($j = $i+1; $j < count($array); $j++) { //Start with the current array of $i, +1. So that collisions never occur.
if (count(array_intersect($array[$i], $array[$j])) !== 0) { //If there are common values (the array_intersect() function returns a non-empty array
if (!in_array($array[$i], $result)) $result[] = $array[$i]; //Add the first array (if it's not there already)
if (!in_array($array[$j], $result)) $result[] = $array[$j]; //Add the second array (if it's not there already)
}
}
}
Which outputs
array (size=2)
0 =>
array (size=4)
0 => string 'apple' (length=5)
1 => string 'colophon' (length=8)
2 => string 'byo-fusion-drive' (length=16)
3 => string 'scroll-targeting' (length=16)
1 =>
array (size=3)
0 => string 'macbook' (length=7)
1 => string 'colophon' (length=8)
2 => string 'nifty-minidrive' (length=15)
Try
$arr = array (
array('apple', 'colophon', 'byo-fusion-drive', 'scroll-targeting', ),
array('apply', 'exploring-web-typography', 'on-performance-content-management', ),
array('macbook', 'colophon', 'nifty-minidrive', 'nifty-minidrive', ),
);
$repeated_values = array_keys(
array_filter(
array_count_values(
array_reduce($arr, function ($res, $value) {
return array_merge($res, array_unique($value));
}, array()
)
), function ($count) {
return $count > 1;
})
);
$result = array_filter($arr, function($value) use ($repeated_values) {
if (sizeof(array_intersect($repeated_values, $value)) > 0) return true;
});
var_dump($result);
Output
array (size=2)
0 =>
array (size=4)
0 => string 'apple' (length=5)
1 => string 'colophon' (length=8)
2 => string 'byo-fusion-drive' (length=16)
3 => string 'scroll-targeting' (length=16)
2 =>
array (size=4)
0 => string 'macbook' (length=7)
1 => string 'colophon' (length=8)
2 => string 'nifty-minidrive' (length=15)
3 => string 'nifty-minidrive' (length=15)

Categories