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;
Related
Is it possible to change the highlighted word to numbers without changing it in database table?
wanted from this
$value['how']
to this
$value['0']
Yes, you need to use array_values()
$array = array("how" => "how-value", "to" => "to-value");
print_r(array_values($array));
Output:
Array
(
[0] => how-value
[1] => to-value
)
EDIT BY OP
To get the value
foreach($array as $key => $value) {
$someArray = array_values($value);
print_r($someArray[0]);
}
//return 144
#Milan Chheda answer is correct but I am just briefing here so the user can get a better idea of that.
Use array_values() to get the values of an array.
$FormedArray = array_values($your_array);
now print that array print_r($FormedArray);
You will get your result like
Array
(
[0] => 144
[1] => 41
[2] => USA
[3] => 12
[4] => 12
)
Here is a working example. https://eval.in/843720
I am working in php where I need to add a static value at each index of existing single dimensional array so that after adding it will become multidimensional array.
Existing single dimensional array:
[checklists] => Array
(
[0] => 20
[1] => 50
[2] => 35
[3] => 23
[4] => 24
[5] => 21
[6] => 22
[7] => 27
[8] => 25
)
Static value to be inserted 90
After insertion array will look like this:
[checklists] => Array
(
[0] => Array(90,20)
[1] => Array(90,50)
[2] => Array(90,35)
[3] => Array(90,23)
[4] => Array(90,24)
[5] => Array(90,21)
[6] => Array(90,22)
[7] => Array(90,27)
[8] => Array(90,25)
)
I want to know is there any php builtin function through which i can achieve this or should i use loop?
You can use array_map, $static is your 90, $array is your array.
$array['checklists'] = array_map(function($v) use($static){
return [$static, $v];
}, $array['checklists']);
Demo: https://3v4l.org/3ugLR
Here we are using array_map to achieve the desired output.
Solution 1:
Try this code snippet here
$staticValue=90;
$array["checklists"]= array_map(function($value) use ($staticValue){
return array($staticValue,$value);
}, $array["checklists"]);
print_r($array);
Solution 2: try this simplest one
$staticValue=90;
foreach($array as &$value)
{
$value=array($staticValue,$value);
}
print_r($array);
To modify an array, array_walk can be used:
array_walk($array['checklists'], 'addStatic');
function addStatic(&$v) {
$v = [90, $v];
}
More easy way you can do this.
$array = array();
$array['checklist'][] = array(90, 20);
$array['checklist'][] = array(90, 50);
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 ( [0] => ../files/flv/1 [1] => ../files/flv/10 [2] => ../files/flv/2 [3] => ../files/flv/3 [4] => ../files/flv/4 [5] => ../files/flv/5 [6] => ../files/flv/6 [7] => ../files/flv/7 [8] => ../files/flv/8 [9] => ../files/flv/9 )
I need to sort it this way:
Array ( [0] => ../files/flv/1 [1] => ../files/flv/2 [2] => ../files/flv/3 [3] => ../files/flv/4 [4] => ../files/flv/5 [5] => ../files/flv/6 [6] => ../files/flv/7 [7] => ../files/flv/8 [8] => ../files/flv/9 [9] => ../files/flv/10 )
I tried to use sort($array,SORT_NUMERIC);, but no luck because of this prefix ../files/flv/
I know only this solution: $array2 = array_map('basename', $array); and then sort($array2,SORT_NUMERIC);
Is there any other solutions not so complex?
Use SORT_NATURAL instead of SORT_NUMERIC (requires PHP 5.4.0 or latest):
sort($array, SORT_NATURAL);
EDIT: I used this code to test it:
$array = array(
'../files/flv/1',
'../files/flv/10',
'../files/flv/2'
);
sort($array, SORT_NATURAL);
print_r($array);
It outputs:
Array
(
[0] => ../files/flv/1
[1] => ../files/flv/2
[2] => ../files/flv/10
)
EDIT 2: Alternatively you can use the natsort() function, it works on older versions too:
natsort($array);
If "../files/flv/" path is always same you can try using str_replace function on all elements and then do sorting using sort numeric and later add same path back to all elements.
So three steps are:
Use foreach loop over array and use str_replace() function on ecah element.
do sorting as you did using sort numeric.
Use foreach loop over array and put the constant path back as prefix.
To make it more perfect you can do this by creating your own function and pass path as parameter.
Have you tried without the SORT_NUMERIC flag?
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?