I did this from the database.
how to find the the $dependency separator after explode i want explode that array .
$dependency = 2/3&3/6;
$find = explode('&',$dependency);
string(7) "2/3&3/6"
I am getting like,
sarray(2) {
[0]=>string(3) "2/3"
[1]=>string(3) "3/6"
}
But i want the result to be like this:
sarray(2) {
[0]=>array(2) {
[0]=>string(1) "2"
[1]=>string(3) "3"
}
[1]=>array(2) {
[0]=>string(1) "3"
[1]=>string(3) "6"
}
}
please help to find the this array separator.
You need read the array using foreach then explode again to get the desired result.
$dependency = '2/3&3/6';
$find = explode('&',$dependency);
$result = array();
foreach($find as $val){
$result = array_merge($result,explode("/",$val));//Store all the values in one array
or
$result[]=explode("/",$val); //store array as key
}
var_dump($result);
Here you go first explode with '&' then with '/' on each array item
$str = "2/3&3/6";
$arr= explode('&',$str);
foreach($arr as $val){
$arrData[]= explode('/',$val);
}
echo "</pre>";
print_r($arrData);
You have to further use foreach to separate string again
$dependency = "2/3&3/6";
$find = explode('&',$dependency);
$new_array=array();
foreach ($find as $key => $value) {
$new_array[]=explode('/',$value);
}
var_dump($new_array);
OUTPUT:
array (size=2)
0 =>
array (size=2)
0 => string '2' (length=1)
1 => string '3' (length=1)
1 =>
array (size=2)
0 => string '3' (length=1)
1 => string '6' (length=1)
Related
I have an code from database like
$exp = ukuran:33,34,35;warna:putih,hitam;
i want to make an array like
$ukuran = array("33", "34", "35");
$warna = array("putih","hitam");
i have try to use explode but i have trouble result.
explode(";",$exp);
the result like
Array
(
[0] => ukuran:33,34,35
[1] => warna:putih,hitam
[2] =>
)
Anyone can help me, how to explode this case please?
$string = 'ukuran:33,34,35;warna:putih,hitam;';
$string = str_replace(['ukuran:', 'warna:'], '', $string);
$exploded = explode(';', $string);
$ukuran = explode(',', $exploded[0]);
$warna = explode(',', $exploded[1]);
If you want to do it dynamically you can't create variables but you can create an array with the types as keys:
$string = 'ukuran:33,34,35;warna:putih,hitam;';
$exploded = explode(';', $string);
$keysAndValues = [];
foreach($exploded as $exp) {
if (strlen($exp) > 0) {
$key = substr($exp, 0, strpos($exp, ':' ) );
$values = substr($exp, strpos($exp, ':' ) + 1, strlen($exp) );
$values = explode(',', $values);
$keysAndValues[$key] = $values;
}
}
This will output:
array (size=2)
'ukuran' =>
array (size=3)
0 => string '33' (length=2)
1 => string '34' (length=2)
2 => string '35' (length=2)
'warna' =>
array (size=2)
0 => string 'putih' (length=5)
1 => string 'hitam' (length=5)
Call them like this:
var_dump($keysAndValues['ukuran']);
var_dump($keysAndValues['warna']);
I would personally say continue as you are, then with your array in your result, loop through each item in the array and store it like so
$string = 'ukuran:33,34,35;warna:putih,hitam;';
$exploded = explode(';',$string);
$master = [];
foreach($exploded as $key => $foo){
$namedKey = explode(':',$foo);
$bar = substr($foo, strpos($foo, ":") + 1); //Get everything after the ; character
$master[$namedKey[0]] = explode(",",$bar);
}
This should return a result something along the lines of
array(3) {
["ukuran"]=>
array(3) {
[0]=>
string(2) "33"
[1]=>
string(2) "34"
[2]=>
string(2) "35"
}
["warna"]=>
array(2) {
[0]=>
string(5) "putih"
[1]=>
string(5) "hitam"
}
}
You can use regex to match words and numbers separate.
Then use array_filter to remove the empty values.
$exp = "ukuran:33,34,35;warna:putih,hitam";
Preg_match_all("/(\d+)|([a-zA-Z]+)/", $exp, $matches);
Unset($matches[0]);
$matches[1] = array_filter($matches[1]);
$matches[2] = array_filter($matches[2]);
Var_dump($matches);
https://3v4l.org/cREn7
Actually there are many ways to do what you want, But If I were you then I'll try this way :)
<?php
$exp = 'ukuran:33,34,35;warna:putih,hitam';
$result = explode(';',$exp);
foreach($result as $k=>$v){
$key_value = explode(':',$v);
// this line will help your to treat your $ukuran and $warna as array variable
${$key_value[0]} = explode(',',$key_value[1]);
}
print '<pre>';
print_r($ukuran);
print_r($warna);
print '</pre>';
?>
DEMO: https://3v4l.org/BAaCT
I have an array with corresponding value.
Array
(
[0] => BBsma=200
[1] => SMAperiod=300
[2] => SMA1=400
[3] => SMA2=500
[4] => EMAperiod=300
[5] => EMA1=24
[6] => EMA2=8
)
Now I want to match a certain string like for example BBsma that should return 200. Any help?
Got the array using these codes.
$txt = file_get_contents('INDICATORS.txt');
$rows = explode("\n", $txt);
array_shift($rows);
INDICATORS.txt content
BBperiod=100
BBsma=200
SMAperiod=300
SMA1=400
SMA2=500
EMAperiod=300
EMA1=24
EMA2=8
After you explode your text to the lines use this code:
for($i=0;$i<sizeof($rows);$i++)
{
$temp=explode("=",$rows[$i]);
if(sizeof($temp)==2)
{
$arr[$temp[0]]=$temp[1];
}
}
You will have named array in $arr
if you want to cast second part to int, you just change 6-line to this:
$arr[$temp[0]]=intval($temp[1]);
You could iterate over every line of your array and find the value with a regular match.
Code:
$txt = file_get_contents('INDICATORS.txt');
$rows = explode("\n", $txt);
/*
$rows = [
"BBsma=200",
"SMAperiod=300",
"SMA1=400",
"SMA2=500",
"EMAperiod=300",
"EMA1=24",
"EMA2=8",
];
*/
foreach ($rows as $k=>$v) {
if (preg_match("/(BBsma|SMAperiod|EMAperiod)=([0-9]+)/", $v, $matches)) {
echo "found value " . $matches[2] . " for entry " . $matches[1] . " in line " . $k . PHP_EOL;
}
}
Output:
found value 200 for entry BBsma in line 0
found value 300 for entry SMAperiod in line 1
found value 300 for entry EMAperiod in line 4
You can explode by new line as PHP_EOL like this
$col = "BBsma";
$val = "";
foreach(explode(PHP_EOL,$str) as $row){
$cols = explode("=",$row);
if(trim($cols[0]) == $col){
$val = $cols[1];
break;
}
}
echo "Value $col is : $val";
Live Demo
If your going to use the array a few times, it may be easier to read the file into an associative array in the first place...
$rows = [];
$file = "INDICATORS.txt";
$data = file($file, FILE_IGNORE_NEW_LINES);
foreach ( $data as $item ) {
$row = explode("=", $item);
$rows [$row[0]] = $row[1];
}
echo "EMA1 =".$rows['EMA1'];
This doesn't do the array_shift() but not sure why it's used, but easy to add back in.
This outputs...
EMA1 =24
I think that using array filter answers your question the best. It returns an array of strings with status code 200. If you wanted to have better control later on and sort / search through codes. I would recommend using array_walk to create some sort of multi dimensional array. Either solution will work.
<?php
$arr = [
"BBsma=200",
"SMAperiod=300",
"SMA1=400",
"SMA2=500",
"EMAperiod=300",
"EMA1=24",
"EMA2=8",
];
$filtered = array_filter($arr,"filter");
function filter($element) {
return strpos($element,"=200");
}
var_dump($filtered); // Returns array with all matching =200 codes: BBSMA=200
Output:
array (size=1)
0 => string 'BBsma=200' (length=9)
Should you want to do more I would recommend doing something like this:
///////// WALK The array for better control / manipulation
$walked = [];
array_walk($arr, function($item, $key) use (&$walked) {
list($key,$value) = explode("=", $item);
$walked[$key] = $value;
});
var_dump($walked);
This is going to give you an array with the parameter as the key and status code as it's value. I originally posted array_map but quickly realized array walk was a cleaner solution.
array (size=7)
'BBsma' => string '200' (length=3)
'SMAperiod' => string '300' (length=3)
'SMA1' => string '400' (length=3)
'SMA2' => string '500' (length=3)
'EMAperiod' => string '300' (length=3)
'EMA1' => string '24' (length=2)
'EMA2' => string '8' (length=1)
Working with the array becomes a lot easier this way:
echo $walked['BBsma']; // 200
$anything = array("BBsma"=>"200", "SMAperiod"=>"300", "SMA1"=>"400");
echo "the value is " . $anything['BBsma'];
This will return 200
I fetch a record from database.
In that I have an array.
$new_val = explode(',',$param->arg_2);
When I var_dump it I get this:
0 => string 'Profile1' (length=8)
1 => string 'Profile2' (length=8)
2 => string 'Profile3' (length=8)
How Can I Get this in var_dump :
Profile1 => string 'Profile1' (length=8)
Profile2 => string 'Profile2' (length=8)
Profile3 => string 'Profile3' (length=8)
After the code:
$new_val = explode(',',$param->arg_2);
Add:
$new_val = array_combine($new_val, array_values($new_val));
Try this
$new_array=array();
foreach($new_val as $nv)
{
$new_array[$nv]=$nv;
}
var_dump($new_array);
Try this:
$array = array('Profile 1', 'Profile 2', 'Profile 3'); //its your exploded string
$newArray = array();
foreach($array as $key => $value)
$newArray[$value] = $value;
var_dump($newArray);
And result is:
array(3) {
["Profile 1"]=>
string(9) "Profile 1"
["Profile 2"]=>
string(9) "Profile 2"
["Profile 3"]=>
string(9) "Profile 3"
}
array_combine — Creates an array by using one array for keys and another for its values
Try this
$array = explode(',',$param->arg_2);
$names = array_combine($array, $array);
var_dump($names);
$arr = array(0 => 'Profile1',
1 => 'Profile2',
2 => 'Profile3');
$vals = array_values($arr);
var_dump(array_combine($vals, $arr));
should output
array(3) { ["Profile1"]=> string(8) "Profile1" ["Profile2"]=> string(8) "Profile2" ["Profile3"]=> string(8) "Profile3" }
While searching thorught PHP guide I came across this and eve this helped me:
eval("\$new_val = array (".$param->arg_2.");");
I have an array an add it in foreach loop my code is:
foreach($array as $a) {
echo var_dump($a);
}
and my output is:
how can set my output to this:
and then show count of each value in my array. how can set this?
Stony answer is good. But I realize you want to echo the length of the value that you have. So I just expanded Stony's answer.
$myarray = array();
foreach($array as $a) {
$myarray[] = $a;
}
foreach($myarray as $key => $value) {
echo "Length of $value at index $key is " . strlen($value) . " <br/> ";
}
Note that I use PHP function strlen to calcualate the length of the string. Hope this helps. Thank you.
Using FOR loop u can do this like
for($i=0;$i<=sizeof($array);$i++)
{
echo $i."=>".$array[$i]."(length=".sizeof($array[$i]).")";
}
Try this code,
$arr = array("value1", "value2", "value3", "value4");
$newArr = array();
foreach ($arr as $val) {
$newArr [] = "string '$val' (length=".strlen($val).")";
}
print_r($newArr);exit;
Expected output:
Array
(
[0] => string 'value1' (length=6)
[1] => string 'value2' (length=6)
[2] => string 'value3' (length=6)
[3] => string 'value4' (length=6)
)
Try Demo
I'm programming in php for years, but i have encountered a redicilous problem and have no idea why it has happened. I guess i'm missing something, but my brain has stopped working!
I have an ass. array and when i var_dump() it, it's like this:
array
0 =>
array
4 => string '12' (length=2)
1 =>
array
2 => string '10' (length=2)
2 =>
array
1 => string '9' (length=1)
I want to do something with those values like storing them in an array, (12, 10, 9), but I dont know how to retrieve it! I have tested foreach(), array_value(), but no result. No matter what i do, the var_dump() result is still the same!
by the way i'm using codeigniter framework, but logically it should have nothing to do with the framework
thanks guys
You can try using array_map
$array = array(
0 => array(4 => '12'),
1 => array(2 => '10'),
2 => array(1 => '9'));
$array = array_map("array_shift", $array);
var_dump($array);
Output
array
0 => string '12' (length=2)
1 => string '10' (length=2)
2 => string '9' (length=1)
You can access them like this:
$array[0][4]= '13';
$array[1][2]= '11';
$array[2][1]= '10';
var_dump($array); gives this result:
array(3) {
[0]=> array(1) {
[4]=> string(2) "13" }
[1]=> array(1) {
[2]=> string(2) "11" }
[2]=> array(1) {
[1]=> string(2) "10" }
}
like this:
for ($i = count($array) ; $i >= 0 ; $i--) {
foreach($array[$1] as $k => $v) {
echo $k . "=>".$v;
}
}
If you want them to end up in an array, declare one, then iterate over your items.
$arr = new array();
foreach ($arrItem in $yourArray) {
foreach ($innerArrItem in $arrItem) {
array_push($arr, $innerArrItem);
}
}
print_r($arr);
If you have an unknown depth, you can do something like this:
$output = array();
array_walk_recursive($input, function($value, $index, &$output) {
$output[] = $value;
}, $output);