PHP Merge with separators - php

I am trying to merge two strings in a specific way.
Essentially I need to merge the first two strings into a third with a pipe symbol between them then separated by commas:
$merge1 = "id1,id2,id3"
$merge2 = "data1,data2,data3"
Those two would become:
$merged = "id1|data1,id2|data2,id3|data3"
I hope this makes sense?

I mean there is no PHP function that can output what you need.
Code produced desired output could be
<?php
$merge1 = "id1,id2,id3";
$merge2 = "data1,data2,data3";
$merged = [];
$arr1 = explode(',', $merge1);
$arr2 = explode(',', $merge2);
foreach ($arr1 as $key => $val) {
$merged[] = $val . '|' . $arr2[$key];
}
echo implode(',', $merged);
// id1|data1,id2|data2,id3|data3

This script will help you
<?php
$merge1 = "id1,id2,id3";
$merge2 = "data1,data2,data3";
$merge1 = explode(",", $merge1);
$merge2 = explode(",", $merge2);
$final = [];
foreach ($merge1 as $index => $value) {
$final[] = $value . "|" . $merge2[$index];
}
$final = implode(",", $final);
print_r($final);
output
id1|data1,id2|data2,id3|data3

Try this.
<?php
$merge1 = "id1,id2,id3";
$merge2 = "data1,data2,data3";
$merge1 = explode(",",$merge1);
$merge2 = explode(",",$merge2);
$mergeArr = array_combine($merge1,$merge2);
$mergeStr = [];
foreach($mergeArr as $k => $v) {
$mergeStr[] = $k.'|'.$v;
}
$mergeStr = implode(",",$mergeStr);
echo $mergeStr;
?>

Related

Formating a txt file

I have a TXT file formatting that looks like:
123451234512345
123451234512345
I want to format the file with php in this format:.
12345-12345-12345
12345-12345-12345
This is what I have tried:
$codes = "codes.txt";
$unformatedCode = file($codes, FILE_IGNORE_NEW_LINES);
$abcd = "-";
foreach ($array as $value) {
$text = (string)$unformatedCode;
$arr = str_split($text, "");
$formatedCode = implode("-", $arr);
echo $formatedCode;
}
Try this,
<?php
$arr = array_map(
fn($v) => implode('-', str_split($v, 5)),
file("codes.txt", FILE_IGNORE_NEW_LINES)
);
print_r($arr);
Result:
Array
(
[0] => 12345-12345-12345
[1] => 12345-12345-12345
)
If you want to echo the result then do <?= implode(PHP_EOL, $arr) ?>
Possible that if can be written shorter
$codes = "codes.txt";
$array = file($codes, FILE_IGNORE_NEW_LINES);
$p = "/([1-9]{5})([1-9]{5})([1-9]{5})/i";
$r = "${1}-${2}-${3}";
foreach ($array as $a) {
echo preg_replace($p, $r, $a);
}

How to splitt a string to an array with keys and values using php

I have string and I want to splitt it to array and remove a specific part and then convert it back to a string .
$string = "width,100;height,8600;block,700;narrow,1;"
I want to search block in this string and remove it with its value "block,700;" and get the string as "width,100;height,8600;narrow,1;"
below is my code php which i tried.Please advice me
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$arr[] = $innerarr;
}
for ($i = 0; $i < count($arr); $i++) {
if (in_array($arr, 'block')) {
unset($arr[$i]);
}
}
Please note that "block" in aboive string will not always contain and the value may differ. So I can't use string replace . please advice
You essentially want to replace part of your string:
$string = "width,100;height,8600;block,700;narrow,1;";
$regex = '#(block,(.*?);)#';
$result = preg_replace($regex, '', $string);
echo $result;
Try this:
$string = "width,100;height,8600;block,700;narrow,1;"
$outerARR = explode(";", $string);
$arr = array();
foreach ($outerARR as $arrvalue) {
$innerarr = explode(",", $arrvalue);
$arr[$innerarr[0]] = $innerarr[1];
}
if (array_key_exists('block', $arr)) {
unset($arr['block']);
}

How to add characters into string

I have a string
$str = "a,b,c,d,e";
And I want convert string as:
$str_convert = "'a','b','c','d','e'";
What should I do?
Try my solution:
<?php
$str = "a,b,c,d,e";
$arr = explode(',',$str);
foreach ($arr as &$value) {
$value = "'$value'";
}
$str_convert= implode(',', $arr);
echo $str_convert;
Like this:
$str = "a,b,c,d,e";
$items = split(",", $str);
$convert_str = "";
foreach ($items as $item) {
$convert_str .= "'$item',";
}
$convert_str = rtrim($convert_str, ",");
print($convert_str);
If you would like a different solution using functional programming coding style, here it is:
<?php
$str = 'a,b,c,d,e';
$add_quotes = function($str, $func) {
return implode(',', array_map($func, explode(',', $str)));
};
print $add_quotes(
$str,
function ($a) {
return "'$a'";
}
);

How can I define dynamically the associative array keys?

I have two variable:
$keystr = 'plant,fruit,exotic';
$value='kiwi';
how can i create the associative array?
$arr = ('plant'=>array('fruit'=>array('exotic'=>'kivi')));
$keystr = 'plant,fruit,exotic';
$value='kiwi';
$arr = array();
$current = &$arr;
$keys = explode(',', $keystr);
foreach($keys as $key) {
$current[$key] = array();
$current = &$current[$key];
}
$current = $value;
unset($current);
var_dump($arr);
See http://ideone.com/YiMIRb for a demonstration

String to associative array

I'm having trouble using preg_match_all to split a string into key value pairs. An example of my string:
"%Title:Movie%Sortable%Writer:%Indexed:false%"
Where I expect results like:
$result['Title'] = 'Movie';
$result['Sortable'] = '';
$result['Writer'] = '';
$result['Indexed'] = 'false';
I can split the string using preg_match('/%/',$str,-1,PREG_SPLIT_NO_EMPTY); but it returns an indexed array. I need an associative array so that order is not important and I can use the key in a switch statement. What would be the correct regex to use in preg_match_all?
Try with:
$input = "%Title:Movie%Sortable%Writer:%Indexed:false%";
$output = array();
$data = explode('%', $input);
foreach ($data as $item) {
list($key, $value) = explode(':', $item);
$output[$key] = $value;
}
<?php
$arr = array();
$string = "%Title:Movie%Sortable%Writer:%Indexed:false%";
$d = explode('%', $string);
foreach($d as $item){
list($key,$value) = explode(':', $item);
$arr[$key] = $value;
}
print_r($arr);
?>

Categories