need to join arrays in to single one [duplicate] - php

This question already has answers here:
Combine two arrays
(11 answers)
Closed 5 months ago.
how can we join this two arrays into one array
for this, I had done my code like this and got the output as shown below
$groups_array = array_map('trim',explode(',', $tt));
$tt looks like this string(5) "11:00" string(5) "10:00"
array(1) { [0]=> string(5) "11:00" } array(1) { [0]=> string(5) "10:00" }
need desired output to look like
array(1) { [0]=> string(5) "11:00",[1]=> string(5) "10:00" }
My code is here please have a look
<?php $time_booked=$this->Hospital_model->get_already_booked_time($d,$timeslot->doctor_id);
foreach($time_booked as $index1=> $t) {
$tt=$t->time;
$groups_array = array_merge(array_map('trim',explode(',', $ttt)));
} ?>
my var_dump($time_booked) looks like this
array(2) { [0]=> object(stdClass)#40 (1) { ["time"]=> string(5) "11:00" } [1]=> object(stdClass)#41 (1) { ["time"]=> string(5) "10:00" } }

What about array_merge() with array_map()
$groups_array = array_merge(array_map('trim',explode(',', $tt)));
Output:-https://eval.in/1012484
By looking your edit in your question no need to do any extra stuff, just create an array and add values to it
<?php
$groups_array = []; //create array
$time_booked=$this->Hospital_model->get_already_booked_time($d,$timeslot->doctor_id);
foreach($time_booked as $index1=> $t) {
$groups_array[] =$t->time; //add values to array
}
var_dump($groups_array);
?>

What about array_merge ? That should give you the result.
http://php.net/manual/de/function.array-merge.php
EDIT:
$tt = ['11:00'];
$tt2 = ['10:00'];
$result = array_merge($tt,$tt2);
var_dump($result);
Result is
array(2) {
[0]=>
string(5) "11:00"
[1]=>
string(5) "10:00"
}
Is that not what you meant ?

Suppose you have two array which look like
$array1 = array(0 => "10:00 am");
$array2 = array(0 => "11:00 am");
and you want to join and want output like: Array ( [0] => 10:00 am [1] => 11:00 am )
then you can use array_merge option
$array3 = array_merge($array1, $array2);
If you print print_r($array3);
output will be
Array ( [0] => 10:00 am [1] => 11:00 am )

Related

PHP Why two same value from two different string are not equal to each other

Why are these two strings not equal?
I tried to get the same name so I can create a file, however I cannot get two strings equal to each other, even though I think both strings have the same value.
I uploaded var_dump output
any idea how to fix it?
$selectCategory = scandir($_SERVER['DOCUMENT_ROOT'].'/database/');
$cat = explode('.',$category);
print_r($cat);
print_r($selectCategory);
if($cat[0] == $selectCategory[2]){
echo " true";
}
else{
echo "no";
}
output:
Array ( [0] => bus [1] => php )
Array ( [0] => . [1] => .. [2] => bus [3] => fruit )
no
This is var_dump output
array(2) { [0]=> string(5) " bus" [1]=> string(3) "php" }
array(4) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(3) "bus" [3]=> string(5) "fruit" }
no
As you can see from the var_dump output the items you are comparing are different lengths. There is a space and possibly a hidden character in the $cat one:
To trim all space and some other characters use this:
$cat = array_map('trim', $cat);
$selectCategory = array_map('trim', $selectCategory);

NOt getting the array - PHP JSON Explode

I am trying to explode the JSON file below with the 2 arrays but keep getting an output of..
Array
(
[0] =>
)
Array
(
[0] =>
)
What am I doing wrong?
[0]=>
array(2) {
["question_id"]=>
string(2) "88"
["weight"]=>
string(1) "5"
}
[1]=>
array(2) {
["question_id"]=>
string(2) "89"
["weight"]=>
string(1) "5"
$quest_id = $data["question_id"];
$quest_points = $data["weight"];
$quest_id_array = explode(" ,", $quest_id);
$quest_points_array = explode(" ,", $quest_points);
print_r($quest_id_array);
print_r($quest_points_array);
Assuming $data is the array you show at the top, it's not an array containing comma-separated strings. It's a 2-dimensional array, so you need to loop over it and push the values onto the new arrays.
$quest_id_array = [];
$quest_points_array = [];
foreach ($data as $val) {
$quest_id_array[] = $val['question_id'];
$quest_points_array[] = $val['weight'];
}

Simple array_diff not working

I have 2 arrays.
The first one is $teach_array and the second one is $langs_array.
Their respective values are:
$teach_array : Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
$langs_array : Array ( [0] => 2 [1] => 3 )
Im trying to return a new array containing all the entries from $teach_array that are not present in $langs_array.
So the end result should be: Array ( [0] => 1 [3] => 4 [4] => 5 )
I have tried using a couple of methods including :
Option 1
$result = array_diff($teachArray, $language_1d_array);
This still returns all the values of $teach_array.
Option 2
$result = array_diff_key($teachArray, $language_1d_array);
However, this only returns Array ( [2] => 3 [3] => 4 [4] => 5 ) which is not correct.
Option 3
$result = array_values(array_diff_key($teachArray, $language_1d_array));
This returns the same result as Option 2. I also tried using only array_diff instead of array_diff_key and it returns the same result as Option 1.
I did a var_dump on both of my arrays and here is the result.
$teach_array : array(5) { [0]=> string(5) " 1 " [1]=> string(5) " 2 " [2]=> string(5) " 3 " [3]=> string(5) " 4 " [4]=> string(5) " 5 " }
$lang_array : array(2) { [0]=> string(1) "2" [1]=> string(1) "3" }
hope you have already found the solution, but just in case I want to point you on following.
Blockquote
I did a var_dump on both of my arrays and here is the result.
$teach_array : array(5) { [0]=> string(5) " 1 " [1]=> string(5) " 2 " [2]=> string(5) " 3 " [3]=> string(5) " 4 " [4]=> string(5) " 5 " }
$lang_array : array(2) { [0]=> string(1) "2" [1]=> string(1) "3" }
No single value from $teach_array matches any value of $lang_array.
Because there are differently formatted values, one array contains whitespaces before and after the value you want to match " 2 ".
var_dump($teach_array) => array(5) { [0]=> string(5) " 4 " ... }
var_dump($lang_array) => array(5) { [0]=> string(1) "2" ... }
I guess you have some whitespaces included. Please try again with:
$diff = array_diff(array_map('trim', $teach_array), $lang_array);
PHPTester just tested yours, works fine for me..?
$teachArray =[1,2,3,4,5];
$langsarray =[2,3];
$result = array_diff($teachArray,$langsarray);
print_r($result);
works and prints 1, 4, 5 for me.
BUT...here's a solution for what you're trying to acquire: the values in teacher array that are not in langs
$new_array = array();
foreach($teach_array as $item){ // Loop the teacher_array
if(!in_array($item,$langs_array)){ // If the teach_array value doesn't exist in the lang_array, add the value
$new_array[] = $item;
}
}
I'm sure theres a more elegant way, but this works:
$teach = [1, 2, 3,4, 5];
$langs = [2, 3];
$result = [];
foreach ($teach as $key => $t) {
if (!in_array($t, $langs)) {
$result[$key] = $t;
}
}
var_dump($result);
This is (basically) what you say you have. It works for me:
<?php
$fred = array(0=>1, 1=>2, 2=>3, 3=>4, 4=>5);
$bert = array(0=>2, 1=>3);
$res = array_diff($fred, $bert);
print_r($res);

Remove array from multi-dimensional indexed array based on duplicate value [duplicate]

This question already has answers here:
PHP unique array by value?
(6 answers)
Closed 6 years ago.
I have a PHP array that looks like this,
[["a","b"],["e","j"],["a","s"]]
I need it to look like this,
[["a","b"],["e","j"]]
or this,
[["e","j"],["a","s"]]
I cannot have two inner arrays that contain the same "0" index. It does not matter which inner array is deleted as long as only one remains. How can I go through this array and remove inner arrays that contain the same "0" index?
Thanks!
You could simply loop through the array with two loops. Let's say this is your data:
$data = array(
array('a', 'b'),
array('e', 'j'),
array('a', 's'),
array('a', 't'),
array('c', 't'),
array('a', 'h'),
array('c', 'e'),
array('f', 'g')
);
Then you go ahead and loop through everything, and unset it if it's the same value.
$count = count($data);
foreach($data as $index => $array){
for($i=$index + 1; $i<$count; $i++)
if(isset($array[0]) && isset($data[$i][0]) && $array[0] == $data[$i][0])
unset($data[$i]);
}
The var_dump of $data after the loops would be:
array(4) {
[0]=>
array(2) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
}
[1]=>
array(2) {
[0]=>
string(1) "e"
[1]=>
string(1) "j"
}
[4]=>
array(2) {
[0]=>
string(1) "c"
[1]=>
string(1) "t"
}
[7]=>
array(2) {
[0]=>
string(1) "f"
[1]=>
string(1) "g"
}
}
<?php
$array = [["a","b"],["e","j"],["a","s"]];
$values = array();
foreach ($array as $key => $arrayChild) {
foreach ($arrayChild as $val) {
if (in_array($val, $values)) {
unset($array[$key]);
}
$values[] = $val;
}
}
print_r($array);
?>
Result:
Array (
[0] => Array ( [0] => a [1] => b )
[1] => Array ( [0] => e [1] => j )
)

Sorting array of n objects - uasort and sort

i get data from a database which results in array with unlimited objects which look like this (example given with 2 objects, but there can be more):
array(2)
{
[0] object(stdClass)#34 (6)
{
["id"] string(2) "32"
["voting_id"] string(2) "42"
["answer"] string(4) "No"
["color"] string(7) "#F7464A"
["votes"] string(1) "1"
["percentage"] float(0.166666666667)
}
[1] object(stdClass)#33 (6)
{
["id"] string(2) "31"
["voting_id"] string(2) "42"
["answer"] string(2) "Yes"
["color"] string(7) "#E2EAE9"
["votes"] string(1) "5"
["percentage"] float(0.833333333333)
}
}
I want to sort this array either by votes or by percentage asc/desc. I tried it with:
function sorterVotes($a, $b) {
if ( $a->votes < $b->votes ) return -1;
if ( $a->votes > $b->votes ) return 1;
return 0;
}
usort($answers, 'sorterVotes');
var_dump($answers);
But the only thing that gets sorted is that the [0] and the [1] changes, but this is not helpful for me because i go through that array via foreach later.
How can i sort this array of n objects by votes / percentage and asc/desc?
Would be great if anybody can help. Thanks.

Categories