I have one array as
$tmpArr = array('A', 'B', 'C');
I want to process this array and want new array as
$tmpArr[A][B][C] = C
I.e last element becomes the value of final array.
Can anyone suggest the solution? Please help. Thanks in advance
Iterate the array of keys and use a reference for the end of the chain:
$arr = array();
$ref = &$arr;
foreach ($tmpArr as $key) {
$ref[$key] = array();
$ref = &$ref[$key];
}
$ref = $key;
$tmpArr = $arr;
$tmpArr = array('A', 'B', 'C');
$array = array();
foreach (array_reverse($tmpArr) as $arr)
$array = array($arr => $array);
Output:
Array
(
[A] => Array
(
[B] => Array
(
[C] => Array
(
)
)
)
)
$tmpArr[$tmpArr[0]][$tmpArr[1]][$tmpArr[2]] = $tmpArr[2];
Is that what you want?
Related
I have an array with date & time like below:
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
From each value, the T10:00 should be cut off, so that my new array looks like this:
$new_array = array('2021-05-04', '2021-05-05', '2021-05-06');
How can i do that?
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = [];
foreach($array as $a) {
$a = explode('T', $a)[0];
array_push($new_array, $a);
}
Iterate through the array by array_map with callback function take only first 10 chars, Which represent time.
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = array_map(fn($time)=>substr($time, 0, 10), $array);
print_r($new_array);
Prints:
/*
Array
(
[0] => 2021-05-04
[1] => 2021-05-05
[2] => 2021-05-06
)
*/
the T10:00 should be cut off
If you have a constant time T10:00 and want to get rid of it just replace it with empty!
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = array_map(fn($time)=>str_replace('T10:00', '', $time), $array);
print_r($new_array);
//Array ( [0] => 2021-05-04 [1] => 2021-05-05 [2] => 2021-05-06 )
I want difference of multidimensional array with single array. I dont know whether it is possible or not. But my purpose is find diference.
My first array contain username and mobile number
array1
(
array(lokesh,9687060900),
array(mehul,9714959456),
array(atish,9913400714),
array(naitik,8735081680)
)
array2(naitik,atish)
then I want as result
result( array(lokesh,9687060900), array(mehul,9714959456) )
I know the function array_diff($a1,$a2); but this not solve my problem. Please refer me help me to find solution.
Try this-
$array1 = array(array('lokesh',9687060900),
array('mehul',9714959456),
array('atish',9913400714),
array('naitik',8735081680));
$array2 = ['naitik','atish'];
$result = [];
foreach($array1 as $val2){
if(!in_array($val2[0], $array2)){
$result[] = $val2;
}
}
echo '<pre>';
print_r($result);
Hope this will help you.
You can use array_filter or a simple foreach loop:
$arr = [ ['lokesh', 9687060900],
['mehul', 9714959456],
['atish', 9913400714],
['naitik', 8735081680] ];
$rem = ['lokesh', 'naitik'];
$result = array_filter($arr, function ($i) use ($rem) {
return !in_array($i[0], $rem); });
print_r ($result);
The solution using array_filter and in_array functions:
$array1 = [
array('lokesh', 9687060900), array('mehul', 9714959456),
array('atish', 9913400714), array('naitik', 8735081680)
];
$array2 = ['naitik', 'atish'];
$result = array_filter($array1, function($item) use($array2){
return !in_array($item[0], $array2);
});
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => lokesh
[1] => 9687060900
)
[1] => Array
(
[0] => mehul
[1] => 9714959456
)
)
The same can be achieved by using a regular foreach loop:
$result = [];
foreach ($array1 as $item) {
if (!in_array($item[0], $array2)) $result[] = $item;
}
I've some difficulties creating a nested array by array of keys and assigning a value for the last nested item.
For example, lets $value = 4; and $keys = ['a', 'b', 'c'];
The final result should be:
[
'a' => [
'b' => [
'c' => 4
]
]
]
I've tried with a recursion, but without success.
Any help would be greatly appreciated.
you don't need recursion, just do it from the right to left:
$a = $value;
for ($i = count($keys)-1; $i>=0; $i--) {
$a = array($keys[$i] => $a);
}
or the even shorter version from #felipsmartins:
$a = $value;
foreach (array_reverse($keys) as $valueAsKey) $a = [$valueAsKey => $a];
Your can try it.
$value = 4;
$keys = ['a', 'b', 'c'];
$a = $value;
$i=count($keys)-1;
foreach($keys as $key){
$a = array($keys[$i] => $a);
$i--;
}
print_r($a);
Output
Array
(
[a] => Array
(
[b] => Array
(
[c] => 4
)
)
)
Solution by getting a nested item in the resulting array by reference:
$value = 4;
$keys = ['a', 'b', 'c'];
$result = [];
$reference = &$result;
foreach($keys as $key) {
if (!array_key_exists($key, $reference)) $reference[$key] = [];
$reference = &$reference[$key];
}
$reference = $value;
print_r($result);
Array 1:
Array (
'127.0.0.1',
'235.107.12.3'
)
Array 2:
Array (
'34.235.54.6',
'230.56.78.1'
)
Final Array should like below:
Array (
[127.0.0.1] => Array (
'34.235.54.6',
'230.56.78.1'
),
[235.107.12.3]' => Array (
'34.235.54.6',
'230.56.78.1'
)
)
Please give an advice as to how I can merge these two arrays (array 1 and array 2) to achieve the desired result.
Use array_fill_keys:
$final = array_fill_keys( $array1, $array2 );
Try this one
$a = array_fill_keys($array1, $array2);
Print_r($a);
Output:
Array(
[127.0.0.1]=>
array
(
'34.235.54.6',
'230.56.78.1'
),
[235.107.12.3]'=>
array
(
'34.235.54.6',
'230.56.78.1'
)
)
you can get your job done using loop, for eg. foreach loop here
foreach($array1 AS $val1)
{
foreach($array2 AS $val2)
{
$newarr[$val1][] = $val2;
}
}
print_r($newarr);
<?php
$final = array();
foreach($array1 as $k => $v)
$final[$k] = $array2;
var_dump($final);
?>
$arrayA = array('127.0.0.1','235.107.12.3' );
$arrayB = array('34.235.54.6','230.56.78.1');
$i = 0;
foreach($arrayA as $a){
$arrayC[$i] = arrayB;
$i++;
}
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/