Merge a string in an array - php

I have this array:
$fields = $_GET['r'];
Which has some ids, for example:
Array (
[0] => 3134
[1] => 3135 )
and then I have this string in $tematiche:
3113,3120
How can I marge this string in the first array? Also, how can I remove the equals id (if any)?

Try,
$fields = array_unique(array_merge($fields,explode(",",$tematiche)));
echo "<pre>";
print_r($fields);

Try this :
$fields = $_GET['r'];
$string = '3113,3120';
$array = explode(",",$string);
$res_array = array_unique(array_merge($fields,$array));
echo "<pre>";
print_r($res_array);
Output :
Array (
[0] => 3134
[1] => 3135
[2] => 3113
[3] => 3120
)

A combination of explode(), array_merge() and array_unique() would be suitable.
// Make $tematiche into an array
$tematiche_array = explode(',', $tematiche);
// Merge the two arrays
$merged_array = array_merge($fields, $tematiche_array);
// Remove all duplicate values in the array
$unique_array = array_unique($merged_array);

Please try like this. This should work for you.
$a = explode(',', '3113,3120');
print_r(array_merge($fields,$a));

Related

how to remove part of values from array in php

I have an array with date & time like below:
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
From each value, the T10:00 should be cut off, so that my new array looks like this:
$new_array = array('2021-05-04', '2021-05-05', '2021-05-06');
How can i do that?
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = [];
foreach($array as $a) {
$a = explode('T', $a)[0];
array_push($new_array, $a);
}
Iterate through the array by array_map with callback function take only first 10 chars, Which represent time.
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = array_map(fn($time)=>substr($time, 0, 10), $array);
print_r($new_array);
Prints:
/*
Array
(
[0] => 2021-05-04
[1] => 2021-05-05
[2] => 2021-05-06
)
*/
the T10:00 should be cut off
If you have a constant time T10:00 and want to get rid of it just replace it with empty!
$array = array('2021-05-04T10:00', '2021-05-05T10:00', '2021-05-06T10:00');
$new_array = array_map(fn($time)=>str_replace('T10:00', '', $time), $array);
print_r($new_array);
//Array ( [0] => 2021-05-04 [1] => 2021-05-05 [2] => 2021-05-06 )

Compare a string and a php array and only show the matches

I have a string that looks like this:
"illustration,drawing,printing"
I have an array that looks like this:
Array (
[0] => illustration
[1] => drawing
[2] => painting
[3] => ceramics
)
How can I compare the two and only display the terms that exist in both the string and array?
So in the above example, I would want to output something that looks like this:
SKILLS: illustration, drawing
Thank you!
use explode(), array_intersect() and implode()
<?php
$string = "illustration,drawing,printing";
$array = Array (
0 => 'illustration',
1 => 'drawing',
2 => 'painting',
3 => 'ceramics'
);
$stringArray = explode(',',$string);
$common = array_intersect($stringArray,$array);
echo "Skills :".implode(',',$common);
https://3v4l.org/StqYN
You can do something like this:
Function:
function functionName($string, $array){
$skills = '';
// Create a temp array from your string breaking appart from the commas
$temparr = explode(",", $string);
// Iterate through each of the words now
foreach($temparr as $str){
// Check to see if our current string is in the array provided
if(in_array($str, $array)){
// If it is, then add it to the string "skills"
$skills .= "${str}, ";
}
}
// Cut the last space and comma off the end of the str.
$skills = substr($skills, 0, strlen($skills) - 2);
// Return our results
return 'SKILLS: '.$skills;
}
Usage:
$string = "illustration,drawing,printing";
$array = Array (
0 => "illustration",
1 => "drawing",
2 => "painting",
3 => "ceramics"
);
// Just input our string and the array we want to search through
echo(functionName($string, $array));
Live Demos:
https://ideone.com/qyqgzo
http://sandbox.onlinephpfunctions.com/code/65dc988370ad8ce61ab5ccbc232af4bcf8bd3d42
You could replace the comma , and join the string words on OR | and grep for them. This will match the word anywhere in each array element:
$result = preg_grep("/".str_replace(",", "|", $string)."/", $array);
If you want exact matches only then: "/^(".str_replace(",", "|", $string).")$/"
Firstly convert that string into array.
use array_intersect();
$str = "illustration,drawing,printing";
$ar1 = explode(",",$str);
print_r ($ar1);
$ar2 = Array ("illustration","drawing","painting","ceramics");
echo "<br>";
print_r ($ar2);
$result = array_intersect($ar1, $ar2);
echo "Skills :".implode(',',$result);
OUTPUT:
Array (
[0] => illustration
[1] => drawing
[2] => printing
)
Array (
[0] => illustration
[1] => drawing
[2] => painting
[3] => ceramics
)
SKILLS: illustration, drawing

Sum arrays in array variable

I have a little problem. Here is the code:
$arr = explode(',', $odluka);
$arr2 = array($arr[0], $arr[1], $arr[2], $arr[3], $arr[4], $arr[5], $arr[6], $arr[7], $arr[8], $arr[9]);
while ($arrk = current($arr2)) {
if ($arrk == '1') {
$ark = key($arr2);
//print_r($ark);
//echo $arr2[$ark];
$arop = explode(',', $utroseno);
$aropk = array($arop[0], $arop[1], $arop[2], $arop[3], $arop[4], $arop[5], $arop[6], $arop[7], $arop[8], $arop[9]);
$array = array($aropk[$ark]);
print_r($array);
}
next($arr2);
}
Output of $array is
Array ( [0] => 1 ) Array ( [0] => 5 ) Array ( [0] => 10 ) Array ( [0] => 4 ) Array ( [0] => 4 ) Array ( [0] => 1 ) Array ( [0] => 1 )
How can I merge this values and sum them. I want sum of 1+5+10+4+4+1+1. Thanks!
declare a variable to store sum
iterate over array
-> add value to sum
Here is a simple example how to deal with your output array:
$data = [
[1], [5], [10], [4]
];
$sum = array_sum(array_map(function($elem) { return $elem[0]; }, $data));
var_dump($sum);
You don't need to assign them to another array and loop..you can just sum everything after explode. You just need one line of code for that:
array_sum(explode(',', $odluka));
Then you'll get the sum of all the numbers
Not need using any array and loop.You are using only "array_sum ()" php building function.Like
<?php
$foo[] = "12";
$foo[] = 10;
$foo[] = "bar";
$foo[] = "summer";
echo array_sum ($foo); //same as echo "22";
?>
For more information Read Php Manual link
Use this function
array_sum ($arr);

PHP switch array values

I have a very simple array like this:
Array ( [friend_id] => 180 [user_id] => 175 )
What I want to do is just to switch the values, in order to be come this:
Array ( [friend_id] => 175 [user_id] => 180 )
Is there any elegant NON STATIC way in PHP to do it?
you can use array_combine and array_reverse
$swapped = array_combine(array_keys($arr), array_reverse(array_values($arr)));
No. Use a temporary value:
$temp = $array['friend_id'];
$array['friend_id'] = $array['user_id'];
$array['user_id'] = $temp;
A bit longish, but I think it satisfies you requirements for 2 element arrays, just as you used in your example:
// your input array = $yourarray;
$keyarray = array_keys($yourarray);
$valuearray = array_values($yourarray);
/// empty input array just to make sure
$yourarray = array();
$yourarray[$keyarray[0]] = $valuearray[1];
$yourarray[$keyarray[1]] = $valuearray[0];
Basically Orangepill's answer done manually...
How about using array_flip?
array array_flip ( array $trans )
$myar = array('apples', 'oranges', 'pineaples'); print_r($myar);
print_r(array_flip($myar));
Array
(
[0] => apples
[1] => oranges
[2] => pineaples
)
Array
(
[apples] => 0
[oranges] => 1
[pineaples] => 2
)
$tmp = $array['user_id'];
$array['user_id'] = $array['friend_id'];
$array['friend_id'] = $tmp;

Comparing two arrays with array_diff

I have the following code and am trying to compare two array's with array_diff however I keep getting no results. I not sure if it matters, but there are many fields in the array and I really only want to compare 1 field...is this possible? what am I missing?
<?php
$json = file_get_contents("http://ebird.org/ws1.1/data/obs/region/recent?rtype=subnational1&r=US-AZ&back=7&fmt=json");
$json2 = file_get_contents("http://ebird.org/ws1.1/data/obs/region/recent?rtype=subnational1&r=US-NV&back=7&fmt=json");
$array1 = json_decode($json, TRUE);
$array2 = json_decode($json2, TRUE);
if ( $array1 == $array2 ) {
echo 'There are no differences';
}else
var_dump(array_diff($array2, $array1));
echo 'they are different';
?>
You will need to check the arrays against each other:
$Array_1 = array (1,2,3,4,5);
$Array_2 = array(1,2,3,4,5,6);
print_r(array_diff($Array_1,$Array_2));
Will output:
Array
(
)
Whereas:
print_r(array_diff($Array_2,$Array_1));
will output:
Array
(
[5] => 6
)
So this might be a solution:
function ArrayDiff ($Array_1, $Array_2){
$Compare_1_To_2 = array_diff($Array_1,$Array_2);
$Compare_2_To_1 = array_diff($Array_2,$Array_1);
$Difference_Array = array_merge($Compare_1_To_2,$Compare_2_To_1);
return $Difference_Array;
}
print_r(ArrayDiff($Array_1,$Array_2));
Which will output:
Array
(
[0] => 6
)
Putting this into an if statement:
$Differences = ArrayDiff($Array_2,$Array_1);
if (count($Differences) > 0){
echo 'There Are Differences Between The Array:';
foreach ($Differences AS $Different){
echo "<br>".$Different;
}
All the examples and code is based off the arrays at the start ($Array_1 and $Array_2)
$po_line_array=array();
$po_line_clone_array=array();
foreach($cart->line_items as $line_no => $po_line)
$po_line_array[$line_no]=$po_line->labdip_details_id;
print_r($po_line_array,1);
foreach($cart->line_items_clone as $line_no_clone => $po_line_clone)
$po_line_clone_array[$line_no_clone]=$po_line_clone->labdip_details_id;
print_r($po_line_clone_array,1);
$result=array_diff($po_line_clone_array,$po_line_array);
print_r($result,1);
Output:
Array ( [0] => 101 )
Array ( [0] => 101 [1] => 103 )
Array ( [1] => 103 )

Categories