I have two array like this::
$doctor = Array(
[0] => 4
[1] => 5
[2] => 8
[3] => 35
[4] => 41
[5] => 42
)
$clinic = Array(
[0] => 1
[1] => 3
[2] => 9
[3] => 15
[4] => 19
[5] => 20
)
Now i want to add these array like this
$all = array(
[0] => 4
[1] => 1
[2] => 5
[3] => 3
[4] => 8
[5] => 9
[6] => 35
[7] => 15
[8] => 41
[9] => 19
[10] => 42
[11] => 20
I have tried this but it is not my expected output:
$all = array_merge( $doctor , $clinic );
Any solution?
Thanks
You can use for loop to do that
$all=[];
for($i=0;$i<6;$i++){
$all[]=$doctor[$i];
$all[]=$clinic[$i];
}
if you don't have same length for the arrays,
Try
$doctor_size=sizeof($doctor);
$clinic_size=sizeof($clinic);
$all=[];
$size=$doctor_size;
if($doctor_size<$clinic_size){
$size=$clinic_size;
}
for($i=0;$i<$size;$i++){
if(isset($doctor[$i])){
$all[]=$doctor[$i];
}
if(isset($clinic[$i])){
$all[]=$clinic[$i];
}
}
Related
i have an multidimensional array like this, but i just only need some index to be displayed,
Array
Array
(
[0] => Array
(
[1] => 220
[38] => 200
[232] => 970
)
[1] => Array
(
[0] => 220
[2] => 190
[39] => 200
)
[2] => Array
(
[1] => 190
[3] => 40
[50] => 220
)
[3] => Array
(
[2] => 40
[4] => 200
[57] => 120
)
)
then i just want to display only index [1] and [3], so it would be like this
Array
(
[1] => Array
(
[0] => 220
[2] => 190
[39] => 200
)
[3] => Array
(
[2] => 40
[4] => 200
[57] => 120
)
)
i try using this code
$order = array(1,3);
uksort($graph, function($key1, $key2) use ($order) {
return (array_search($key1, $order) > array_search($key2, $order));
});
but still, its displayed the rest array that i dont need it that's key [0] and [2]
Like this:
foreach(array_keys($graph) as $key)
{
if($key == 0|| $key == 2)
{
unset($graph[$key]);
}
}
print_r
Array
(
[1] => Array
(
[0] => 220
[2] => 190
[39] => 200
)
[3] => Array
(
[2] => 40
[4] => 200
[57] => 120
)
)
i've been busting my head over this array:
Array (
[invoicenr] => Array (
[0] => 1234
[1] => 1234
[2] => 1234
[3] => 4321
[4] => 3214
)
[invoicedate] => Array (
[0] => 17.07.2017.
[1] => 17.07.2017.
[2] => 17.07.2017.
[3] => 11.07.2017.
[4] => 11.07.2017. )
[amount] => Array (
[0] => 10
[1] => 1
[2] => 23
[3] => 10
[4] => 1 )
[cause] => Array (
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1 )
)
I'm trying to split array above based on 1st level key "invoicenr" value, but without luck so far.
I'm expecting result:
Array (
[invoicenr] => Array (
[0] => 1234
[1] => 1234
[2] => 1234)
[invoicedate] => Array (
[0] => 17.07.2017.
[1] => 17.07.2017.
[2] => 17.07.2017.)
[amount] => Array (
[0] => 10
[1] => 1
[2] => 23)
[cause] => Array (
[0] => 1
[1] => 1
[2] => 1)
)
Array (
[invoicenr] => Array (
[0] => 4321 )
[invoicedate] => Array (
[0] => 11.07.2017. )
[amount] => Array (
[0] => 10)
[cause] => Array (
[0] => 1 )
)
Array (
[invoicenr] => Array (
[0] => 3214 )
[invoicedate] => Array (
[0] => 11.07.2017. )
[amount] => Array (
[0] => 1)
[cause] => Array (
[0] => 1 )
)
I want to know if this is possible and how, or i need to rewrite array first?
Thank you all in advance, i'm new into coding and struggling to learn so far. :)
Please post the code you attempted to solve the problem with.
I believe you will need a counter (i) to go through invoicenr and a temporary variable that remembers the last value (Array[i]) and compares it with the next value in the array of invoicenr. If the value is the same you keep going, and if it is different you print out all the other arrays[i].
i've finnaly did it, but i think my way is too primitive. Here is what i did:
//Count number of rows in initial array
$rows = count(array_filter($_POST['invoicenr']));
//Crete unique array that contains only $_POST['invoicenr'] which i can count later
$uniquearray = array();
for ($row = 0; $row < $rows; $row++) {
$uniquearray[] = $_POST['invoicenr'][$row] ;
}
//return unique keys only
$invoicenrunique=array_keys(array_flip($uniquearray));
//count number of rows in unique array
$invoicenruniquecount=count(array_keys(array_flip($uniquearray)));
//do magic, this splits main array into n smaller arrays based on $_POST['invoicenr']
for($uniquerow = 0; $uniquerow < $invoicenruniquecount; $uniquerow++) {
$testarray = array();
for ($row = 0; $row < $rows; $row++) {
if($_POST['invoicenr'][$row]==$invoicenrunique[$uniquerow]){
$testarray[$row]['invoicenr']=$_POST['invoicenr'][$row];
$testarray[$row]['invoicedate']=$_POST['invoicedate'][$row];
$testarray[$row]['amount']=$_POST['amount'][$row];
$testarray[$row]['cause']=$_POST['cause'][$row];
}
}
print_r(array_values($testarray));
echo "<br>";
}
Do you have any suggestions on how to improve my solution?
Best regards!
I have this array:
Array (
[0] => Array ( [0] => b [1] => d [2] => c [3] =>a [4] => )
[1] => Array ( [0] => c [1] => a [2] => d [3] => [4] => )
[2] => Array ( [0] => b [1] => d [2] => a [3] => [4] => )
[3] => Array ( [0] => a [1] => d [2] => c [3] =>b [4] => )
)
and was wondering whether I can copy all inner array from it to another array where the first element is "b" so it looks like. Will the new array be reindexed when created? Thank you.
Array (
[0] => Array ( [0] => b [1] => d [2] => c [3] =>a [4] => )
[1] => Array ( [0] => b [1] => d [2] => a [3] => [4] => )
)
UPDATE: Had a little error in my code. Now it is fixed and working if you still need it.
$firstArray = array (array ('b', 'd', 'c', 'a'),
array ('c', 'd', 'a', 'b'),
array ('b', 'd', 'a', 'c'),
array ('a', 'd', 'c', 'b'));
$secondArray = array();
foreach($firstArray as $sub) {
if($sub[0] == 'b') {
$secondArray[] = $sub;
}
}
print_r($secondArray);
Output:
Array ( [0] => Array ( [0] => b [1] => d [2] => c [3] => a )
[1] => Array ( [0] => b [1] => d [2] => a [3] => c ) )
function check($val) {
return ($val[0] == 'b');
}
$secondArray = array_filter($firstArray, "check");
I have this array:
Array (
[0] => Array ( [0] => b [1] => d [2] => **c** [3] =>a [4] => )
[1] => Array ( [0] => **c** [1] => a [2] => d [3] => [4] => )
[2] => Array ( [0] => b [1] => d [2] => a [3] => [4] => )
[3] => Array ( [0] => **c** [1] => d [2] => a [3] =>b [4] => )
)
and need to delete (unset?) all elements where value is "c" so that one ends up with:
Array (
[0] => Array ( **[0] => b [1] => d [2] => a [3] => [4] =>** )
[1] => Array ( **[0] => a [1] => d [2] => [3] =>** )
[2] => Array ( [0] => b [1] => d [2] => a [3] => [4] => )
[3] => Array ( **[0] => d [1] => a [2] =>b [3] =>** )
)
The element gets removed, and the other elements to shift up. I know that unset does not re-index the array. Cannot get to unset for all multidimensional arrays, but only with one array. Can the arrays be re-indexed afterwards? Appreciate it.
The code BELOW removes elements where the value is equal to "c" but the index of the first element is not re-indexed. Can anyone suggest a solution to re-indexing the inner arrays?
$i=0;
foreach ($array as $val)
{
foreach ($val as $key => $final_val)
{
if ($final_val =="$search_value")
{
unset($array[$i][$key]);
}
}
i = $i + 1;
}
The following code will do what you want:
<?php
$a = 1;
$b = 2;
$c = 3;
$d = 4;
$arr = array(
array ( $b, $d, $c, $a, $b),
array ($c, $a),
array ( $b, $d, $c ),
array( $c, $d, $a, $b, $b)
);
echo "before:\n";
print_r($arr);
foreach($arr as $k1=>$q) {
foreach($q as $k2=>$r) {
if($r == $c) {
unset($arr[$k1][$k2]);
}
}
}
echo "after:\n";
print_r($arr);
?>
Output:
before:
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[2] => 3
[3] => 1
[4] => 2
)
[1] => Array
(
[0] => 3
[1] => 1
)
[2] => Array
(
[0] => 2
[1] => 4
[2] => 3
)
[3] => Array
(
[0] => 3
[1] => 4
[2] => 1
[3] => 2
[4] => 2
)
)
after:
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[3] => 1
[4] => 2
)
[1] => Array
(
[1] => 1
)
[2] => Array
(
[0] => 2
[1] => 4
)
[3] => Array
(
[1] => 4
[2] => 1
[3] => 2
[4] => 2
)
)
As you can see, all the 3's have gone...
Search the value in the sub array then unset it.
$search = 'c';
$result = array_map(function ($value) use ($search) {
if(($key = array_search($search, $value)) !== false) {
unset($value[$key]);
}
return $value;
}, $your_array);
Or you could use a loop too:
// this way change your original array
foreach ($your_array as &$sub_array) {
if(($key = array_search($search, $sub_array)) !== false) {
unset($sub_array[$key]);
}
}
var_dump($your_array);
This is an array from input field as below
Array(
[0] =>
[1] => 737
[2] => 736
[3] => 735
[4] =>
[5] => 748
[7] => 744
[8] => 747
[9] => 746
[10] =>
[11] => 748
[12] => 747
[13] => 746
[14] => 745
[15] => 744
);
Is there a way to divide it by empty value and removing the empty value as below:
Array(
[0] => Array(
[0] => 737
[1] => 736
[2] => 735
)
[1] => Array(
[0] => 748
[1] => 744
[2] => 747
[3] => 746
)
[2] => Array(
[0] => 748
[1] => 747
[2] => 746
[3] => 745
[4] => 744
)
)
If you want to split the array into sub-arrays with the "null" values as delimiters, something like that should do the job:
$newArray = array();
$subArray = null;
for ($i = 0; $i < count($array); $i++) {
if (!$subArray) $subArray = array();
$v = $array[$i];
if ($v) {
array_push($subArray, $v);
} else {
if (count($subArray) > 0) array_push($newArray, $subArray);
$subArray = null;
}
}
if ($subArray) array_push($newArray, $subArray);