Can we do multiple explode array on one line - php

multiple Array Explode,i Want Output Using Explode This Type
1,1000,AA
2,2000,BB
3,3000,CC
<?php
$data1= "1,2,3";
$data2= "1000,2000,3000";
$data3= "AA,BB,CC";
$array = explode(',', $data1);
foreach ($array as $data1)
{
echo $data1;
echo $data2;
echo $data3."<br>";
}
?>
multiple Array Explode
I Want Output Using Explode
1 1000 AA
2 2000 BB
3 3000 CC

All you need to do is explode each of your original data items into an array and then process one array using the index to also reference the other 2 arrays.
<?php
$data1= "1,2,3";
$data2= "1000,2000,3000";
$data3= "AA,BB,CC";
$arr1 = explode(',', $data1);
$arr2 = explode(',', $data2);
$arr3 = explode(',', $data3);
foreach ( $arr1 as $key => $val ) {
echo sprintf( '%s %s %s<br>', $val, $arr2[$key], $arr3[$key] );
}
RESULTS:
1 1000 AA<br>2 2000 BB<br>3 3000 CC<br>
Or if seen in a browser
1 1000 AA
2 2000 BB
3 3000 CC

Use this code it will help you
If there is space between string (After AA,BB) then use space (" ") in explode and if there is new line then use \n in explode
<?php
$str = "1,1000,AA 2,2000,BB 3,3000,CC";
$arr = explode(" ",$str);
print_r($arr);
$arr1 = array();
$arr2 = array();
$arr3 = array();
foreach($arr as $val){
$value = explode(",",$val);
$arr1[] = $value[0];
$arr2[] = $value[1];
$arr3[] = $value[2];
}
print_r($arr1);
print_r($arr2);
print_r($arr3);
?>

$data1= "1,2,3";
$data2= "10,20,30";
$data3= "100,200,300";
$arr1 = explode(',', $data1);
$arr2 = explode(',', $data2);
$arr3 = explode(',', $data3);
foreach ( $arr1 as $key => $val ) {
echo $val;
echo $arr2[$key];
echo $arr3[$key];
}

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);
}

PHP Merge with separators

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;
?>

Php three array merge combine

$arr1 = array(1,2,3);
$arr2 = array("a","b","c");
$arr3 =array("1a","2b","3c");
How can I do the following ?
print
$one = 1,a,1a
$two = 2,b,2b
$three = 3,c,3c
Use array_map() function to map through all arrays at the same time. Like this :
$array_merged = array_map(function($v1,$v2,$v3) {
return $v1 . ','. $v2 . ',' . $v3;
}, $arr1, $arr2, $arr3);
/*
Array (
[0] => "1,a,1a",
[1] => "2,b,2b",
[2] => "3,c,3c",
)
*/
You can try this:
$arr1 = array(1,2,3);
$arr2 = array("a","b","c");
$arr3 = array("1a","2b","3c");
$i = 0;
foreach ($arr1 as $key => $value) {
$newArr[] = $value.",".$arr2[$i].",".$arr3[$i];
$i++;
}
echo implode("<br/>", $newArr);
Result:
1,a,1a
2,b,2b
3,c,3c
You can also perform this by using for loop.
Try this:
$arr1 = [1,2,3];
$arr2 = ["a","b","c"];
$arr3 = ["1a","2b","3c"];
$matrix = [$arr1, $arr2, $arr3];
var_dump(transpose($matrix));
function transpose($matrix) {
return array_reduce($matrix, function($carry, $item) {
array_walk($item, function ($value, $key) use (&$carry) {
$carry[$key][] = $value;
});
return $carry;
});
}

PHP Convert Array To An Associative Array

How do I convert the array
Array
(
[1] => a,b,c
[2] => x,y,z
)
into an associative array like
Array
(
[a]=> b,c
[x]=> y,z
)
Basically want to convert value of an array into a key.
How about this:
$arr = array('a,b,c','x,y,z');
$newArr = array();
foreach($arr as $key => $value) {
$value = explode(",",$value);
$firstValue = $value[0];
array_shift($value);
$newArr[$firstValue] = implode(",",$value);
}
print_r($newArr); //Array ( [a] => b,c [x] => y,z )
A faster solution:
foreach($array as $item){
$x = explode(',',$item);
$new_array[$x[0]] = implode(','array($x[1],$x[2]));
}
print_r($new_array);
Try out this,
$newArray = array();
foreach($array as $data){
$values = explode(",",$data);
$key = array_shift($values);
$newArray[$key] = implode($values,",");
}
print_r($newArray);
DEMO.
Do this:
$myArray=array(1=>'a,b,c', 2=>x,y,z);
foreach($myArray as $val){
$Xval=explode(",",$val);
$newKey=$Xval[0];
unset($Xval[0]);
$newArray[$newKey]=implode(",",$Xval);
}
Try like
$res = array();
foreach($my_arr as $value)
{
$my_var[] = explode(',',$value);
$i = 0;
foreach($my_var as $ky)
{
if($i++ != 0)
$exp_arr[] = $ky;
}
$res[$my_var[0]] = implode(',',$exp_arr);
}
or you can unset like
foreach($my_arr as $value)
{
$my_var[] = explode(',',$value);
$temp = $my_var[0];
unset($my_var[0]);
$res[$temp] = implode(',',$my_var);
}
try this
<?php
$array=array('a,b,c', 'x,y,z');
foreach($array as $key=>$val)
{
$array[substr($val,0,1)]=substr($val,2);
unset($array[$key]);
}
print_r($array);
?>
See Demo

php 2 foreach result mixed

I have 2 array datas, ("a","b","c") and ("x","y","z"), how to mixed them and out put a result as (ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)? (each $array1 + $array2 combine into a new words).
$array1 = array("a","b","c");
foreach($array1 as $data1){
}
$array2 = array("x","y","z");
foreach($array2 as $data2){
}
//$output = '(ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)';
$array = array();
$array1 = array("a","b","c");
$array2 = array("x","y","z");
foreach($array1 as $data1){
foreach($array2 as $data2){
$array[] = '('.$data1.$data2.')';
}
}
echo implode('|', $array);
<?php
$array1 = array("a","b","c");
$array2 = array("x","y","z");
$array3 = array();
foreach($array1 as $data1){
foreach($array2 as $data2){
$array3[] = "($data1$data2)";
}
}
echo implode('|', $array3);
// (ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)
You'll want to use a nested loop:
$items = array();
foreach($array1 as $a)
{
foreach($array2 as $b)
{
$items[] = '(' . $a . $b . ')';
}
}
echo implode('|', $items); // (ax)|(ay)|(az)|(bx)|(by)|(bz)|(cx)|(cy)|(cz)

Categories