Say, I have data in a tree structure implemented as an array of arrays to an arbitrary depth, something like
print_r($my_array);
Array
(
[id] => 123
[value] => Hello, World!
[child] => Array
(
[name] => Foo
[bar] => baz
)
[otherchild] => Array
(
[status] => fubar
[list] => Array
(
[one] => 1
[two] => 3
)
)
[sanity] => unchecked
)
Now, using a single string as key I would like to address a node at an arbitrary depth, Let's say I have a key something like this:
$key = 'otherchild|list|two';
Using this key I want to be able to address the value stored in
$my_array['otherchild']['list']['two']
Obviously I can explode('|', $key) to get an array of keys, and shifting values off of this and using those to address sub-arrays makes it easy to obtain the value I'm looking for, something like
$value = $my_array;
$keys = explode('|', $key);
while ($k = array_shift($keys)) {
if (isset($value[$k])) {
$value = $value[$k];
} else {
// handle failure
}
} // Here, if all keys exist, $value holds value of addressed node
But I'm stuck trying to update values in a generic way, ie without having to resort to something like
$keys = explode('|', $key);
if (count($keys) == 1) {
$my_array[$keys[0]] = $new_value;
} else if (count($keys) == 2) {
$my_array[$keys[0]][$keys[1]] = $new_value;
} else if ...
Any ideas?
function setAt(array & $a, $key, $value)
{
$keys = explode('|', $key);
// Start with the root node (the array itself)
$node = & $a;
// Walk the tree, create nodes as needed
foreach ($keys as $k) {
// Create node if it does not exist
if (! isset($node[$k])) {
$node[$k] = array();
}
// Walk to the node
$node = & $node[$k];
}
// Position found; store the value
$node = $value;
}
// Test
$array = array();
// Add new values
setAt($array, 'some|key', 'value1');
setAt($array, 'some|otherkey', 'val2');
setAt($array, 'key3', 'value3');
print_r($array);
// Overwrite existing values
setAt($array, 'some|key', 'new-value');
print_r($array);
setAt($array, 'some', 'thing');
print_r($array);
If you're looking for a short answer, you can also use eval().
$elem = "\$array['" . str_replace("|", "']['", $key) . "']";
$val = eval("return isset($elem) ? $elem : null;");
Related
I have an array from array_diff function and it looks like below:
Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
[5] => in
[6] => our
)
As you can see, we have a gap between the keys #3 and #5 (i.e. there is no key #4).
How can I split that array into 2 parts, or maybe more if there are more gaps?
The expected output would be:
Array
(
[0] => Array
(
[0] => world
[1] => is
[2] => a
[3] => wonderfull
)
[1] => Array
(
[0] => in
[1] => our
)
)
You can use old_key,new_key concept to check that there difference is 1 or not? if not then create new index inside you result array otherwise add the values on same index:-
<?php
$arr = Array(0 => 'world',1 => 'is',2 => 'a',3 => 'wonderfull',5 => 'in',6 => 'our');
$new_array = [];
$old_key = -1;
$i = 0;
foreach($arr as $key=>$val){
if(($key-$old_key) ==1){
$new_array[$i][] = $val;
$old_key = $key;
}
if(($key-$old_key) >1){
$i++;
$new_array[$i][] = $val;
$old_key = $key;
}
}
print_r($new_array);
https://3v4l.org/Yl9rp
You can make use of the array internal pointer to traverse the array.
<?php
$arr = Array(0=>"world",1=>"is",2=>"a",3=>"wonderfull",5=>"in",6=>"our");
print_r($arr);
$result = Array();
$lastkey;
while($word = current($arr))
{
$key = key($arr);
if(isset($lastkey) && $key == $lastkey + 1)
{
$result[count($result) - 1][] = $word;
}
else
{
$result[] = Array($word);
}
$lastkey = $key;
next($arr);
}
print_r($result);
?>
This task is a perfect candidate for a reference variable. You unconditionally push values into a designated "bucket" -- in this case a subarray. You only conditionally change where that bucket is in the output array.
There are two important checks to make when determining if a new incremented key should be generated:
if it is not the first iteration and
the current key minus (the previous key + 1) does not equal 0.
Code: (Demo)
$nextKey = null;
$result = [];
foreach ($array as $key => $val) {
if ($nextKey === null || $key !== $nextKey) {
unset($ref);
$result[] = &$ref;
}
$ref[] = $val;
$nextKey = $key + 1;
}
var_export($result);
This solution generates an indexed array starting from zero with my sample input and uses only one if block. In contrast, AliveToDie's solution generates a numerically keyed array starting from 1 and uses two condition blocks containing redundant lines of code.
I have the following:
$values = Array (
['level1'] => Array (
['level2'] => Array(
['level3'] => 'Value'
)
)
)
I also have an array of keys:
$keys = Array (
[0] => 'level1',
[1] => 'level2',
[2] => 'level3'
)
I want to be able to use the $keys array so I can come up with: $values['level1']['level2']['level3']. The number of levels and key names will change so I need a solution that will read my $keys array and then loop through $values until I get the end value.
You could iterate over $values and store a $ref like this :
$ref = $values ;
foreach ($keys as $key) {
if (isset($ref[$key])) {
$ref = $ref[$key];
}
}
echo $ref ; // Value
You also could use references (&) to avoid copy of arrays :
$ref = &$values ;
foreach ($keys as &$key) {
if (isset($ref[$key])) {
$ref = &$ref[$key];
}
}
echo $ref ;
<?php
$values['level1']['level2']['level3'] = 'Value';
$keys = array (
0 => 'level1',
1 => 'level2',
2 => 'level3'
);
$vtemp = $values;
foreach ($keys as $key) {
try {
$vtemp = $vtemp[$key];
print_r($vtemp);
print("<br/>---<br/>");
}
catch (Exception $e) {
print("Exception $e");
}
}
Hope this helps. Of course remove the print statements but I tried it and in the end it reaches the value. Until it reaches the values it keeps hitting arrays, one level deeper at a time.
I want to concat values of array with same key
Example:
[0] => Array
(
[0] => A
[1] => XYZ
)
[1] => Array
(
[0] => B
[1] => ABC
)
[2] => Array
(
[0] => A
[1] => LMN
)
[3] => Array
(
[0] => B
[1] => PQR
)
)
Expected output:
[0] => Array
(
[0] => A
[1] => XYZ,LMN
)
[1] => Array
(
[0] => B
[1] => ABC,PQR
)
)
A simple solution uses the PHP function array_reduce():
// The input array you posted in the question
$input = array(
array('A', 'XYZ'),
array('B', 'ABC'),
array('A', 'LMN'),
array('B', 'PQR'),
);
// Reduce the array to a new array that contains the data aggregated as you need
$output = array_reduce(
// Process each $item from $input using a callback function
$input,
// The callback function processes $item; the partial result is $carry
function (array $carry, array $item) {
// Extract the key into a variable
$key = $item[0];
// If the key was encountered before
// then a partial entry already exists in $carry
if (isset($carry[$key])) {
// Append the desired value to the existing entry
$carry[$key][1] .= ','.$item[1];
} else {
// Create a new entry in $carry (copy $item to key $key for quick retrieval)
$carry[$key] = $item;
}
// Return the updated $carry
return $carry;
},
// Start with an empty array (it is known as $carry in the callback function)
array()
);
// $output contains the array you need
Try this:
$final = array();
foreach ($array_items as $item)
{
$key = $item[0];
$found_index = -1;
for ($i=0; $i<count($final); $i++)
{
if ($key == $final[$i][0])
{
$found_index = $i;
break;
}
}
if ($found_index == -1)
{
$final_item = array();
$final_item[0] = $key;
$final_item[1] = $item[1];
$final[] = $final_item;
}
else
{
$final[$found_index][1] .= ",".$item[1];
}
}
We create a new array $final, and loop through your old array $array_items. For each item, we see if there is already an item in $final that has the same [0] index. If it doesn't exist, we create it and add the initial string to the [1] index. If it does exist, we just have to add the string onto the end of the [1] index.
Try it, substituting $array_items for whatever your array is called, let me know if it works.
Check my solution. It should work fine. I hope it will help you much.
$result = $passed_keys = $extended_arr = [];
foreach ($arr as $k => $value) {
for($i = $k + 1; $i < count($arr); $i++){
if ( $value[0] == $arr[$i][0] ){ // compare each array with rest subsequent arrays
$key_name = $value[0];
if (!array_key_exists($key_name, $result)){
$result[$key_name] = $value[1] .",". $arr[$i][1];
} else {
if (!in_array($i, $passed_keys[$key_name])) {
$result[$key_name] .= ",". $arr[$i][1];
}
}
$passed_keys[$key_name][] = $i; // memorizing keys that were passed
}
}
}
array_walk($result, function($v, $k) use(&$extended_arr){
$extended_arr[] = [$k, $v];
});
The result is in $extended_arr
My solution, creates a custom key which makes identifying the letter much easier. This removes the need to continuously iterate through each array, which can become a major resources hog.
<?php
$inital_array = array(
array('A','XYZ'),
array('B','ABC'),
array('A','LMN'),
array('B','PQR')
);
$concat_array = array();
foreach($inital_array as $a){
$key = $a[0];
if( !isset($concat_array[$key]) ){
$concat_array[$key] = array($key,'');
}
$concat_array[$key][1] .= (empty($concat_array[$key][1]) ? '' : ',').$a[1];
}
$concat_array = array_values($concat_array);
echo '<pre>',print_r($concat_array),'</pre>';
I have a collection of keys in this massive flat single array I would like to basically expand that array into a multidimensional one organized by keys - here is an example:
'invoice/products/data/item1'
'invoice/products/data/item2'
'invoice/products/data/item2'
=>
'invoice'=>'products'=>array('item1','item2','item3')
how can I do this - the length of the above strings are variable...
Thanks!
$src = array(
'invoice/products/data/item1',
'invoice/products/data/item2',
'invoice/products/data/item2',
'foo/bar/baz',
'aaa/bbb'
);
function rsplit(&$v, $w)
{
list($first, $tail) = explode('/', $w, 2);
if(empty($tail))
{
$v[] = $first;
return $v;
}
$v[$first] = rsplit($v[$first], $tail);
return $v;
}
$result = array_reduce($src, "rsplit");
print_r($result);
Output is:
Array (
[invoice] => Array
(
[products] => Array
(
[data] => Array
(
[0] => item1
[1] => item2
[2] => item2
)
)
)
[foo] => Array
(
[bar] => Array
(
[0] => baz
)
)
[aaa] => Array
(
[0] => bbb
)
)
Something along these lines: (Didn't test it though!) Works now ;)
$data = array();
$current = &$data;
foreach($keys as $value) {
$parts = explode("/", $value);
$parts_count = count($parts);
foreach($parts as $i => $part) {
if(!array_key_exists($part, $current)) {
if($i == $parts_count - 1) {
$current[] = $part;
}
else {
$current[$part] = array();
$current = &$current[$part];
}
}
else {
$current = &$current[$part];
}
}
$current = &$data;
}
$keys beeing the flat array.
Although it's not clear from your question how the "/" separated strings will map to an array, the basic approach will probably be something like this:
$result = array();
$k1 = $k2 = '';
ksort($yourData); // This is the key (!)
foreach ($yourData as $k => $v) {
// Use if / else if / else if to watch for new sub arrays and change
// $k1, $k2 accordingly
$result[$k1][$k2] = $v;
}
This approach uses the ksort to ensure that keys at the same "level" appear together, like this:
'invoice/products/data1/item1'
'invoice/products/data1/item2'
'invoice/products/data2/item3'
'invoice/products2/data3/item4'
'invoice/products2/data3/item5'
Notice how the ksort corresponds to the key grouping you're aiming for.
I'm trying to build an associative array in PHP dynamically, and not quite getting my strategy right. Basically, I want to insert a value at a certain depth in the array structure, for instance:
$array['first']['second']['third'] = $val;
Now, the thing is, I'm not sure if that depth is available, and if it isn't, I want to create the keys (and arrays) for each level, and finally insert the value at the correct level.
Since I'm doing this quite a lot in my code, I grew tired of doing a whole bunch of "array_key_exists", so I wanted to do a function that builds the array for me, given a list of the level keys. Any help on a good strategy for this is appreciated. I'm sure there is a pretty simple way, I'm just not getting it...
php doesn't blame you if you do it just so
$array['first']['second']['third'] = $val;
print_r($array);
if you don't want your keys to be hard coded, here's a flexible solution
/// locate or create element by $path and set its value to $value
/// $path is either an array of keys, or a delimited string
function array_set(&$a, $path, $value) {
if(!is_array($path))
$path = explode($path[0], substr($path, 1));
$key = array_pop($path);
foreach($path as $k) {
if(!isset($a[$k]))
$a[$k] = array();
$a = &$a[$k];
}
$a[$key ? $key : count($a)] = $value;
}
// example:
$x = array();
array_set($x, "/foo/bar/baz", 123);
array_set($x, "/foo/bar/quux", 456);
array_set($x, array('foo', 'bah'), 789);
Create a function like:
function insert_into(&$array, array $keys, $value) {
$last = array_pop($keys);
foreach($keys as $key) {
if(!array_key_exists($key, $array) ||
array_key_exists($key, $array) && !is_array($array[$key])) {
$array[$key] = array();
}
$array = &$array[$key];
}
$array[$last] = $value;
}
Usage:
$a = array();
insert_into($a, array('a', 'b', 'c'), 1);
print_r($a);
Ouput:
Array
(
[a] => Array
(
[b] => Array
(
[c] => 1
)
)
)
That's tricky, you'd need to work with references (or with recursion, but I
chose references here):
# Provide as many arguments as you like:
# createNestedArray($array, 'key1', 'key2', etc.)
function createNestedArray(&$array) {
$arrayCopy = &$array;
$args = func_get_args();
array_shift($args);
while (($key = array_shift($args)) !== false) {
$arrayCopy[$key] = array();
$arrayCopy = &$arrayCopy[$key];
}
}
<?php
function setElements(&$a, array $path = [], $values = [])
{
if (!is_array($path)) {
$path = explode($path[0], substr($path, 1));
}
$path = "[ '" . join("' ][ '", $path) . "' ]";
$code =<<<CODE
if(!isset(\$a{$path})){
\$a{$path} = [];
}
return \$a{$path}[] = \$values;
CODE;
return eval($code);
}
$a = [];
setElements($a, [1,2], 'xxx');
setElements($a, [1,2,3], 233);
setElements($a, [1,2,4], 'AAA');
setElements($a, [1,2,3,4], 555);
print_r($a);
Output
Array
(
[1] => Array
(
[2] => Array
(
[0] => xxx
[3] => Array
(
[0] => 233
[4] => Array
(
[0] => 555
)
)
[4] => Array
(
[0] => AAA
)
)
)
)
You should check it here http://sandbox.onlinephpfunctions.com/