I have a array like this
[0] => Array
(
[0] => LBLdss_COLLEsdfCTIONS_RsdfsdEPORT_TITfsdfLE
[1] =>
[2] =>
[3] => Array
(
[Administration] => Array
(
[bidsflldsf6] => Array
(
[0] => themes/Care2012/images/Payments
[1] => LsdfBL_OPsddfD_BIsfdfsLLING_SsdfdsUMsdfMARY_TITLE
[2] => LsdfsdBL_OPDfdsfd_BILfdsLING_dsfdsSUMMARY
[3] => ./index.php?module=Rsdfepfdsforts&action=reposfdfdsrtsel&rname=Opdpasfdfypdf
)
[bilhghjgl_pat_reg] => Array
(
[0] => themsdfsdes/Casfdfre2aasd012/imasdfges/Pasdymesddfnts
[1] => LBL_sdfsPAT_RsdfEG_TsdfITLE
[2] => LBL_PdfsdAT_sfdREG_TdsfdITLE_DsdfsETAIL
[3] => ./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf
)
)
)
[4] =>
)
Now i need to extract value of rname from this index [3] => ./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf (which is Pacxvtregcollxcvectionpdf in this case)and have to save it
I can try explode function but it is heavy for me since my array size is large
please help in resolving this
Thanks
Try placing a foreach for ciclying the array and formulate a regular expression for extract your param. Like this:
foreach($youvar as $text) {
preg_match('$rname=(.+?)$', $text, $rname[])
}
After that you can try a print_r on $rname and adapt to you.
Try below :-
$a = explode("rname=", index[3]);
echo $a[1;]
Code
$array = array(
'./index.php?module=Rsdfepfdsforts&action=reposfdfdsrtsel&rname=Opdpasfdfypdf',
'./index.php?module=Rexcvpofdsrts&action=reportsel&rname=Pacxvtregcollxcvectionpdf',
);
function getRname($str){
//return null if not found
preg_match('/rname\=([a-zA-Z]+)($|&)/', $str , $matches);
return $matches[1];
}
foreach($array as $str){
echo "Rname: " . getRname($str). "<br/>";
}
Result
Rname: Opdpasfdfypdf
Rname: Pacxvtregcollxcvectionpdf
Try it?
Related
I'm consuming an API which returns an array of objects as this:
$base = array(
["orange","_","banana"],
["banana","_","_"],
["_","apple","kiwi"],
["_","raspberry","strawberry"]
);
And I intend to show "0" when key value is "_" however I haven't found a better way to do this than this:
foreach ($base as $key => $value) {
for ($i=0; $i<=3;$i++) {
if ($base[$key][$i]=="_")
$base[$key][$i]="0";
}
}
This works just fine since it's a simple demo but the real array is sometimes big and I've found this solution somewhat inefficient.
My question is, there's some php built-in function to do achieve this in or at least a better way to do this?
Thanks in advance guys,
Use array_walk_recursive(), pass the elements by reference and walk over the array, checking for the value _ - if its a match, replace it with 0.
$base = array(
["orange","_","banana"],
["banana","_","_"],
["_","apple","kiwi"],
["_","raspberry","strawberry"]
);
array_walk_recursive($base, function(&$v) {
if ($v === '_')
$v = 0;
});
Output becomes
Array
(
[0] => Array
(
[0] => orange
[1] => 0
[2] => banana
)
[1] => Array
(
[0] => banana
[1] => 0
[2] => 0
)
[2] => Array
(
[0] => 0
[1] => apple
[2] => kiwi
)
[3] => Array
(
[0] => 0
[1] => raspberry
[2] => strawberry
)
)
Live demo at https://3v4l.org/6Bs8ZE
You can replace _ with 0;
json_decode(str_replace('"_"','"0"',json_encode($base)));
I want to search and delete $srch_data data from the $list, but array_search() is not working. What's going wrong?
$srch_data = 'neha,neha#xyz.com';
$list = "gaurav,gaurav#xyz.com,neha,neha#xyz.com,ayush,ayush#xyz.com";
$arr = explode(',',$list);
$list_array = array_chunk($arr,2);
$pos = array_search($srch_data,$list_array);
echo $pos;
The problem is that you are searching with different things. Once you have used array_chunk(), your data is...
Array
(
[0] => Array
(
[0] => gaurav
[1] => gaurav#xyz.com
)
[1] => Array
(
[0] => neha
[1] => neha#xyz.com
)
...
and you are searching for
'neha,neha#xyz.com'
so this will not match.
If you converted your search string to an array as well, this will work...
$pos = array_search(explode(",", $srch_data),$list_array);
I have a long string like this I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];. Now I just want to get certain words like 'I1','I2','I8','NA1' and so on i.e. words between ':'&';' only ,and store them in array. How to do that efficiently?
I have already tried using preg_split() and it works but giving me wrong output. As shown below.
// $a is the string I want to extract words from
$str = preg_split("/[;:]/", $a);
print_r($str);
The output I am getting is this
Array
(
[0] => I8
[1] => 2
[2] => I1
[3] => 1
[4] => I2
[5] => 2
[6] => I3
[7] => 2
[8] => I4
[9] => 4
[10] =>
)
Array
(
[0] => NA1
[1] => 5
[2] =>
)
Array
(
[0] => IA1
[1] => [1,2,3,4,5]
[2] =>
)
Array
(
[0] => S1
[1] => asadada
[2] =>
)
Array
(
[0] => SA1
[1] => [1,2,3,4,5]
[2] =>
)
But I am expecting 'I8','I1','I2','I3','I4' also in seperated array with position [0]. Any help on how to do this.
You could try something like.
<?php
$str = 'I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];';
preg_match_all('/(?:^|[;:])(\w+)/', $str, $result);
print_r($result[1]); // Matches are here in $result[1]
You can perform a greedy match to match the items between ; and : using preg_match_all()
<?php
$str = 'I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];';
preg_match_all('/;(.+?)\:/',$str,$matches);
print_r($matches[1]);
Live Demo: https://3v4l.org/eBsod
One possible approach is using a combination of explode() and implode(). The result is returned as a string, but you can easily put it into an array for example.
<?php
$input = "I1:1;I2:2;I8:2;NA1:5;IA1:[1,2,3,4,5];S1:asadada;SA1:[1,2,3,4,5];SA1:[1,2,3,4,5];.";
$output = array();
$array = explode(";", $input);
foreach($array as $item) {
$output[] = explode(":", $item)[0];
}
echo implode(",", $output);
?>
Output:
I1,I2,I8,NA1,IA1,S1,SA1,SA1,.
Currently I have an array like :
Array(
[0] => Array([range]=>1-10 [count]=>3 [type]=>A)
[1] => Array([range]=>11-20 [count]=>6 [type]=>A)
[2] => Array([range]=>21-30 [count]=>5 [type]=>A)
[3] => Array([range]=>1-10 [count]=>5 [type]=>B)
[4] => Array([range]=>11-20 [count]=>3 [type]=>B)
[5] => Array([range]=>21-30 [count]=>8 [type]=>B)
[6] => Array([range]=>1-10 [count]=>4 [type]=>C)
[7] => Array([range]=>11-20 [count]=>3 [type]=>C)
[8] => Array([range]=>21-30 [count]=>6 [type]=>C)
[9] => Array([range]=>1-10 [count]=>3 [type]=>D)
[10] => Array([range]=>11-20 [count]=>7 [type]=>D)
And then I am trying to regroup/remake the array depends on their type and the expected output would be like :
Array(
[0] => Array([type]=>A [1-10]=>3 [11-20]=>6 [21-30]=>5)
[1] => Array([type]=>B [1-10]=>5 [11-20]=>3 [21-30]=>8)
[2] => Array([type]=>C [1-10]=>4 [11-20]=>3 [21-30]=>6)
[3] => Array([type]=>D [1-10]=>3 [11-20]=>7)
)
I have tried array_column but isn't what exactly I want...
Example Here.
Thanks in advance.
This should work for you:
Here I simply loop through the entire array and then check with isset() if the result array already has an innerArray with the same type (e.g $result["A"]), if not I add the type as value to the inner array (.e.g. $result["A"]["type"] = "A";).
After this check I simply add the range and count to each type (e.g. $result["A"]["1-10"] = 3;)
At the end I simply reindex the entire $result array with array_values().
<?php
foreach($arr as $k => $v) {
if(!isset($result[$v["type"]]))
$result[$v["type"]]["type"] = $v["type"];
$result[$v["type"]][$v["range"]] = $v["count"];
}
$result = array_values($result);
print_r($result);
?>
output:
Array
(
[0] => Array
(
[type] => A
[1-10] => 3
[11-20] => 6
[21-30] => 5
)
//...
)
I have this array, $array :
Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_ABS_132.jpg
[5] => http://download.server.com/18821_SM_138.jpg
)
and in this case, I am looking for any line that has ABS inside.
I could look for that by using the regexp http://.+ABS.+, and this will select the entire line.
But I still need to remove it from the array, not just replace it (or leave it empty.) But in this case, the array will become:
Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_SM_138.jpg
)
Any ideas what method i need to use?
Thanks.
edit:
i am using OOP php
Use array_filter() with a custom callback.
Example:
function testABS($elem) {
return strpos($elem, 'ABS') === false;
}
print_r(array_filter($the_array, 'testABS'));
Note: This is a contrived example. You will need to adjust the logic in the callback function to meet your needs.
There's preg_grep:
$abs = preg_grep('/ABS/', $your_array);
and returns the matches as an array. It also has a flag to return only the non-matching entries, which is probably what you want: return all entries which do NOT have 'abs' in them.
What i understood is u want to remove that element from the array.
can do like this.
$arr = Array
(
[0] => http://download.server.com/18821_SM_139.jpg
[1] => http://download.server.com/18821_SM_134.jpg
[2] => http://download.server.com/18821_SM_138.jpg
[3] => http://download.server.com/18821_SM_138.jpg
[4] => http://download.server.com/18821_ABS_132.jpg
[5] => http://download.server.com/18821_SM_138.jpg
);
$new_arr = array();
foreach($arr as $link){
if(!preg_match('ABS', $link)){
$new_arr[] = $link;
}
}
//ths will give array with only 4 elements as '18821_ABS_132.jpg' will be removed
return $new_arr;