PHP ARRAY - remove certain array value - php

Is there any function or method to remove array value for example, i have an array like this:
Array (
[0] => (Some string value)51351
[1] => (Some string value)43822
)
So the question is, How do i get the value that is not in the "( )". and counting the value of array after remove "( Some string value )" to do some looping process?
Thank you!

<?php
function digits_only($str){
return preg_replace("/\(.*\)/", "", $str);
}
$arr = array("(Some content)531", "(Another Content)613");
$digits_array = array_map("digits_only", $arr);
var_dump($digits_array);
echo array_sum($digits_array);
Live Demo:
http://codepad.org/mYPL3PYH

Related

How to Remove Elements from array in php after some given character?

I want to Remove all the Elements from my array which is after , but I am unable to do this.
I have an array like this => ["18-08-2022, 05:08:23pm","18-08-2022, 05:09:05pm"]
and I want to print array something like this => ["18-08-2022","18-08-2022"]
I want to remove Elements after the ,
This is what I tried
<?php
while ($row = mysqli_fetch_array($result)) {
$Etime[] = $row["Etime"];
$Etime1[]=$row["Etime"];
$E2[]=substr($Etime1[],',',true);
}
$Etime = json_encode($Etime);
$Etime1=json_encode($Etime1);
echo $Etime1
?>
this is easy-
steps to do so-
1.Parse your array as string.
2.store that in a variable.
3. then use a if loop and use php explode funtion. explode function will seperate that string elements by the seperator in this case the "," you want to remove.
explode(string $separator, string $string)
where,
separator-The boundary string.
string-The input string.
limit
example=
$text = "hello,there";
//using explode-
var_dump( explode( ',', $input2 ) );
output=
array(2)
(
[0] => string(5) "hello"
[1] => string(5) "there"
)
more about explode() here- https://www.php.net/manual/en/function.explode.php
Here's a simple php that generates the output you want. Since you only need date bits I have provided you with only the date bits. You can get general idea from here
$arr = ["18-08-2022, 05:08:23pm","18-08-2022, 05:09:05pm"];
//
// sample output: [ "18-08-2022", "18-08-2022" ]
//
$dateMappedToYourFormat = array_map(function($dt) {
return explode(",", $dt)[0]; // getting everything before comma
}, $arr);
echo implode(",", $dateMappedToYourFormat); //to view your result

How to get array value that doesn't exist in another array [duplicate]

This question already has answers here:
Using array_diff(), how to get difference from array2 instead of array1?
(4 answers)
difference between two arrays
(7 answers)
Closed 5 years ago.
I have two arrays, each generated from a string. The strings are:
$string1 = "#574390, #574387, #574386, #574383 (keyboard enter)
#574368, #574367, #574364, #574361, #574357, #574355, #574351, #574343, #574341 (keyboard enter)
#574381, #574379, #574377, #574375, #574374, #574373, #574372, #574371, #574369"
$string2 = "574390
574386
574383
574381
574379
574377
574375
574374
574373
574372
574371
574369
574368
574367
574364
574361
574357
574355
574351
574343
574341"
Then, I do this to explode each string to array:
$str1 = checkstring($string1);
$str2 = checkstring($string2);
function checkstring($x) {
//check whether the string has "#" in it
if (!strstr($x, '#')) {
$array1 = str_replace(" ", "", $x);
$array1 = explode("\n", str_replace("\r", "", $array1));
return $array1;
}
else {
$array2 = str_replace("\r", ", ", str_replace("#", '', $x));
$array2 = array_unique(explode(", ", $array2), SORT_REGULAR);
return $array2;
}
}
After that, I try to find the difference between the two array:
$result = array_diff($str1, $str2);
print_r($result);
As you can see, the difference between array 1 and array 2 is that in array 1 there is 574387 but not in array 2. The result that I get from the code is this:
Array ( [1] => 574387 [4] => 574368 [13] => 574381 )
And if I switch the value between $string1 and $string2 the result will be this:
Array ( [3] => 574381 [12] => 574368 )
I do the switching because I want to make it able to check both ways. I was wondering what's wrong with it.
Thanks for the help.
[array_diff] Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.
Taken from php.net
So, it only shows the elements of array1 (in your case $str1), which are different to the compared arrays (in your case $str2). That is why 574387 is not shown in the second comparison. Also, the other two differences are shown, because instead of using a comma, you use a (keyboard enter) in front of them in $str1.
If you want to see the differences from both arrays, try something like this:
array_merge(array_diff($str1,$str2),array_diff($str2,$str1));
You can try this
preg_match_all('/[0-9]+/', $string1, $result);
$array1 = array_unique($result[0]);
preg_match_all('/[0-9]+/', $string2, $result);
$array2 = array_unique($result[0]);
$arrayDiff = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));
print_r($arrayDiff);

jQuery to PHP Array and string

I'm trying to get the checked values into a PHP array so that I am able to loop through the array but I can't seem to convert it to an array.
jQuery Ajax Posts
checked=28,24
PHP
$checked = $_POST['checked'];
$arr = array($checked);
print_r($arr);
OUTPUT
Array
(
[0] => 28,24
)
Use explode function to convert comma separated string to an array,
$arr = explode(",",$checked);

Convert Array into Key Value Pair array

I have a comma separated string which i explode into an array. If the array is of un-known length and i want to make it into a key value pair array where each element in the array has the same key, how do i do this? i'm assuming i'd have to use array_combine? can anyone give me an example using the array bellow? :
for instance:
array([0]=>zebra, [1]=>cow, [2]=>dog, [3]=>monkey, [4]=>ape)
into:
array([animal]=>zebra, [animal]=>cow, [animal]=>dog, [animal]=>monkey, [animal]=>ape)
You can't use the same key for each element in your array. You need a unique identifier to access the value of the array. When you use animal for all, what value should be used? What you can do is to make a 2 dimensional array that you have an array inside an array:
array(
[animals] => array(
[0]=>zebra, [1]=>cow, [2]=>dog, [3]=>monkey, [4]=>ape
)
)
this can be used with $array['animals'][0]
But still you need numbers or unique identifiers to access the values of the array.
Something like this:
$string = 'zebra,cow,dog,monkey,ape';
$array = explode(',', $string);
$arrayReturn['animals'] = $array;
print_r($arrayReturn);
u cant have same key for all the values but u can do this
lets say your string is
$a = 'dog,ant,rabbit,lion';
$ar = explode(',',$a);
$yourArray = array();
foreach($ar as $animals){
$yourArray['animals']=$animals;
}
Now it doesnot matter how long your string is you will have you array as
$yourArray['animals'][0]='dog'
$yourArray['animals'][1]='ant'
....... so on ......

Insert array contents into a string

I am trying to insert the contents of an array in to a string using PHP.
My array ($array1) looks like this:
Array1
(
[0] => http://www.example.com/1
[1] => http://www.example.com/2
)
I want to insert both links in to a coma separated string, so I can then insert it in to a database field.
I tried this:
foreach ($array1 as $name => $value) {
$string1 .= $value . ",";
}
echo $string1;
Which does work, but I am doing this twice in my code for another array that I also want in a separate string ($string2)
Array2
(
[0] => http://www.example.com/3
[1] => http://www.example.com/4
)
When I echo $string1 I get the correct output
http://www.example.com/1,http://www.example.com/2
But $string2 becomes this:
http://www.example.com/1,http://www.example.com/2,http://www.example.com/3,http://www.example.com/4
This happens even if I use different variable names in the foreach loop above.
Someone else also suggested I try this:
$string1 = implode(',' , $array1);
But I'm not getting any output.
Any help as to how to solve this, or any different approach is greatly appreciated!
There's a PHP function called implode for this exact purpose.
$csv = implode(',', $array);
echo $csv; //blah,blah,blah,blah
implode should work fine. It's won't give you any output unless you echo or otherwise output the result, of course.
$array1 = array("http://www.example.com/1", "http://www.example.com/2");
$array2 = array("http://www.example.com/3", "http://www.example.com/4");
echo implode(", ", array_merge($array1,$array2));
Output:
http://www.example.com/1, http://www.example.com/2, http://www.example.com/3, http://www.example.com/4

Categories