Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have 2 fields in a table
filed1 | filed2
ahmed,join,maya,omar | omar,maya
I get data by 2 array , I want check if Each value in $array1 the present in $array2 or not
in my case : if $array1[omar] present in $array2 or not , if $array1[maya] present in $array2 or not .. elc
That's my code , not worked ...What's wrong in it ?
$query = $db->query_first("SELECT * FROM table ");
$array1 = explode(",",$query[filed1]);
$array2 = explode(",",$query[filed2]);
foreach($array1 as $value)
{
if (in_array($value,$array2))
{
//true
}else{
//false
}
}
output of array1 :
Array ( [0] => maya [1] => omar [2] => ahmed [3] => join)
output of array2 :
Array ( [0] => omar [1] => maya )
I have known the cause of the problem, not in the code ... Content of the field at the base value of each line separately So used str_replace to delete the line
$query = $db->query_first("SELECT * FROM table ");
$array1 = explode(",",$query[filed1]);
$array1 = str_replace("\n","",$array1);
$array2 = explode(",",$query[filed2]);
$array2 = str_replace("\n","",$array2);
foreach($array1 as $value)
{
if (in_array($value,$array2))
{
//true
}else{
//false
}
}
As #MarkBaker said, you want to use array_intersect:
array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.
In your case:
$array1 = explode(",",$query[filed1]);
$array2 = explode(",",$query[filed2]);
$intersect = array_intersect($array1, $array2);
Since you asked, to check if this array is empty or not do the following:
if (empty($intersect))
echo 'It is empty';
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
my string has
$string = "apple,banana,orange,lemon";
I want to as
$array = [
[apple,banana],
[orange,lemon]
]
Use array_chunk in combination with explode defined in php https://www.php.net/manual/en/function.array-chunk.php
<?php
$string = "apple,banana,orange,lemon";
$input_array = explode (",", $string);
print_r(array_chunk($input_array, 2));
?>
Will output as below:
Array
(
[0] => Array
(
[0] => apple
[1] => banana
)
[1] => Array
(
[0] => orange
[1] => lemon
)
)
I hope this helps you get on your way. To transform a string to an array, you can use
$elements = explode(',', $string);
This will leave you with this array: ($elements == [apple,banana,orange,apple])
From there, you can build it the way you want, like:
$array[] = [$elements[0], $elements[1]];
$array[] = [$elements[2], $elements[3]];
This results in the array you are looking for, but this solution is only for the string you've given with four elements, separated by comma.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
So, i have 2 arrays which look like this:
$a = array(1,3,5);
$b = array(2,3,4,5);
The expected result should look like this:
array(3,5);
Is there a quick and easy way to achieve my expected result? :)
Option One:
$a = array(1,3,5);
$b = array(2,3,4,5);
$result = array_intersect($a, $b);
print_r($result);
Option 1 output:
Array
(
[1] => 3
[2] => 5
)
Option 2:
$a = array(1,3,5);
$b = array(2,3,4,5);
$resultTwo = [];
foreach($a as $val){
if(in_array($val, $b)){
$resultTwo[] = $val;
}
}
print_r($resultTwo);
Option 2 Output (unlike option 1, the array index starts from 0):
Array
(
[0] => 3
[1] => 5
)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to get only the distinct values from $pjt. I have tried the below code:
$unique_pjtdata = array_unique($pjt);
foreach($unique_pjtdata as $val) {
echo $val;
}
I am getting an HTTP Error 500 after trying this code.
Use array_unique().
Example:
$pjt = array(1, 2, 2, 3);
$array = array_unique($pjt);
and if you still get error then you need to enable error reporting to know error.
You can use array_flip() to switch the keys and values in the array therefore forcing duplicate values to overwrite into a unique set of keys:
$b = ["test1", "test2", "test1", "test3"];
$b = array_flip($b);
print_r( $b );
//output
Array
(
[test1] => 2
[test2] => 1
[test3] => 3
)
You can then extract the keys using array_keys():
$b = array_keys($b);
print_r($b)
// output
Array
(
[0] => test1
[1] => test2
[2] => test3
)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need to display some data in the chart. Basically i have an array from database which looks like this-
[
[0] =>[
[user_type] => 'Manager'
[total_count] => 3
],
[1] =>[
[user_type] => 'Director'
[total_count] => 2
]
]
There is one more user_type which is Trainee . If user Trainee has 0 Total_count it is not coming in above array. So i need to add manually. So first will check which user_type is not exist in above array out of 3 user_types. If not exist then just need to add total_count 0.
And finally my desired array should look like this-
[
['labels'] =>['Manager','Director','Trainee],
['dataset'] => [ 3, 2, 0 ]
]
Please notice here, Proper sequence is very important. So in above array Manager has 3, Director has 2 and Trainee has 0 total count.
Thanks in advance!
Try the below code maybe It will help.
/*
using array column will get the result in
Array ( [Manager] => 3 [Director] => 2 )
*/
$array_column = array_column($array, 'total_count', 'user_type');
//Create new array like below
$add_array = array('Manager'=>'0', 'Director'=>'0', 'Trainee'=>'0',);
//using array_merge merge add_array and array_column
$new_array = array_merge($add_array, $array_column);
//using array_walk_recursive get the requird result
array_walk_recursive($new_array, function($item, $key) use (&$final_array){
$final_array['labels'][]=$key;
$final_array['dataset'][]=$item;
});
echo "<pre>";
print_r($final_array);
?>
Case 1- if Trainee is not there in array
DEMO
Case 2 - If Manager and Director both are not there in array
DEMO
Case 3- if Director is not there - DEMO
Here is the script you can use with array_combine and array_map with splat(...) operator
$arr = array_combine(['labels','dataset'],array_map(null, ...$arr));
if(!in_array('Trainee',$arr['labels'])){
array_push($arr['labels'],'Trainee');
array_push($arr['dataset'],0);
}
print_r($arr);die;
array_combine — Creates an array by using one array for keys and another for its values
Note:- In array_map NULL can be passed as a value to callback to perform a zip
operation on multiple arrays. If only array1 is provided, array_map()
will return the input array. In short to perform transpose of array operation
Working demo
Output:-
Array
(
[labels] => Array
(
[0] => Manager
[1] => Director
[2] => Trainee
)
[dataset] => Array
(
[0] => 3
[1] => 2
[2] => 0
)
)
Try this
$arr = array_combine(['labels','dataset'], array_map(null, $arr));
Make a function for adding non-existing user_type into array
function setUserType($userType, $array) {
if(!in_array($userType, $array['labels'])){
array_push($array['labels'], $userType);
array_push($array['dataset'], 0);
}
return $array;
}
Call function by any user_type like 'Manager', 'Trainee', 'Director'
$arr = setUserType('Trainee', $arr);
Or make an array of all user types and run a loop
foreach(['Manager', 'Trainee', 'Director'] as $type) {
$arr = setData($type, $arr);
}
Print and show the final value
print_r($arr);
die();
Demo https://3v4l.org/UbiXn
Try it
$array = array();
foreach($yourArr as $row)
{
$array['labels'][] = $row['user_type'];
$array['dataset'][] = $row['total_count'];
}
print_r($array);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a few foreaches which show a total of some numbers.
One of them is this one:
foreach ($lijst['palen'] as $key => $valuepalen)
{
echo $valuepalen ."x Bekaclip palen (48mm / lengte " . $??? . " cm" . "\n";
}
And then there is this one which contains the values I need:
foreach ($optellen as $key => $hoogtevalue)
{
}
The values I need is $hoogtevalue which contains 100 and 110.
But if I insert $hoogtevalue in $??? it only shows the last submitted number 110.
I want to show it like this:
......... lengte is 100
......... lengte is 110
It seems to me that you are trying to map the values from one array to the other, by the position that they sit in the array, rather than by their existing key.
You could use array_map with null as the first parameter to create a new array that consists of pairs from each. (I've added a print_r of the mapped array below to demonstrate the data structure.)
You can then just loop through the pairs.
<?php
$one = [ 63 => 2, 123 => 2];
$two = [ 1 => 100, 3 => 110];
$pairs = array_map(null, $one, $two);
print_r($pairs);
foreach($pairs as $pair)
printf("%d = %d\n", $pair[0], $pair[1]);
Output:
Array
(
[0] => Array
(
[0] => 2
[1] => 100
)
[1] => Array
(
[0] => 2
[1] => 110
)
)
2 = 100
2 = 110
Alternatively you could use the array_values function on both arrays to re-index them and then use keys for association.
This will handle it:
foreach ($lijst['palen'] as $valuepalen) {
foreach ($optellen as $hoogtevalue) {
echo $valuepalen."x Bekaclip palen (48mm / lengte ".$hoogtevalue".cm \n";
}
}