How to create array using customize value? - php

I have some values below:
11 12 13.
I need to make an array using this value.
array(11,12,13);
I tried this code below :
$selected is the variable that contain the value 11 12 13 //Special Instruction
foreach($selected as $key=>$val)
{
$sel.=$val;
$sel.=",";
}
$str = rtrim($sel,',');
// echo $str;
$shortlist = array_map('trim', explode(',',$str));
I need help to make an array like array(11,12,13).Any idea?

You can use explode here. split is deprecated in latest version.
$str = "11 12 13";
$array = explode(" ",$str);

try str_split see http://www.php.net/manual/en/function.str-split.php
$str = "111213";
$array = str_split($str, 2);
print_r($array);
output :
Array
(
[0] => 11
[1] => 12
[2] => 13
)

$values = explode(' ', "11 12 13");
// if there is no space, you can do it like this
$strLen = strlen($string);
$i = 0;
while($i < $strLen) {
$myArr[] = substr($string, $i, 2);
$i += 2;
}
print_r($myArr);

$selected = "11 12 13";
print_r (explode(" ",$selected));

Related

How do i start text str_split after some characters

I have the code:
$txt = "lookSTARTfromhereSTARTagainhere";
$disp = str_split($txt, 4);
for ($b = 0; $b<3; $b++) {
echo "$disp[$b]";
}
which return 'look', 'STAR' 'Tfor' in a text line of 'lookSTARTfromhereSTARTagainhere' my problem is how do i start my text split from 'START' example my result output for text line of 'lookSTARTfromhereSTARTagainhere' after split look like 'from' 'here' 'again' thanks for your time and understanding
it may not be possible by str_split as 'again' has 5 characters. you can get 'from', 'here' by following code.
$txt = "lookSTARTfromhereSTARTagainhere";
$txt = str_replace('look','',$txt);
$txt = str_replace('START','',$txt);
$disp = str_split($txt, 4);
for ($b = 0; $b<3; $b++) {
echo "$disp[$b]";
}
how do i start my text split from 'START'
Simply with explode and array_slice functions:
$txt = "lookSTARTfromhereSTARTagainhere";
$result = array_slice(explode('START', $txt), 1);
print_r($result);
The output:
Array
(
[0] => fromhere
[1] => againhere
)
If your expected output is four letter words from start you can explode on START then remove first item and use str_split to split each array item to four letter words.
$txt = "lookSTARTfromhereSTARTagainhere";
$arr = explode("START", $txt); // Explode on START
unset($arr[0]); // first item is before START, we don't need that.
$res = [];
foreach($arr as $val){
$temp = str_split($val, 4); // Split array item on four letters.
$res = array_merge($res, $temp); // merge the new array with result array
}
var_dump($res);
https://3v4l.org/3BQ1b
<?php
$txt = "lookSTARTfromhereSTARTagainhere";
$split = explode("START",$txt);
unset($split[0]);
$first_str = str_split($split[1],4);
$t2 = str_split($split[2],5);
$second_str = $t2[0];
array_push($first_str,$second_str);
print_r($first_str);
?>
output
Array ( [0] => from [1] => here [2] => again )

How to split evenly and oddly a string to form an array of even and odd results OK Like

I have a php string formed by images and corresponding prices like OK Like
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
I know that if I do:
$myArray = explode(',', $myString);
print_r($myArray);
I will get :
Array
(
[0] => ddb94-b_mgr3043.jpg
[1] => 3800
[2] => 83acc-b_mgr3059.jpg
[3] => 4100
)
But How could I split the string so I can have an associative array of the form?
Array
(
"ddb94-b_mgr3043.jpg" => "3800"
"83acc-b_mgr3059.jpg" => "4100"
)
Easier way to do like below:-
<?php
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
$chunks = array_chunk(explode(',', $myString), 2); //chunk array into 2-2 combination
$final_array = array();
foreach($chunks as $chunk){ //iterate over array
$final_array[trim($chunk[0])] = trim($chunk[1]);//make key value pair
}
print_r($final_array); //print final array
Output:-https://eval.in/859757
Here is another approach to achieve this,
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100,test.jpg,12321";
$arr = explode(",",$myString);
$temp = [];
array_walk($arr, function($item,$i) use (&$temp,$arr){
if($i % 2 != 0) // checking for odd values
$temp[$arr[$i-1]] = $item; // key will be even values
});
print_r($temp);
array_walk - Apply a user supplied function to every member of an array
Here is your working demo.
Try this Code... If you will receive all the key and value is equal it will work...
$myString = "ddb94-b_mgr3043.jpg,3800,83acc-b_mgr3059.jpg,4100";
$myArray = explode(',', $myString);
$how_many = count($myArray)/2;
for($i = 0; $i <= $how_many; $i = $i + 2){
$key = $myArray[$i];
$value = $myArray[$i+1];
// store it here
$arra[$key] = $value;
}
print_r($arra);

php change the array value

I have an Array like this
$first = array("10.2+6","5.3+2.2");
I want to convert it like this
$second = array("10+10+6","5+5+5+2+2");
I also want to print out this such as way
10
10
6
5
5
5
2
2
How can I do this?
You can use this preg_replace_callback function:
$first = array("10.2+6", "5.3+2.2");
$second = preg_replace_callback('/\b(\d+)\.(\d+)\b/', function($m){
$_r=$m[1]; for($i=1; $i<$m[2]; $i++) $_r .= '+' . $m[1] ; return $_r; }, $first);
print_r($second);
Output:
Array
(
[0] => 10+10+6
[1] => 5+5+5+2+2
)
We use this regex /\b(\d+)\.(\d+)\b/ where we match digits before and after DOT separately and capture them in 2 captured groups. Then in callback function we loop through 2nd captured group and construct our output by appending + and 1st captured group.
Here's a solution using regular expressions and various functions. There are many ways to accomplish what you're asking, and this is just one of them. I'm sure this could even be improved upon, but here it is:
$first = array("10.2+5","5.3+2.2");
$second = array();
$pattern = '/(\d+)\.(\d)/';
foreach($first as $item){
$parts = explode('+',$item);
$str = '';
foreach($parts as $part){
if(strlen($str)>0) $str .= '+';
if(preg_match_all($pattern, $part, $matches)){
$str .= implode("+", array_fill(0,$matches[2][$i], $matches[1][$i]));
}else{
$str .= $part;
}
}
$second[] = $str;
}
print_r($second);
Output:
Array
(
[0] => 10+10+5
[1] => 5+5+5+2+2
)
<?php
$first = array("10.2+5","5.3+2");
foreach($first as $term)
{
$second="";
$a=explode("+", $term);
$b=explode(".", $a[0]);
$c=$b[0];
for ($i=0;$i<$b[1];$i++)
$second=$second.$c."+";
echo $second.$a[1]."+";
}
?>

How to count exploded array

My string looks like:
244.53.66=1,53.646.77=1,666.534.23=5,664.521.64=3,535.777.54=2,533.886=5,22.153=5,643.786=2
I exploded it like:
$string = "244.53.66=1,53.646.77=1,666.534.23=5,664.521.64=3,535.777.54=2,533.886=5,22.153=5,643.786=2";
$array = explode(",", $string);
$array2=array();
for($i = 0; $i<count($array); $i++)
{
array_push($array2, explode("=",$array[$i]));
}
now $array2 got these values:
$array2[0][0] = "244.53.66";
$array2[0][1] = "1";
$array2[1][0] = "53.646.77";
$array2[1][1] = "1";
etc.
How Can I count all values each type, example:
2 x 1 (two records with value 1), 1x1(1 record with value 1) itd.
I want to store this as array:
$array3[index] = "amount same values";
Can You help me?
You need first to get the IDs in a seperate array, then use array_count_valuesto count the number of the IDs.
Here's the final code:
<?php
$string = "244.53.66=1,53.646.77=1,666.534.23=5,664.521.64=3,535.777.54=2,533.886=5,22.153=5,643.786=2";
$array = explode(",", $string);
$array2=array();
for($i = 0; $i<count($array); $i++)
{
array_push($array2, explode("=",$array[$i]));
}
$array3 = array();
//Iterate through array 2 to extract all IDs
for($i = 0; $i<count($array2); $i++)
{
array_push($array3, $array2[$i][1]);
}
$vals = array_count_values($array3);
print_r($vals);
?>
Which outputs
Array
(
[1] => 2
[5] => 3
[3] => 1
[2] => 2
)
Happy coding on StackOverflow !

php splitting large numbers (like explode)

i need the functionality of php explode(), but without the separators.
for example, turning the variable "12345" into an array, holding each number seperately.
is this possible? i've already googled but only found explode(), which doesn't seem to work.
thanks!
with any string in php:
$foo="12345";
echo $foo[0];//1
echo $foo[1];//2
//etc
or (from the preg_split()) page in the manual
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
EVEN BETTER:
$str = 'string';
$chars=str_split($str, 1)
print_r($chars);
benchmark of preg_split() vs str_split()
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$str = '12345';
$time_start = microtime_float();
for ($i = 0; $i <100000; $i++) {
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
//$chars=str_split($str, 1);
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "$time seconds\n";
results:
str_split =0.69
preg_split =0.9
If you actually want to create an array, then use str_split(), i.e.,
echo '<pre>'. print_r(str_split("123456", 1), true) .'</pre>';
would result in
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Your number can be turned into string and then acted like an array
$i = 2342355; $i=(string)$i;
//or
$i='234523452435234523452452452';
//then
$i[2]==4
//numeration started from 0

Categories