Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to generate a nested array from a string.
The String looks like this
$item = "profile:my_wallet:btn_recharge";
I want to convert it to nested array like this
["profile"]["my_wallet"]["btn_recharge"]
What you could use is a simple recursion to go as deep as you like.
function append(array $items, array $array = []): array
{
$key = array_shift($items);
if (!$key) return $array;
$array[$key] = append($items, $array);
return $array;
}
$array = append(explode(':', "profile:my_wallet:btn_recharge"));
The result of $array looks like below and can be accessed as you asked
$array['profile']['my_wallet']['btn_recharge'];
array (
'profile' =>
array (
'my_wallet' =>
array (
'btn_recharge' =>
array (
),
),
),
)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
my string has
$string = "apple,banana,orange,lemon";
I want to as
$array = [
[apple,banana],
[orange,lemon]
]
Use array_chunk in combination with explode defined in php https://www.php.net/manual/en/function.array-chunk.php
<?php
$string = "apple,banana,orange,lemon";
$input_array = explode (",", $string);
print_r(array_chunk($input_array, 2));
?>
Will output as below:
Array
(
[0] => Array
(
[0] => apple
[1] => banana
)
[1] => Array
(
[0] => orange
[1] => lemon
)
)
I hope this helps you get on your way. To transform a string to an array, you can use
$elements = explode(',', $string);
This will leave you with this array: ($elements == [apple,banana,orange,apple])
From there, you can build it the way you want, like:
$array[] = [$elements[0], $elements[1]];
$array[] = [$elements[2], $elements[3]];
This results in the array you are looking for, but this solution is only for the string you've given with four elements, separated by comma.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
im recieveing an output in the following format.When i foreach the loop only one id is iterated i need to combine these array into one single array so i can iterate all the ids.
Array
(
[0] => 6902680092829
)
Array
(
[0] => 6902680125597
)
Array
(
[0] => 6902680158365
)
Array
(
[0] => 6902680223901
)
Array
(
[0] => 6902680256669
)
i want to combine this array into one single array for eg:
array:{
0=>1,
1=>2,
2=>3
}
im adding shopify product id one by one to an array by doing this
$prdid=[];
array_push($prdid,$shopifyCustomers['body']['container']['product']['id']);
You can simply use array_merge()
$arr1 = [ 6902680092829 ];
$arr2 = [ 6902680125597 ];
$arr3 = [ 6902680158365 ];
$arr4 = [ 6902680223901 ];
$arr5 = [ 6902680256669 ];
$newArr = array_merge($arr1, $arr2, $arr3, $arr4, $arr5);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I confused how to make depth from basic array.
$array = array('736', '827', '831');
With foreach loop, I want final result like this:
Array
(
[736] => Array
(
[827] => Array
(
[831] => Array
(
)
)
)
)
Just like you want them, do this:
array
(
736 => array
(
827 => array
(
831 => array
(
)
)
)
)
or did I misunderstand the question?
using a foreach backwards:
$arr=array(831,827,736);
$newref=array();
foreach($arr as $el)
{
$newref=array($el=>$newref);
}
for reversing an array: $arr=array_reverse($arr);
Possibly not the most efficient, but something like this should do it.
function nestedArray($array) {
$newArray = [];
$pointer = &$newArray;
foreach($array as $value) {
$pointer[$value] = [];
$pointer = &$pointer[$value];
}
return $newArray;
}
$arr = [736, 827, 831];
var_dump(nestedArray($arr));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a database table that would return an array something like this
$roles['admin'] = 'app1,app2,app3';
$roles['moderator'] = 'app2,app3';
Now I want to traverse this array to show in my view, But instead of showing all apps inside each role, i would like to show all roles inside each app.
So ideally i would like the above array to become this
$apps['app1'] = 'admin';
$apps['app2'] = 'admin,moderator';
$apps['app3'] = 'admin,moderator';
I have been trying to solve this for 2 hours now, but for some reason I can't find an efficient way of doing this.
The following will traverse your array and load it into the appropriate array. It works by going through each part of the array and traversing it.
<?php
$apps = array();
foreach($roles as $key1 => $value1){
$parts = explode(',', $value1);
foreach($parts as $key2 => $value2){
$apps[$value2] .= (strlen($apps[$value2])>0)?",":"").$key1;
}
}
?>
You can map one array to the other by using array_reduce, array_keys and explode to turn your CSV values into arrays
$apps = array_reduce(array_keys($roles), function($apps, $key) use ($roles) {
foreach (explode(',', $roles[$key]) as $app) $apps[$app][] = $key;
return $apps;
}, []);
Note that the result is slightly different to what you wanted in that the values are themselves arrays instead of comma separated strings.
Array
(
[app1] => Array
(
[0] => admin
)
[app2] => Array
(
[0] => admin
[1] => moderator
)
[app3] => Array
(
[0] => admin
[1] => moderator
)
)
If you really need CSV values, add this
$apps = array_map(function($list) {
return implode(',', $list);
}, $apps);
which produces
Array
(
[app1] => admin
[app2] => admin,moderator
[app3] => admin,moderator
)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need a simple way to extract values from an array.
I just can't get it to work.
This is the array from the database with <pre>:
Array
(
[0] => Array
(
[title] => Title1
)
[1] => Array
(
[title] => Title2
)
)
I can't get the titles to echo :-(
M.
-EDIT-
This did the trick for me:
foreach($arrays as $array)
{
echo $array['title'];
}
foreach($arrays as $array)
{
echo $array['title'];
}
Loop through the array like:
foreach($array as $arr)
{
echo $arr['title'];
}
Or to access one item, use:
echo $array[0]['title'];
echo $array[1]['title'];