i want the parent with childs in array!
recursive part is make a empty array!
php:
function buildNavigation($items, $parent = NULL) {
$arr = [];
foreach ($items as $item) {
if ($item->parent == $parent) {
$hasChildren = true;
$arr[] = $item->title;
$arr[] = $item->link;
$arr[] = $this->buildNavigation($items, $item->id);
}
}
return $arr;
}
result:
array (size=15)
0 => string 'صفحه اصلی' (length=17)
1 => string 'index' (length=5)
2 =>
array (size=0)
empty
3 => string 'محصولات' (length=14)
4 => string 'products' (length=8)
5 =>
array (size=69)
0 => string 'ابزار' (length=10)
1 => string 'cornic' (length=6)
2 =>
array (size=0)
empty
12 => string 'تمام صفحات' (length=19)
13 => string '' (length=0)
14 =>
array (size=0)
empty
array (size=0)
empty
how can remove this empty array from all?
thanks for help!
how should remove that?
Don't add it to array, if it's empty.
$navigation = $this->buildNavigation($items, $item->id);
if(!empty($navigation)){
$arr[] = $navigation;
}
Related
my question is how can i get all values from determined level of some array, i have this array:
array (size=4)
'Azul' =>
array (size=2)
'128GB' =>
array (size=2)
'Cristal' => string 'Cristal' (length=7)
'Plastico' => string 'Plastico' (length=8)
'64GB' =>
array (size=1)
'Cristal' => string 'Cristal' (length=7)
'Blanco' =>
array (size=2)
'32GB' =>
array (size=1)
'Plastico' => string 'Plastico' (length=8)
'64GB' =>
array (size=1)
'Madera' => string 'Madera' (length=6)
'Dorado' =>
array (size=1)
'64GB' =>
array (size=1)
'Plastico' => string 'Plastico' (length=8)
'Verde' =>
array (size=1)
'64GB' =>
array (size=1)
'Madera' => string 'Madera' (length=6)
And i want get the first level with this recursive function, but i cant find more deeper than 2 levels for example i need the first level and i get:
Azul, Blanco, Dorado, Verde
But i need the second level of Azul i get:
128GB, 64GB
The questions is, if i need the 3rd level of Azul and 64GB, what can i do to get this, having the keys Azul and 64GB or level 3.
My recursive but buggy function is this:
function recursive($array, $level, $itemLVL)
{
foreach ($array as $key => $value) {
//If $value is an array.
if (is_array($value)) {
//We need to loop through it.
if ($level == $itemLVL) {
//echo "<br> Key: $key - Nivel:$level $itemLVL";
echo "<option value='$key'>$key</option>";
}
recursive($value, $level + 1, $itemLVL);
} elseif ($level == $itemLVL) {
echo "<option value='$key'>$key</option>";
}
}
}
If you know the key names and order, then something like this will return what is under those keys:
function get_array_path($path, $array) {
$temp =& $array;
foreach($path as $key) {
$temp =& $temp[$key];
}
return $temp;
}
Pass an array of the keys in order:
$result = get_array_path(['Azul', '64GB'], $array);
If you just want the keys under the path and not everything else, then pass false as the third argument:
function get_array_path($path, $array, $values=true) {
$temp =& $array;
foreach($path as $key) {
$temp =& $temp[$key];
}
if($values === false) {
$temp = array_keys($temp);
}
return $temp;
}
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;
}
}
);
What I am trying to get is to remove duplicate values in the Rest field, but I want to assign / keep its date in the original. element:
array (size=413)
0 =>
array (size=5)
'Date' =>
array (size=1)
0 => int 1588520980
'Rest' => 123abc
1 =>
array (size=5)
'Date' =>
array (size=1)
0 => int 1588520981
'Rest' => qwe123
2 =>
array (size=5)
'Date' =>
array (size=1)
0 => int 1588520983
'Rest' => qwe123
I try it but it doesn't work
public function find_repeats($arr)
{
foreach(array_column($arr, 'Rest') as $ckey=>$value) {
$keys = array_reverse(array_keys(array_column($arr, 'Rest'), $value));
foreach ($keys as $v) {
if ($ckey != $v && isset($arr[$v]))
{
$arr[$ckey]['Date'][] = $arr[$v]['Date'][0];
unset($arr[$v]);
}
}
}
return $arr;
}
This is what the table should look like after this operation
array (size=413)
0 =>
array (size=5)
'Date' =>
array (size=1)
0 => int 1588520980
'Rest' => 123abc
1 =>
array (size=5)
'Date' =>
array (size=1)
0 => int 1588520981
1 => int 1588520983
'Rest' => qwe123
Thanks for help! :)
Simple solution without all these stacked functions:
$newData = [];
foreach ($arr as $item) {
$rest = $item['Rest'];
if (!isset($newData[$rest])) {
$newData[$rest] = $item;
} else {
$newData[$rest]['Date'][] = $item['Date'][0];
}
}
// optionally apply array_values to get 0-indexed array:
$newData = array_values($newData);
I am creating a site and I am trying to do a few complicated things with arrays to get it to work.
It is for an e-commerce site and each product on my site can have a number of attributes. I am trying to get each combination of attributes to do the pricing for them seperately.
First I get an array of the attribute ids ($attribute_id_array) and the query the database for the options for that array.
So if one attribute was colors the options here would be red,green,blue,etc,. or size would be small,medium,large,etc,. These are then stored in a new array ($attribute_arrays).
I then go through these to get every combination of attributes the product can have and sort these into a new array again ($new_attributes_array).
I then loop through this and create a price form for each combination.
$attribute_arrays = [];
foreach($attribute_id_array as $attribute_id){
$params = [$attribute_id];
$sql = "SELECT * FROM attributes WHERE id=?";
$attributeResult = DB::run($sql,$params);
while($row = $attributeResult->fetch(PDO::FETCH_ASSOC)){
array_push($attribute_arrays,$row);
}
}
var_dump($attribute_arrays);
function combinations($arrays, $i = 0) {
if (!isset($arrays[$i])) {
return array();
}
if ($i == count($arrays) - 1) {
return $arrays[$i];
}
$tmp = combinations($arrays, $i + 1);
$result = array();
foreach ($arrays[$i] as $v) {
foreach ($tmp as $t) {
$result[] = is_array($t) ?
array_merge(array($v), $t) :
array($v, $t);
}
}
return $result;
}
$new_attributes_array = combinations($attribute_arrays);
var_dump($new_attributes_array);
This is all working fine except I want to be able to get the keys for all of the key value pairs so I can reference it back to my database.
The way it comes out at the moment is like this:
$attribute_id_array:
array (size=2)
1 => string '5' (length=1)
2 => string '7' (length=1)
$attribute_arrays:
0 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string 'Gated' (length=5)
1 =>
array (size=2)
'attribute1' => string '3 metres' (length=8)
'attribute2' => string '6 metres' (length=8)
$new_attributes_array:
0 =>
array (size=2)
0 => string 'Step Through Bars' (length=17)
1 => string '3 metres' (length=8)
1 =>
array (size=2)
0 => string 'Step Through Bars' (length=17)
1 => string '6 metres' (length=8)
2 =>
array (size=2)
0 => string 'Gated' (length=5)
1 => string '3 metres' (length=8)
3 =>
array (size=2)
0 => string 'Gated' (length=5)
1 => string '6 metres' (length=8)
Is there a way to get it so that the key will be similar in format to:
0 =>
array (size=2)
5-attribute1 => string 'Step Through Bars' (length=17)
7-attribute1 => string '3 metres' (length=8)
1 =>
array (size=2)
5-attribute1 => string 'Step Through Bars' (length=17)
7-attribute2 => string '6 metres' (length=8)
Edit
So I changed the line array_push($attribute_arrays,$row); to $attribute_arrays[$attribute_id] = $row;.
This now means that $attribute_arrays now has the$attribute_id variable as the key like so:
array (size=2)
5 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string 'Gated' (length=5)
7 =>
array (size=2)
'attribute1' => string '3 metres' (length=8)
'attribute2' => string '6 metres' (length=8)
This now means my other function for getting the combinations won't work as it is using the $i variable as the index for the array starting at '0'.
Found another function online to sort it here How to generate in PHP all combinations of items in multiple arrays:
function combinations($arrays) {
$result = array(array());
foreach ($arrays as $property => $property_values) {
$tmp = array();
foreach ($result as $result_item) {
foreach ($property_values as $property_key => $property_value) {
$tmp[] = $result_item + array($property_key => $property_value);
}
}
$result = $tmp;
}
return $result;
}
However, this doesn't do exactly as I want and I end up with this:
array (size=4)
0 =>
array (size=1)
'attribute1' => string 'Step Through Bars' (length=17)
1 =>
array (size=2)
'attribute1' => string 'Step Through Bars' (length=17)
'attribute2' => string '6 metres' (length=8)
2 =>
array (size=2)
'attribute2' => string 'Gated' (length=5)
'attribute1' => string '3 metres' (length=8)
3 =>
array (size=1)
'attribute2' => string 'Gated' (length=5)
try this as your combinations function
modified code taken from here
function combinations($arrays) {
$result = array(array());
foreach ($arrays as $key => $values) {
$tmp = array();
foreach ($result as $item) {
foreach ($values as $k=>$value) {
$tmp[] = array_merge($item, array($key.'-'.$k => $value));
}
}
$result = $tmp;
}
return $result;
}
Instead of using while on following line
while($row = $attributeResult->fetch(PDO::FETCH_ASSOC)){
Use foreach loop to get key paired value. e.g
foreach($databaseFetchedRecord as $mykey=>$myvalue)
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));