Split string with only one delimiting character into key-value pairs [duplicate] - php

This question already has answers here:
Convert backslash-delimited string into an associative array
(4 answers)
Closed 7 months ago.
I have a colon-delimited string like this:
"Part1:Part2:Part3:Part4"
With explode I can split the string into an array:
echo '<pre>';
print_r(explode(":", "Part1:Part2:Part3:Part4"));
echo '</pre>';
Output:
Array
(
[0] => Part1
[1] => Part2
[2] => Part3
[3] => Part4
)
But I need associative array elements like this:
Array
(
[Part1] => Part2
[Part3] => Part4
)
UPDATE
echo '<pre>';
list($key, $val) = explode(':', 'Part1:Part2:Part3:Part4');
$arr= array($key => $val);
print_r($arr);
echo '</pre>';
Result:
Array
(
[Part1] => Part2
)

Alternative solution using only array_* functions.
Collect all values that are going to be keys for the resultant array.
Collect all values that are going to be actual values for the resultant array.
Combine both using array_combine().
Snippet:
<?php
$str = "Part1:Part2:Part3:Part4";
$res = array_combine(
array_map(fn($v) => $v[0], array_chunk(explode(':',$str),2)),
array_map(fn($v) => $v[1], array_chunk(explode(':',$str),2))
);

Using a for loop, you could also use the modulo operator % and create the key in the if part.
The if clause will be true for the first item having the key first and then the value in the else part, toggeling the keys and the values.
$str = "Part1:Part2:Part3:Part4";
$parts = explode(':', $str);
$result = [];
for ($i = 0; $i < count($parts); $i++) {
$i % 2 === 0 ? $k = $parts[$i] : $result[$k] = $parts[$i];
}
print_r($result);
Output
Array
(
[Part1] => Part2
[Part3] => Part4
)
See a php demo

Try this
$string = "Part1:Part2:Part3:Part4";
$arr = explode(":", $string);
$key = "";
$i = 0;
$newArray = [];
foreach($arr as $row){
if($i == 0){
$key = $row;
$i++;
} else {
$newArray[$key] = $row;
$i = 0;
}
}
echo "<pre>";
print_r($newArray);
echo "</pre>";

Related

Get array from string in pattern - key1:val1,val2,..;key2:val1,

I would like to get from a string like this
color:blue,red;size:s
to an associative multiarray
[
color => [blue,red],
size => [s]
]
I tried with ([a-z]+):([a-z^,]+) but it's not enough; I don't know how to recursive it or something.
I wouldn't use regular expressions for something like this. Instead use explode() several times.
<?php
$str = 'color:blue,red;size:s';
$values = explode(';', $str);
$arr = [];
foreach($values as $val) {
$parts = explode(':', $val);
$arr[$parts[0]] = explode(',', $parts[1]);
}
Output:
Array
(
[color] => Array
(
[0] => blue
[1] => red
)
[size] => Array
(
[0] => s
)
)
$dataText = 'color:blue,red;size:s';
$data = explode(';', $dataText);
$outputData = [];
foreach ($data as $item){
$itemData = explode(':', $item);
$outputData[$itemData[0]] = explode(',', $itemData[1]);
}
print_r('<pre>');
print_r($outputData);
print_r('</pre>');
With regex is not so simple like explode, but you can try this...
$re = '/(\w+)\:([^;]+)/';
$str = 'color:blue,red;size:s';
preg_match_all($re, $str, $matches);
// Print the entire match result
$result = array();
$keys = array();
for($i = 1; $i < count($matches); $i++) {
foreach($matches[$i] as $k => $val){
if($i == 1) {
$result[$val] = array();
$keys[$k] = $val;
} else {
$result[$keys[$k]] = $val;
}
}
}
echo '<pre>';
print_r($result);
echo '</pre>';
result
Array
(
[color] => blue,red
[size] => s
)

How to find unique values in array

Here i want to find unique values,so i am writing code like this but i can't get answer,for me don't want to array format.i want only string
<?php
$array = array("kani","yuvi","raja","kani","mahi","yuvi") ;
$unique_array = array(); // unique array
$duplicate_array = array(); // duplicate array
foreach ($array as $key=>$value){
if(!in_array($value,$unique_array)){
$unique_array[$key] = $value;
}else{
$duplicate_array[$key] = $value;
}
}
echo "unique values are:-<br/>";
echo "<pre/>";print_r($unique_array);
echo "duplicate values are:-<br/>";
echo "<pre/>";print_r($duplicate_array);
?>
You can use array_unique() in single line like below:-
<?php
$unique_array = array_unique($array); // get unique value from initial array
echo "<pre/>";print_r($unique_array); // print unique values array
?>
Output:- https://eval.in/601260
Reference:- http://php.net/manual/en/function.array-unique.php
If you want in string format:-
echo implode(',',$final_array);
Output:-https://eval.in/601261
The way you want is here:-
https://eval.in/601263
OR
https://eval.in/601279
I am baffled by your selection as the accepted answer -- I can't imagine how it can possibly satisfy your requirements.
array_count_values() has been available since PHP4, so that is the hero to call upon here.
Code: (Demo)
$array = array("kani","yuvi","raja","kani","mahi","yuvi") ;
$counts = array_count_values($array); // since php4
foreach ($counts as $value => $count) {
if ($count == 1) {
$uniques[] = $value;
} else {
$duplicates[] = $value;
}
}
echo "unique values: " , implode(", ", $uniques) , "\n";
echo "duplicate values: " , implode(", ", $duplicates);
Output:
unique values: raja, mahi
duplicate values: kani, yuvi
To get unique values from array use array_unique():
$array = array(1,2,1,5,10,5,10,7,9,1) ;
array_unique($array);
print_r(array_unique($array));
Output:
Array
(
[0] => 1
[1] => 2
[3] => 5
[4] => 10
[7] => 7
[8] => 9
)
And to get duplicated values in array use array_count_values():
$array = array(1,2,1,5,10,5,10,7,9,1) ;
print_r(array_count_values($array));
Output:
Array
(
[1] => 3 // 1 is duplicated value as it occurrence is 3 times
[2] => 1
[5] => 2 // 5 is duplicated value as it occurrence is 3 times
[10] => 2 // 10 is duplicated value as it occurrence is 3 times
[7] => 1
[9] => 1
)
<?php
$arr1 = [1,1,2,3,4,5,6,3,1,3,5,3,20];
print_r(arr_unique($arr1)); // will print unique values
echo "<br>";
arr_count_val($arr1); // will print array with duplicate values
function arr_unique($arr) {
sort($arr);
$curr = $arr[0];
$uni_arr[] = $arr[0];
for($i=0; $i<count($arr);$i++){
if($curr != $arr[$i]) {
$uni_arr[] = $arr[$i];
$curr = $arr[$i];
}
}
return $uni_arr;
}
function arr_count_val($arr) {
$uni_array = arr_unique($arr1);
$count = 0;
foreach($uni_array as $n1) {
foreach($arr as $n1) {
if($n1 == $n2) {
$count++;
}
}
echo "$n1"." => "."$count"."<br>";
$count=0;
}
}
?>

i want to array key and array value comma separated string

I have this array and I want to convert it into string.
I try to get string from php implode() function but could not get the desired result.
The output I want is arraykey-arrayvalue,arraykey-arrayvalue,arraykey-arrayvalue and so on as long as array limit end.
Array ( [1] => 1 [2] => 1 [3] => 1 )
$data = implode(",", $pData);
//it is creating string like
$data=1,1,1;
// but i want like below
$string=1-1,2-1,3-1;
You could just gather the key pair values inside an array then implode it:
foreach($array as $k => $v) { $data[] = "$k-$v"; }
echo implode(',', $data);
You can also use array_map function as
$arar = Array ( '1' => 1 ,'2' => 1, '3' => 1 );
$result = implode(',',array_map('out',array_keys($arar),$arar));
function out($a,$b){
return $a.'-'.$b;
}
echo $result;//1-1,2-1,3-1;
This can be done using the below code:
$temp = '';
$val = '';
$i=0;
foreach ($array as $key => $value)
{
$temp = $key.'-'.$val;
if($i == 0)
{
$val = $temp; // so that comma does not append before the string starts
$i = 1;
}
else
{
$val = $val.','.$temp;
}
}

How to put comma separated string in two different arrays?

I want to put my string (separated by comma) in 2 different arrays.
$str = 1,First,2,Second,3,Third,4,Forth,5,Fifth
My goal is to group the numbers and the text.
$number = {1,2,3,4,5)
$text = {first, second, third, forth, fifth}
I tried to use explode(), but I only be ale to create a single array.
This should work for you:
First explode() your string by a comma. Then you can array_filter() the numbers out. And at the end you can simply take the array_diff() from the $arr and the $numbers array.
<?php
$str = "1,First,2,Second,3,Third,4,Forth,5,Fifth";
$arr = explode(",", $str);
$numbers = array_filter($arr, "intval");
$text = array_diff($arr, $numbers);
?>
You can use explode() and array_map() over the results:
$str = '1,First,2,Second,3,Third,4,Forth,5,Fifth';
array_map(function($item) use (&$numbers, &$strings) {
if(is_numeric($item)) {
$numbers []= $item;
} else {
$strings []= $item;
}
}, explode(',', $str));
var_dump($numbers, $strings);
<?php
$str = array(1,'First',2,'Second',3,'Third',4,'Forth',5,'Fifth');
$letters =array();
$no = array();
for($i=0;$i<count($str);$i++){
if($i%2 ==0){
$letters[] = $str[$i];
}else{
$no[] = $str[$i];
}
}
print_r($no);
print_r($letters);
?>
May be this can help you
$str = '1,First,2,Second,3,Third,4,Forth,5,Fifth';
$array = explode(',',$str);
$number = array();
$string = array();
foreach($array as $val)
{
if(is_numeric($val))
{
$number[] = $val;
}
elseif(!is_numeric($val))
{
$string[] = $val;
}
}
echo $commNum = implode(',',$number); // These are strings
echo '<br/>'.$commStr = implode(',',$string); // These are strings
echo '<pre>';
print_r($number); // These are arrays
echo '<pre>';
print_r($string); // These are arrays
This should work for you.
$str = '1,First,2,Second,3,Third,4,Forth,5,Fifth';
$result = explode(',',$str);
$number = array();
$text = array();
foreach(explode(',',$str) as $key => $value){
if($key % 2 == 1){
$text[] = $value;
}elseif($key % 2 == 0){
$number[] = $value;
}
}
print_r($number);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
print_r($text);//Array ( [0] => First [1] => Second [2] => Third [3] => Forth [4] => Fifth )
and if your array is not consistent in this manner then some of these above answers are well suited for you like of Rizier123,hek2mgl,& Sunil's one
I'm updating my answer for your correspondent comments over Adrian
foreach(explode(',',$str) as $key => $value){
if(is_numeric($value)){
$number[] = $value;
}else{
$text[] = $value;
}
}
You can use explode, but you need apply some filter in this case is is_numeric:
<?php
$str = '1,First,2,Second,3,Third,4,Forth,5,Fifth, erewwrw , 6 ';
$array = explode(',', $str);
$array_numbers = array();
$array_letters = array();
for($i = 0; $i < count($array); $i++) {
if(is_numeric(trim($array[$i]))) {
$array_numbers[] = trim($array[$i]);
} else {
$array_letters[] = trim($array[$i]);
}
}
print_r($array_numbers);
print_r($array_letters);
?>
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
[0] => First
[1] => Second
[2] => Third
[3] => Forth
[4] => Fifth
)
Assuming that your string contains number followed by string and so on,
following should be the solution.
Create two blank arrays: $numbers and $strings.
Just loop over the array and get even and odd elements.
Even elements should go to numbers array and odd elements should go to strings array.
$str = '1,First,2,Second,3,Third,4,Forth,5,Fifth';
$numbers = array();
$strings = array();
$temp = explode(',', $str);
$i=0;
foreach ($temp as $e) {
if ($i%2) {
$strings[] = $e;
}
else {
$numbers[] = $e;
}
++$i;
}
echo '<pre>';
print_r($numbers);
echo '</pre>';
echo '<pre>';
print_r($strings);
echo '</pre>';

preg_match a "shortcode" to create an array with semantic array keys

How would I preg_match the following piece of "shortcode" so that video and align are array keys and their values are what is with in the quotes?
[video="123456" align="left"/]
Here is another approach using array_combine():
$str = '[video="123456" align="left"/][video="123457" align="right"/]';
preg_match_all('~\[video="(\d+?)" align="(.+?)"/\]~', $str, $matches);
$arr = array_combine($matches[1], $matches[2]);
print_r() output of $arr:
Array
(
[123456] => left
[123457] => right
)
$string='[video="123456" align="left"/]';
$string= preg_replace("/\/|\[|\]/","",$string);
$s = explode(" ",$string);
foreach ($s as $item){
list( $tag, $value) = explode("=",$item);
$array[$tag]=$value;
}
print_r($array);
Alternate solution with named parameters:
$str = '[video="123456" align="left"/][video="123457" align="right"/]';
$matches = array();
preg_match_all('/\[video="(?P<video>\d+?)"\salign="(?P<align>\w+?)"\/\]/', $str, $matches);
for ($i = 0; $i < count($matches[0]); ++ $i)
print "Video: ".$matches['video'][$i]." Align: ".$matches['align'][$i]."\n";
You could also reuse the previous array_combine solution given by Alix:
print_r(array_combine($matches['video'], $matches['align']));
I don't think there's any (simple) way to do it using a single regex, but this will work in a fairly general way, picking out the tags and parsing them:
$s = 'abc[video="123456" align="left"/]abc[audio="123456" volume="50%"/]abc';
preg_match_all('~\[([^\[\]]+)/\]~is', $s, $bracketed);
$bracketed = $bracketed[1];
$tags = array();
foreach ($bracketed as $values) {
preg_match_all('~(\w+)\s*=\s*"([^"]+)"~is', $values, $pairs);
$dict = array();
for ($i = 0; $i < count($pairs[0]); $i++) {
$dict[$pairs[1][$i]] = $pairs[2][$i];
}
array_push($tags, $dict);
}
//-----------------
echo '<pre>';
print_r($tags);
echo '</pre>';
Output:
Array
(
[0] => Array
(
[video] => 123456
[align] => left
)
[1] => Array
(
[audio] => 123456
[volume] => 50%
)
)

Categories