For Loop Formulae - php

I have the following and would want another result.
String = A,B,C,D
Trying to get an array of
A
A,B
A,B,C
A,B,C,D
My current code
$arrayA = explode(',', $query);
$arrSize = count($arrayA);
for ($x=0; $x<$arrSize; ++$x) {
for ($y=0; $x==$y; ++$y) {
array_push($arrayB,$arrayA[$x]);
$y=0;
}
}

Here's one way to do it using array_slice:
$str = 'A,B,C,D';
$strArr = explode(',', $str);
$newArr = array();
for($i=1; $i<=sizeof($strArr); $i++)
{
$newArr[] = implode( ',' , array_slice($strArr, 0, $i) );
}
print_r($newArr);
Outputs:
Array
(
[0] => A
[1] => A,B
[2] => A,B,C
[3] => A,B,C,D
)
Online demo here.

$test="a,b,c,d,e";
$arrayA = explode(',', $test);
$res=array();
$aux=array();
foreach($arrayA as $c){
$aux[]=$c;
$res[]=$aux;
}
i have not tested yet, but i think u catch the idea ;)

Related

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

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

php array_push and array_intersect

I want to add value to array and then I want to use these arrays in array intersect. Codes are in bellow. Where am I doing mistake?
$array =['1,2,3,4','3,4,5','2,3'];
$arr2 = [];
$common = [];
for($i=0; $i<count($array); $i++)
{
$arr1 = [];
if($i==0)
{
array_push($arr1, $array[$i]);
array_push($arr2, $array[$i]);
$common = array_intersect($arr1,$arr2);
}
else
{
array_push($arr1, $array[$i]);
$common = array_intersect($arr1,$common);
}
print_r($common);
}
Output is :
Array (
[0] => 1,2,3,4
)
Array ( )
Array ( )
I want to be this :
Array (
[0] => 1,2,3,4
)
Array(
[0] => 3,4
)
Array(
[0] => 3
)
Thanks,
Try This
<?php
$array =['1,2,3,4','3,4,5','2,3'];
$arr1 = [];
for($i=0; $i<count($array); $i++)
{
$j='arr'.$i;
$j= [];
if($i==0){
array_push($j, $array[$i]);
}
else{
$a = explode(',',$array[$i-1]);
$b = explode(',',$array[$i]);
$c = array_intersect($a,$b);
$d= implode(',',$c);
array_push($j, $d);
}
echo "<pre>"; print_r($j);
}
You are misusing array_intersect. This method does works on values in an array not on a single value. To use it the way You want You should split your values by comma and insert them as separate values. For example:
value: '1,2,3,4' should be inserted as:
$array = ['1', '2', '3', '4'];
Solution (without loops etc):
<?php
$array =['1,2,3,4','3,4,5','2,3'];
$arr1 = array();
$arr2 = array();
$common = array();
$arr1 = explode(',', $array[0]);
$arr2 = explode(',', $array[1]);
$common =array_intersect($arr1, $arr2);
print_r($common);
$arr3 = explode(',', $array[2]);
$common2 = array_intersect($common, $arr3);
print_r($common2);
?>

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