string to array with desired keys [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert a string into list of arrays
I have a string code=AA&price=10&user_id=5.initially i exploded first with & and then by = but i getting.
array(
[0] =>code
[0] =>AA
[0] =>price
[0] =>10
[0] =>user_id
[0] =>5
)
My aim is to show
array(
[code] => AA
[price] => 10
[user_id] => 5
)

parse_str($str, $values);
var_dump($values);
You are parsing a URL encoded format, use the already existing parse_str to parse it.

$string = 'code=AA&price=10&user_id=5';
$params = array();
parse_str($string, $params);
print_r($params);

http://php.net/manual/en/function.parse-str.php
parse_str — Parses the string into variables
parse_str('code=AA&price=10&user_id=5', $outputArray);

Related

parse_url PHP_URL_QUERY get a l value [duplicate]

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 3 years ago.
with the URL
domain.com/index.php?option=component&ctrl=product&task=show&cid=5076
$parse = parse_url( $full_url, PHP_URL_QUERY);
echo $parse['ctrl']; // I get nothing
How can I get my values from keys (option, ctrl, task, and cid)?
You can use parse_str along with parse_url,
$full_url = "domain.com/index.php?option=component&ctrl=product&task=show&cid=5076";
parse_str(parse_url($full_url,PHP_URL_QUERY), $arr); // parse query string
print_r($arr);
Demo
Output
Array
(
[option] => component
[ctrl] => product
[task] => show
[cid] => 5076
)

Array key value to string in PHP [duplicate]

This question already has answers here:
Implode a column of values from a two dimensional array [duplicate]
(3 answers)
Closed 7 months ago.
I want to convert an array of this style:
Array ( [0] => Array ( [post_id] => 20752 ) [1] => Array ( [post_id] => 20753 ) )
into the string:
20752, 20753
How can I do this?
You can use PHP array_column function with combination of implode:
implode(', ', array_column($data, 'post_id'));

Php preg_match_all does not behave as expected [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
Trying to capture all text between tags.
Code:
$test = '<test>foo<tests> asdlkfjklas lkflsdakj <test>sdfsd<tests> asdlkaskl <test>235234<tests>';
$match = '/<test>(.*)<tests>/';
preg_match_all($match, $test, $nextLink);
Result of print_r:
Array ( [0] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) [1] => Array ( [0] => foo asdlkfjklas lkflsdakj sdfsd asdlkaskl 235234 ) )
your regex syntax is greedy. use folowing:
$match = '/<test>(.*?)<tests>/';

sort in explode variable [duplicate]

This question already has answers here:
PHP Sorting Array ASC
(3 answers)
Closed 6 years ago.
Hello everyone I have a small problem with the php sort I have basically a variable example
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
rsort($prova);
echo $prova[0];
but that's out 4,v Instead I would like so 1,x
Use sort() simply.
<?php
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova);
echo $prova[0]; // Prints 1,x
?>
See it working live
have a look at http://php.net/manual/en/function.sort.php, here you can use second parameter i.e. sort_flags
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova, SORT_STRING); //SORT_STRING - compare items as strings
print_r($prova);
sort($prova, SORT_NUMERIC); //SORT_NUMERIC - compare items numerically
print_r($prova);
output
Array
(
[0] => 1,x
[1] => 2,f
[2] => 22,a
[3] => 4,v
)
Array
(
[0] => 1,x
[1] => 2,f
[2] => 4,v
[3] => 22,a
)

PHP preg_split delimiter pattern, split at character chain [duplicate]

This question already has answers here:
Explode string by one or more spaces or tabs
(8 answers)
Closed 7 months ago.
With the following string:
$str = '["one","two"],a,["three","four"],a,,a,["five","six"]';
preg_split( delimiter pattern, $str );
How would I have to set up the delimiter pattern to obtain this result:
$arr[0] = '["one","two"]';
$arr[1] = '["three","four"]';
$arr[2] = '["five","six"]';
In other words, is there a way to split at the pattern ',a,' AND ',a,,a,' BUT check for ',a,,a,' first because ',a,' is a sub string of ',a,,a,'?
Thanks in advance!
If it can only be ,a, and ,a,,a,, then this should be enough:
preg_split("/(,a,)+/", $str);
It looks like what you're actually trying to do is separate out the square bracketed parts. You could do that like so:
$arr = preg_split("/(?<=\])[^[]*(?=\[)/",$str);
Take a look at this code:
$result = array();
preg_match_all("/(\[[^\]]*\])/", '["one","two"],a,["three","four"],a,,a,["five","six"]', $result);
echo '<pre>' . print_r($result, true);
It will return:
Array
(
[0] => Array
(
[0] => ["one","two"]
[1] => ["three","four"]
[2] => ["five","six"]
)
[1] => Array
(
[0] => ["one","two"]
[1] => ["three","four"]
[2] => ["five","six"]
)
)

Categories