how to find array with regex [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have an array that depending of my input, will return different name for a particular node. For example below I the name of the key node is n1:ModemProduct1207 but it could be also n1:ModemProduct1308.
I was wondering is there is a way to have something like this:
$array ['n1:ModemProducts'] ['n1:ModemProduct'. (regex here like"n1:ModemProduct[\d0-9]{4}+")];
$modemProducts = $array ['n1:ModemProducts'] ['n1:ModemProduct1207'];
I tried couple of options but could not get it to work.

Little function for one-dimensional array:
function filterProducts($array, $pattern){
$result = array();
foreach($array as $key => $value){
if (preg_match($pattern, $key)){
$result[$key] = $value;
}
}
return $result;
}
Usage:
$results = filterProducts($array['n1:ModemProducts'], '/n1:ModemProduct[\d]{4}/');

$array = array('Armenia', 'America', 'Algeria', 'India', 'Brazil', 'Croatia', 'Denmark');
$fl_array = preg_grep('/^A.*/', $array);
print_r($fl_array);
for referance you can see . http://ask.amoeba.co.in/php-search-in-an-array-for-values-matching-a-pattern-regex-wildcard/
I am Trying to solve your problam
Hope this Help
Thanks
Anand

Related

One liner to find maximum from array in php for associative array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I looked at the question for maximum from associative array but I am looking for one liner. I have array like following,
$studenRsults=array(
array(
'roll'=>1,
'name'=>'jack Smit',
'marks'=>70
),
array(
'roll'=>4,
'name'=>'Sita',
'marks'=>50
),
array(
'roll'=>2,
'name'=>'Akhilesh',
'marks'=>80
),
array(
'roll'=>3,
'name'=>'jon',
'marks'=>50
),
);
Please suggest one liner thanks.
This needs php 5.5+ for array_column
$max = max(array_column($studenRsults, 'marks'));
or php 5.3+ for lamdas
$max = max(array_map(function($el){ return $el['marks']; }, $studenRsults));
Assuming you mean max of roll. But yeah, code quality isn't measured in how few lines you have...
foreach($studenRsults as $result) $maxResult = (isset($maxResult['roll']) && ($maxResult['roll'] > $result['roll'])) ? $maxResult : $result;
print_r($maxResult); // print the max roll student

i need Apostrophe in a variable result MYSQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I receive data in this format like:
C00001,C00302,C00303,C00287,C00286,C00285,C00017
in a variable but i need in this shape:
'C00001','C00302','C00303','C00287','C00286','C00285','C00017'
i am new in mysql kindly any help
You can explode the data, modify it, then re-implode it.
$data = 'C00001,C00302,C00303,C00287,C00286,C00285,C00017';
$arr = explode(',', $data);
$new_arr = array_map(function($a){
return "'{$a}'";
}, $arr);
$new_data = implode(',', $new_arr);

How to retrieve a word in a string from an array of possible words [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I hope I worded the question correctly! I want to retrieve the name of a country if it exists within a string.
$bio = 'A biography about someone from France';
$countries = ['Germany', 'Spain', 'France'];
How could I check for the existence of a country in the string using the countries array?
And then, if a match is found, return it? In this example, I'd be left with the word France.
try this code:
print_r(array_intersect(explode(' ', $bio), $countries));
or
foreach ($countries as $v)
if (mb_stripos($bio, $v) !== false)
{
echo $v;
break;
}
or
echo #array_shift(array_intersect(explode(' ', $bio), $countries));
u have to use foreach on ur countries array, and inside check if strpos(bio, country) to find the position of the country word. if the position is not false it means it's there!!

Break string into fields [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string input using PHP that look like this:
Log,,t1,12.12:t2,20.23:t3,30.00:t4,50.20:11.23
I want to save it into MySQL
field:reading
t1:12.12
t2:20.23
t3:30.00
t4:50.20
Temp:11.23
Can anyone give me an direction?
Begin you have to parse to rows and columns
$string = 't1,12.12:t2,20.23:t3,30.00:t4,50.20:11.23';
$rows = explode(':', $string);
$table = array();
foreach(rows as $row) {
$table[] = explode(',', $row);
}
You can use regex below:
if (preg_match_all('/[a-z]+\d+,\d+\.\d+|\d+\.\d+/i', $string, $matches) {
var_dump($matches[0]);
}
The regex can be a bit simpler if each field will called "t+number":
if (preg_match_all('/t\d+,\d+\.\d+|\d+\.\d+/i', $string, $matches) {
var_dump($matches[0]);
}

preg_replace php KEY <-> VALUE [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have array:
$array = array(
'/news/show/([0-9])/([0-9])'=>'/news/show/id/$1/id2/$2',
'/home/ayz/([0-9])'=>'/home/xyz/sid/$1'
);
I want result:
$array = array(
'/news/show/$1/$2'=>'/news/show/id/([0-9])/id2/([0-9])',
'/home/ayz/$1'=>'/home/xyz/sid/([0-9])'
);
How to do it??
$aReplace = array('$1'=>'([0-9])','$2'=>'([0-9])');
$aNewArray = array();
foreach ($array as $key=>$value){
$nKey = str_replace(array_values($aReplace), array_keys($aReplace),$key);
$nValue = str_replace(array_keys($aReplace), array_values($aReplace),$value);
$aNewArray[$nKey]=$nValue;
}

Categories