I got this from the name attribute of a field
name="field[a][2][b][0][c][1][field_name]"
after serializing the form, I got this:
array('field[a][2][b][0][c][1][field_name]'=>'value')
and I need to convert that into the following array:
$field = array (
'a' => array (
[2] => array (
'b' => array (
[0] => array (
'c' => array (
[1] => array (
'field_name'=>'value'
)
)
)
)
)
)
);
do I need some sort of foreach function or php can recognize this string as array?
If you want the result nested, use parse_str().
$text = "field[a][2][b][0][c][1][field_name]=value";
parse_str($text, $result);
print_r($result);
Output:
Array
(
[field] => Array
(
[a] => Array
(
[2] => Array
(
[b] => Array
(
[0] => Array
(
[c] => Array
(
[1] => Array
(
[field_name] => value
)
)
)
)
)
)
)
)
See https://3v4l.org/7nmFT
You can get values in brackets with the regular expression, and then reduce it to the array that you want:
$key = 'field[a][2][b][0][c][1][field_name]';
$value = 'value';
$matches = array();
preg_match_all('/\[([^[]+)\]/', $key, $matches);
$keys = array_reverse($matches[1]);
$result = array_reduce($keys, function ($array, $item) {
return array($item => $array);
}, $value);
Explanation
In the regular expression \[([^[]+)\]:
([^[]+) is matches any symbol except opening bracket, one or more
times, and gets it into the capturing group (I hope you will not have nested brackets);
\[...\] is literally matches brackets around.
The preg_match_all function should populate the $matches array with following data:
Array
(
[0] => Array
(
[0] => [a]
[1] => [2]
[2] => [b]
[3] => [0]
[4] => [c]
[5] => [1]
[6] => [field_name]
)
[1] => Array
(
[0] => a
[1] => 2
[2] => b
[3] => 0
[4] => c
[5] => 1
[6] => field_name
)
)
The $matches[0] have values of a full match and the $matches[1] have values of our first and only capturing group. We have interested only in capturing group values.
Then with the array_reduce function we can simply go through keys in the reverse order, and sequentially wrap our value into an array.
You can use explode function to do it. But the values should have space in between them, for example "Hello World" in which the values would be in Array ( [0] => Hello [1] => world ) but in your case it would be like Array ( [0] => field[a][2][b][0][c][1][field_name] ) until $name as space in-between the characters or word.
$name="field[a][2][b][0][c][1][field_name]"
$name_array = explode(" ",$name);
print_r ($name_array);
When testing your problem on my PHP server with this test code
<form method="post">
<input type="text" name="field[a][2][b][0][c][1][field_name]">
<input type="submit" value="OK">
<form>
<pre>
<?php
if (isset($_POST['field']))
print_r($_POST['field']);
?>
</pre>
I got the following response (after entering the word "hello" into the text box and clicking the "OK" button):
Array ( [a] => Array ( [2] => Array ( [b] =>
Array ( [0] => Array ( [c] => Array ( [1] =>
Array ( [field_name] => hello ) ) ) ) ) ) )
Admittedly, it was formatted nicer, but I am posting from my smartphone, so, please, forgive me for not formatting it again manually.
So, to me it is not quite clear why OP needs extra code to solve his/her problem.
Here is your solution:
https://codebrace.com/editor/b06588218
I have used regex match twice to match variable name and arrays
/[(.+?)]/
matches any values in the array where "?" is lazy matching.
while following regex
/^[^[]+/ matches the variable name.
I have used variable variables to create variable from string extracted from above regex
Result:
Array
(
[a] => Array
(
[2] => Array
(
[b] => Array
(
[0] => Array
(
[c] => Array
(
[1] => Array
(
[field_name] => value
)
)
)
)
)
)
)
I hope this helps
Related
I am getting a string separated from commas and I am trying to split them into an array, which works. The only problem is that there is an outer array wrapping the array I want to use. So I don't want to use the $excludes[0] when passing the array to a function. Does anyone know a function I can use to unwrap the array inside $excludes[0]
$excludes = [];
Array ( [0] => Array (
[0] => company_logo,
[1] => social_links,
[2] => rss_link,
[3] => telephone ) )
My expected results would be the below.
Array (
[0] => company_logo,
[1] => social_links,
[2] => rss_link,
[3] => telephone
)
You can simple do :
1st Option:
print_r(array_shift($excludes));
2nd Option:
$new_array = $excludes[0];
print_r($new_array);
Hope this will work
check this
$excludes = Array ( 0 => Array (
0 => 'company_logo',
1 => 'social_links',
2 => 'rss_link',
3 => 'telephone' ) ) ;
$excludes=$excludes[0];
print_r($excludes);die();
I have this array $data :
Array
(
[0] => 86086
[1] => Arnel
[2] => Paras
)
Array
(
[0] => 86085
[1] => Arnely
[2] => Para
)
Array
(
[0] =>
)
How do i remove the bottom array that contains no values totally so it only contains :
Array
(
[0] => 86086
[1] => Arnel
[2] => Paras
)
Array
(
[0] => 86085
[1] => Arnely
[2] => Para
)
I have tried using array_filter($data, strlen) and it just does this :
Array
(
)
array_pop() pops and returns the value of the last element of array, shortening the array by one element and will do exactly what you describe. A.
array_shift() does the opposite (removes first element from array and returns the value)
array_pop() on PHP.net
So you can either do:
$firstVal = array_pop($data)
or just
array_pop($data)
depending on if you want the value back or not.
Might help.
array_values(array_filter($data))
This is my string.
$str = '"additional_details":" {"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"}],';
I want to find all patterns that start with "{" and end with "}".
I am trying this:
preg_match_all( '/"(\{.*\})"/', $json, $matches );
print_r($matches);
It gives me an output of:
Array
(
[0] => Array
(
[0] => "{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"
)
[1] => Array
(
[0] => {"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}
)
)
See the array key 1. It gives all matches in one key and other details too.
I want an array of all matches. Like
Array
(
[0] => Array
(
[0] => "{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"}],"additional_details":"{"mode_of_transport":"air"}"
)
[1] => Array
(
[0] => {"mode_of_transport":"air"},
[1] => {"mode_of_transport":"air"},
[2] => {"mode_of_transport":"air"}
)
)
What should I change in my pattern.
Thanks
You can use:
preg_match_all( '/({[^}]*})/', $str, $matches );
print_r($matches[1]);
Array
(
[0] => {"mode_of_transport":"air"}
[1] => {"mode_of_transport":"air"}
[2] => {"mode_of_transport":"air"}
)
i want to preg_match the following code:
{{{/foo:bar/a/0/b}}}
This is my regex (which doesn't work, and i don't understand why):
|{{{\/([[:alpha:]][[:alnum:]\_]*\:[[:alpha:]][[:alnum:]\_]*)(?:\/([[:alnum:]\_]*))+}}}|Uism
Expected result:
Array (
[0] => Array
(
[0] => {{{/foo:bar/a/0/b}}}
)
[1] => Array
(
[0] => foo:bar
)
[2] => Array
(
[0] => a
)
[3] => Array
(
[0] => 0
)
[4] => Array
(
[0] => b
)
)
The result i get:
Array (
[0] => Array
(
[0] => {{{/foo:bar/a/0/b}}}
)
[1] => Array
(
[0] => foo:bar
)
[2] => Array
(
[0] => b
)
)
I only get the last element back. So what's wrong with it?
You're repeating the second capturing group:
(?:
\/
(
[[:alnum:]\_]*
)
)+
On each repetition of the outer non-capturing group, the contents of the inner capturing group are overwritten, which is the reason why only the last match is preserved. This is standard behavior across all regex engines.
(?=(^.*$)|(?:\/(.*?)(?:\/|})))
Try this.See demo.
http://regex101.com/r/lS5tT3/3
Each subsequent match of the same capture group will overwrite the previous one; that's why you end up with just b.
What I would suggest in this case is to match the whole block first and then use a simpler explode() to dig out the inner data; use this expression:
|{{{\/([[:alpha:]][[:alnum:]\_]*\:[[:alpha:]][[:alnum:]\_]*(?:\/[[:alnum:]\_]*)+)}}}|U
Then, with the resulting $matches array (third argument to preg_match()):
$data = explode('/', $matches[1]);
Your pattern is complete overkill for something that should be quite simple:
$rex = "#[{]{3}/(\w+:\w+)/(\w)/(\d)/(\w)[}]{3}#";
$str = "{{{/foo:bar/a/0/b}}}";
preg_match($rex, $str, $res);
Result:
Array
(
[0] => {{{/foo:bar/a/0/b}}}
[1] => foo:bar
[2] => a
[3] => 0
[4] => b
)
I have a array like below
Array
(
[0] => Array
(
[0] => Date
[1] => Name
[2] => Hours
)
[1] => Array
(
[0] => 2013-01-02
[1] => Test User
[2] => 7:59
)
[2] => Array
(
[0] => 2013-01-03
[1] => Test User
[2] => 7:53
)
[3] => Array
(
[0] => 2013-01-04
[1] => Test User
[2] => 8:12
)
.
.
.
.
[16] => Array
(
[0] =>
[1] => Total
[2] => 103:1
)
[17] => Array
(
[0] =>
)
)
And want to remove last item from array, I have tried array_pop but this is not working after passing above array to array_pop gives me output
Array
(
[0] =>
)
How can I achieve this with minimum code.
Try:
unset ($array_name[count($array_name)-1]);
$callback = function(&$array) { array_pop($array); };
array_walk($array, $callback);
This will pop the last element from each triplet.
Try like
$my_cnt = count($my_arr);
unset($my_arr[$my_cnt-1]);
if all your arrays are indexed by numbers from zero to max without any breaks, you can use
unset($ar[count($ar)-1]);
otherwise try
end($ar);
unset($ar[key($ar)]);
You are seeing the "popped" element instead of the modified array.
array_pop() returns the data in the element that it removes from the array.
This means that you wrote:
print_r(array_pop($array));
Instead, modify the array with array_pop(), then print the array.
array_pop($array);
print_r($array);