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;
}
Related
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)
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;
}
i have the following array i obtain after running a var_dump($response).
array (size=2)
'count' => int 3
'data' =>
array (size=3)
0 => array (size=38)
'ref' => string '24750.0.2530' (length=12)
1 => array (size=38)
'ref' => string '24450.0.2530' (length=12)
i would like to display the 'ref' from the above array.tried running a foreach as below but i get a notice
Trying to get property of non-object
foreach($response as $object)
{
var_dump($object->ref);
}
foreach ($response['data'] as $data) {
var_dump($data['ref']);
}
That's because it isn't an object. It is an array of arrays. Try:
foreach ($response['data'] as $data) {
var_dump($data['ref']);
}
I have 2 arrays and I would like to delete everything in the first array that is in the second array. In thise case I would like to delete "ibomber" and "apphero" in array one. I tried something with unset, but it doesn't look like it works.
array (size=5)
0 => string 'Air Hippo' (length=9)
1 => string 'iBomber Defense Pacific' (length=23)
3 => string 'AppHero' (length=7)
5 => string 'Pillboxie' (length=9)
6 => string 'Monogram' (length=8)
array (size=2)
0 => string ' iBomber Defense Pacific' (length=24)
1 => string ' AppHero' (length=8)
This is what I tried:
foreach ($_SESSION["appsarray"] as $k=>$v)
{
foreach ($_SESSION["finalapps"] as $k2=>$v2)
{
if ($v == $v2)
{
unset ($_SESSION["appsarray"][$k]);
}
}
}
Session appsarray is my first array and session finalapps is my second array.
Thanks!
function TrimmedStrCaseCmp($str1,$str2)
{
return strcasecmp(trim(str1),trim(str2));
}
$result = array_udiff(values,to_remove_from_values,'TrimmedStrCaseCmp');
http://php.net/manual/en/function.array-udiff.php
You're looking for array_diff i.e.;
$appsarray = array('Air Hippo','iBomber Defense Pacific','AppHero','Pillboxie','Monogram');
$finalapps = array('iBomber Defense Pacific','AppHero');
$result = array_diff($appsarray,$finalapps);
print_r($result);
Will output;
Array ( [0] => Air Hippo [3] => Pillboxie [4] => Monogram )