how to explode array within array in php - php

i have an array contain value like :
Check_Value ('level_codes', '1000', '1001','(1000,1002,1004)', 'DO ', '1')==1
i want to get values and store into an other parametrs like :
$codes = level_codes;
$first_value = 1000;
$second_value = 1001;
$list_values = (1000,1002,1004);
$action = DO;
$timer = 1;
$case = ==;
$status = 1;
is there any one please help me to do this work...!

Use the list() function: http://php.net/list

If the order is always the same, the following will reassign the values to the variables you want:
$parts = preg_split("/'?\(|\)'?/", $original_values);
$sub1 = explode(',', $parts[1]);
$codes = trim(str_replace("'", '', $sub1[0])) ;
$first_value = trim(str_replace("'", '', $sub1[1]));
$second_value = trim(str_replace("'", '', $sub1[2]));
$list_values = "($parts[2])";
$sub2 = explode(',', $parts[3]);
$action = trim(str_replace("'", '', $sub2[1]));
$timer = trim(str_replace("'", '', $sub2[2]));
preg_match('/(\D+)(\d+)/', $parts[4], $matches);
$case = $matches[1];
$status = $matches[2];
There might be a more elegant way to do it, but the original value is a string, rather than an array.

If the array is string, i think you should look into preg_match

Related

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

PHP- how to convert string to number?

How to convert this 19,500 string to number. If I do this
<?php
$val = "19,500";
$number = explode("," , $val);
$newnumber = $number[0].$number[1];
?>
But I don't think this is correct way.
LIVE DEMO
You can replace the string ',' to '' and then convert into integer by int
<?php
$number = "19,500";
$updatedNumber = str_replace(',','',$number);
echo (int)$updatedNumber ;
NOTE: int is better to use than intval function.
It reduces overhead of calling the function in terms of Speed.
http://objectmix.com/php/493962-intval-vs-int.html
Try this:
<?php
$val = "19,500";
$val = str_replace(',', '.', $val);
$number = (float)$val;
?>
UPDATED:
if comma comes out as a thousands-separator then:
<?php
$val = "19,500";
$val = str_replace(',', '', $val);
$number = (int)$val;
?>
you can simply replace ',' with ''
$newnumber = str_replace(',', '', $val);
the best way is:
<?php
$val = "19,500";
$tmp = str_replace(',', '', $val);
$newnumber = (float)$tmp;
?>
I think you just want to remove the ,:
<?php
$val = "19,500";
$val = strtr($val, array(',' => ''));
$number = intval($val);
var_dump($number);
use strtr() instead of str_replace() in case of multiple ,
When I see your code it's just sting string(5) "19500"
<?php
$val = "19,500";
$number = explode("," , $val);
$newnumber = $number[0].$number[1];
var_dump($newnumber);
?>
so you can convert to integer like the following
<?php
$val = "19,500";
$number = explode("," , $val);
$newnumber = $number[0].$number[1];
$realinteger = (int)($newnumber);
var_dump($realinteger);
?>
So the result will be int(19500)
Just try this code
$val = "19,500";
$newnumber = intval(str_replace(',', '', str_replace('.', '', $val))); // output : 19500
$val = "19,500.25";
$newnumber = intval(str_replace(',', '', str_replace('.', '', $val))); // output : 1950025
you can edit delimiter that want to replace with balnk string :)
Or you can try this
$newnumber = preg_match("/[^0-9,. -]/", $val) ? 0 : preg_replace("/[^0-9.-]/", "",$val);
just try one. simple as that :)
<?php
$val = "19,500";
$val = str_replace(',','', $val);
$newnumber = number_format($val,2);
?>
OUTPUT:
19,500.00
If you want to have no decimal you can change the 2 in 0. Like this.
OUTPUT:
19,500

How can i explode the string with , and |

How can i explode this? mars#email.com,123,12,1|art#hur.com,321,32,2
the output should be :
$email = mars#email.com
$score = 123
$street = 12
$rank = 1
then remove the |
$email = art#hur.com
$score = 321
$street = 32
$rank = 2
$string = mars#email.com,123,12,1|art#hur.com,321,32,2
explode( ',', $string );
is that correct?
foreach(explode('|', $str) as $v){
$data = explode(',',$v);
echo '$email = '.$data[0].
'$score = '.$data[1].
'$street = '.$data[2].
'$rank = '.$data[3];
}
You might want to use strtok() rather than explode().
http://www.php.net/manual/en/function.strtok.php
$arr = preg_split( '"[,|]"', 'mars#email.com,123,12,1|art#hur.com,321,32,2' );
$len = count($arr);
for( $i = 0; $i < $len; $i+=4 ) {
$email = $arr[$i];
$score = $arr[$i+1];
$street = $arr[$i+2];
$rank = $arr[$i+3];
}
you need to store the new array in variable >
$arr = explode(',',$string);
and I dont get what you want to do with the second part (after the |), but you can get the first par by doing this > $half = explode('|',$string)[0];
You need to unravel it in the right order:
first the blocks separated by |
then individual cells separated by ,
A concise way to do so is:
$array = array_map("str_getcsv", explode("|", $data));
Will give you a 2D array.
Use strtok and explode.
$tok = strtok($string, "|");
while ($tok !== false) {
list($email, $score, $street, $rank) = explode(',', $tok);
$tok = strtok(",");
}
I think what you want is something like this
$output = array();
foreach (explode('|', $string) as $person) {
$output[] = array(
'email' => $person[0],
'score' => $person[1],
'street' => $person[2],
'rank' => $person[3]
)
}
This stores all the results in a multidimensional array. For example, to print person 1's email, you'd use
echo $output[0]['email']; // mars#email.com
and to access person 2's street, you'd use
echo $output[1]['street']; // 32

PHP - Convert a string with dashes while removing first word

$title = '228-example-of-the-title'
I need to convert the string to:
Example Of The Title
How would I do that?
A one-liner,
$title = '228-example-of-the-title';
ucwords(implode(' ', array_slice(explode('-', $title), 1)));
This splits the string on dashes (explode(token, input)),
minus the first element (array_slice(array, offset))
joins the resulting set back up with spaces (implode(glue, array)),
and finally capitalises each word (thanks salathe).
$title = '228-example-of-the-title'
$start_pos = strpos($title, '-');
$friendly_title = str_replace('-', ' ', substr($title, $start_pos + 1));
You can do this using the following code
$title = '228-example-of-the-title';
$parts = explode('-',$title);
array_shift($parts);
$title = implode(' ',$parts);
functions used: explode implode and array_shift
$pieces = explode("-", $title);
$result = "";
for ($i = 1; $i < count(pieces); $i++) {
$result = $result . ucFirst($pieces[$i]);
}
$toArray = explode("-",$title);
$cleanArray = array_shift($toArray);
$finalString = implode(' ' , $cleanArray);
// echo ucwords($finalStirng);
Use explode() to split the "-" and put the string in an array
$title_array = explode("-",$title);
$new_string = "";
for($i=1; $i<count($title_array); $i++)
{
$new_string .= $title_array[$i]." ";
}
echo $new_string;

Remove Last String if its ', '

i have string like this
$string = 'aaaaaa, bbbbbb, cccccc, ';
and i want to modified it to be like this
$string = 'aaaaaa, bbbbbb, cccccc';
the last ',' and space is removed.
how to do this in php?
what is the function needed the achieve that?
my full code is like this
if(isset($_POST['accomodation'])) $accomodation = 'Accomodation, ';
if(isset($_POST['dance'])) $dance = 'Dance Lessons, ';
if(isset($_POST['vacation'])) $vacation = 'Vacation planning, ';
if(isset($_POST['group'])) $group = 'Group Vacation, ';
if(isset($_POST['inprivate'])) $inprivate = 'Private Vacation, ';
if(isset($_POST['land'])) $land = 'Land purchase/lease';
if(isset($_POST['all'])) $all = 'All';
#$interest = $accomodation.$dance.$vacation.$group.$inprivate.$land;
#echo $string;
*sorry for such dumb question, it's been so long i didn't touch native PHP programming
rtrim() function:
rtrim($string,', ');
but how are you defining the string? It may be that you can build it without the comma and space.
EDIT
$interests = array();
if(isset($_POST['accomodation'])) $interests[] = 'Accomodation';
if(isset($_POST['dance'])) $interests[] = 'Dance Lessons';
if(isset($_POST['vacation'])) $interests[] = 'Vacation planning';
if(isset($_POST['group'])) $interests[] = 'Group Vacation';
if(isset($_POST['inprivate'])) $interests[] = 'Private Vacation';
if(isset($_POST['land'])) $interests[] = 'Land purchase/lease';
if(isset($_POST['all'])) $all = 'All';
$interest = implode(', ',$interests);
echo $interest;
$string = preg_replace('/\s*,\s*$/', '', $string);
or, way cooler:
$string = rtrim($string, " ,");
Note that it does not matter the order of the characters in the pattern string.
#You last update.
This changes some things. You could put all your variables in one array and then implode it. Like so:
$items = array();
$items[] = $accomodation = 'Accomodation';
$items[] = $dance = 'Accomodation';
...
$result = implode(', ', $items)
$string = preg_replace( "/,\s*$/","",$string);
Should do the trick
Is it always a comma then a space at the end?
substr($string, 0, -2)
Often times, you can avoid the trailing comma altogether by changing the way you build the string. For example:
$count = 0;
foreach ($this as $that) {
if ($count != 0) {
$string .= ',';
}
$string .= $that['stuff'];
$count++;
}
Would remove the possibility of any trailing comma at the end, no matter the combination of results.

Categories