<?php
Class A {
public static array $data = ["a" => [0, 1], "c" => [10, 1]];
public static function getData($a): array {
return self::$data[$a];
}
}
// FIRST
print_r(A::$data);
print("<BR>");
// SECOND
$c = A::getData("c");
$c[0] = [20];
print_r(A::$data);
print("<BR>");
// THIRD
A::$data["c"] = [20];
print_r(A::$data);
print("<BR>");
RESULT
Array ( [a] => Array ( [0] => 0 [1] => 1 ) [c] => Array ( [0] => 10 [1] => 1 ) )
Array ( [a] => Array ( [0] => 0 [1] => 1 ) [c] => Array ( [0] => 10 [1] => 1 ) )
Array ( [a] => Array ( [0] => 0 [1] => 1 ) [c] => Array ( [0] => 20 [1] => 1 ) )
SECOND didn't work.
So I changed getData($a) with &getData($a)
But still not solved.
WHY?
Wasn't it "return by reference" error?
How can I fix it?
Related
I have two associative arrays like
Array
(
[0] => Array
(
[0] => 2022-01-19
[1] => 6
)
[1] => Array
(
[0] => 2022-01-20
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-22
[1] => 2
)
)
and
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 1
)
[2] => Array
(
[0] => 2022-01-21
[1] => 1
)
[3] => Array
(
[0] => 2022-01-23
[1] => 2
)
)
I need to merge them with the date and want a result array-like below
Array
(
[0] => Array
(
[0] => 2022-01-17
[1] => 0
[2] => 6
)
[1] => Array
(
[0] => 2022-01-18
[1] => 0
[2] => 1
)
[2] => Array
(
[0] => 2022-01-19
[1] => 6
[2] => 0
)
[3] => Array
(
[0] => 2022-01-20
[1] => 1
[2] => 0
)
[4] => Array
(
[0] => 2022-01-21
[1] => 1
[2] => 1
)
[5] => Array
(
[0] => 2022-01-22
[1] => 2
[2] => 0
)
[6] => Array
(
[0] => 2022-01-23
[1] => 0
[2] => 2
)
)
I tried with the below code but not any success.
$final_array = [];
foreach($openTicket as $val){
$closeTicketNo = 0;
foreach($closeTicket as $value){
if($val[0] == $value[0]){
$closeTicketNo = $value[1];
}
}
$final_array[] = [$val[0],$val[1],$closeTicketNo];
}
I get all the elements from the $openTicket but not get all the elements from a $closeTicket to my result array $final_array
This code first finds all of the unique dates (using array_unique) from the first values in each array (array_column fetches the values and array_merge puts them into 1 array).
Then it indexes each array by the dates (using array_column again).
Finally looping through the unique dates and adding a new element to the output with the values (using ?? 0 so that if no value is present the array is still filled properly)...
$dates = array_unique(array_merge(array_column($openTicket, 0), array_column($closedTicket, 0)));
$open = array_column($openTicket, 1, 0);
$closed = array_column($closedTicket, 1, 0);
$finalArray = [];
foreach ($dates as $date) {
$finalArray[] = [$date, $open[$date] ?? 0, $closed[$date] ?? 0];
}
You can try like this
$array1 = [
['2022-01-19',6],
['2022-01-20',1],
['2022-01-21',0]
];
$array2 = [
['2022-01-17',6],
['2022-01-20',2],
['2022-01-21',1]
];
function mergeMultiple($array1,$array2){
foreach($array1 as $item1){
$mergedArray[$item1[0]] = $item1;
}
foreach($array2 as $item2){
if(isset($mergedArray[$item2[0]])){
array_push($mergedArray[$item2[0]],$item2[1]);
$mergedArray[$item2[0]] = $mergedArray[$item2[0]];
}else{
$mergedArray[$item2[0]] = $item2;
}
}
return array_values($mergedArray);
}
$mergedArray = mergeMultiple($array1,$array2);
ksort($mergedArray);
print_r($mergedArray);
I have two array two array. First is multidimensional and other is single dimensional. I want to find difference between them. How do I found.
$arrayresult
Array 1
Array (
[0] => Array ( [0] => ishani.lad [1] => 9033187384 )
[1] => Array ( [0] => rajkumar.prajapati [1] => 8460078459 )
[2] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[3] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[4] => Array ( [0] => vishal.dake [1] => 9879815299 )
[5] => Array ( [0] => mohsin [1] => 8347163123 )
)
$useduser
Array 2
Array (
[0] => ishani.lad
[1] => rajkumar.prajapati
[2] => lokesh.bhandari
)
I need difference as result as below
Result
Array (
[0] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[1] => Array ( [0] => vishal.dake [1] => 9879815299 )
[2] => Array ( [0] => mohsin [1] => 8347163123 )
)
I have used solution as
$resultremainig = [];
foreach($arrayresult as $val2){
if(!in_array($val2[0], $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
}
But it show last record also. Result of above code is as below. It always show me last record in second array's also
Array (
[0] => Array ( [0] => lokesh.bhandari [1] => 9687060900 )
[1] => Array ( [0] => shishanshu.rai [1] => 8401915337 )
[2] => Array ( [0] => vishal.dake [1] => 9879815299 )
[3] => Array ( [0] => mohsin [1] => 8347163123 )
)
If you wanted you could try using nested loops like so:
<?php
$arrOne = $arrFinal = [
["ishani.lad", 9033187384],
["rajkumar.prajapati", 8460078459],
["lokesh.bhandari" , 9687060900],
["shishanshu.rai" , 8401915337],
["vishal.dake" , 9879815299],
["mohsin" , 8347163123],
];
$arrTwo = [
"ishani.lad",
"rajkumar.prajapati",
"lokesh.bhandari",
];
foreach($arrOne as $key=>$item){
foreach($arrTwo as $k=>$v){
if(in_array($v, $item)){
unset($arrFinal[$key]);
}
}
}
var_dump($arrFinal);
// PRODUCES:::
array (size=3)
3 =>
array (size=2)
0 => string 'shishanshu.rai' (length=14)
1 => int 8401915337
4 =>
array (size=2)
0 => string 'vishal.dake' (length=11)
1 => int 9879815299
5 =>
array (size=2)
0 => string 'mohsin' (length=6)
1 => int 8347163123
You could use array_filter():
$output = array_filter($arrayresult, function($a) use ($useduser) {
return !in_array($a[0], $useduser);
});
Hi You can also try this
$one = array(array('ishani.lad',9033187384),array('rajkumar.prajapati',8460078459),array('lokesh.bhandari',9687060900),array('shishanshu.rai',8401915337),array('vishal.dake',9879815299),array('mohsin',8347163123));
$two = array('ishani.lad','rajkumar.prajapati','lokesh.bhandari');
foreach($one as $array){
if(!in_array($array[0],$two)){
$final[] = $array;
}
}
echo "<pre>";print_r($final);
Output
Array
(
[0] => Array
(
[0] => shishanshu.rai
[1] => 8401915337
)
[1] => Array
(
[0] => vishal.dake
[1] => 9879815299
)
[2] => Array
(
[0] => mohsin
[1] => 8347163123
)
)
Trim the value before checking in $useduser array
$resultremainig = [];
foreach($arrayresult as $val2){
// this removes any extra spaces from the search string
if(!in_array(trim($val2[0]), $useduser)){
echo $val2[0]."<br>";
$resultremainig[] = $val2;
}
You need to use the array_diff function.
Store your 2 arrays in variables and compare them.
I have two arrays and looking for the way to merge them. Standard array_merge() function don't work.
Do you know any nice solution without foreach iteration?
My first array:
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] =>
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] =>
)
)
My second array:
Array
(
[0] => 2
[1] => 7
)
And as a result I would like to get:*
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] => 7
)
)
This should work for you:
Just simply loop through both arrays with array_map() and pass the argument from array one as reference. Then you can simply assign the value to the count property.
<?php
array_map(function(&$v1, $v2){
$v1->count = $v2;
}, $arr1, $arr2);
print_r($arr1);
?>
output:
Array
(
[0] => stdClass Object
(
[field_value] => Green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => Yellow
[count] => 7
)
)
[akshay#localhost tmp]$ cat test.php
<?php
$first_array = array(
(object)array("field_value"=>"green","count"=>null),
(object)array("field_value"=>"yellow","count"=>null)
);
$second_array = array(2,7);
function simple_merge($arr1, $arr2)
{
return array_map(function($a,$b){ $a->count = $b; return $a; },$arr1,$arr2);
}
print_r($first_array);
print_r($second_array);
print_r(simple_merge($first_array,$second_array));
?>
Output
[akshay#localhost tmp]$ php test.php
Array
(
[0] => stdClass Object
(
[field_value] => green
[count] =>
)
[1] => stdClass Object
(
[field_value] => yellow
[count] =>
)
)
Array
(
[0] => 2
[1] => 7
)
Array
(
[0] => stdClass Object
(
[field_value] => green
[count] => 2
)
[1] => stdClass Object
(
[field_value] => yellow
[count] => 7
)
)
it is simple
code:
$i = 0;
foreach($firstarrays as $firstarr)
{
$firstarr['count'] = $secondarray[$i];
$i++;
}
Another option:
$a1 = Array(
(object) Array('field_value' => 'Green', 'count' => null),
(object) Array('field_value' => 'Yellow', 'count' => null)
);
$a2 = Array(2, 7);
for ($i=0; $i<sizeof($a1); $i++) {
$a1[$i]->count=$a2[$i];
}
This question already has answers here:
How to use return inside a recursive function in PHP
(4 answers)
Closed 9 months ago.
Given groups of pairs in each level of the structure below, I want to find if there's a "loop" of connecting points. (if "b" on level 0 has a match on "a" on the next level).
I'm using a recursive function to test it and it's working fine. But when I test the return value it fails:
The code:
$tracker=array();
array_push($tracker, 0);
if (findloop(1, $candidates[0]['pairs'][0]['b'])){
echo "<h1>a path has been found</h1><pre>";
print_r($tracker);
echo "</pre>";
}else{
echo "<h3>no loop found</h3><pre>";
print_r($tracker);
echo "</pre>";
}
The function:
function findloop($level, $value){ // so far.. only progression...
echo "<p>level $level, $value</p>";
global $candidates;
global $tracker;
foreach($candidates[$level]['pairs'] as $key=>$pair){
if($pair['a']==$value){
array_push($tracker, $key);
if($level==sizeof($candidates)-1){
echo "omgggggg";
return true;
}else{
findloop($level+1, $pair['b']);
}
}
}
}
The result:
level 1, 19
level 2, 15
level 3, 18
omgggggg
no loop found < - - - - - - - function does (?) return true but it fails
Array
(
[0] => 0
[1] => 1
[2] => 0
[3] => 1
)
The structure:
Array
(
[0] => Array
(
[angle] => 41.7
[pairs] => Array
(
[0] => Array
(
[a] => 6
[b] => 19
)
[1] => Array
(
[a] => 19
[b] => 6
)
)
)
[1] => Array
(
[angle] => 11.8
[pairs] => Array
(
[0] => Array
(
[a] => 15
[b] => 19
)
[1] => Array
(
[a] => 19
[b] => 15
)
)
)
[2] => Array
(
[angle] => 14.3
[pairs] => Array
(
[0] => Array
(
[a] => 15
[b] => 18
)
[1] => Array
(
[a] => 16
[b] => 17
)
[2] => Array
(
[a] => 17
[b] => 16
)
[3] => Array
(
[a] => 18
[b] => 15
)
)
)
[3] => Array
(
[angle] => 29.5
[pairs] => Array
(
[0] => Array
(
[a] => 6
[b] => 18
)
[1] => Array
(
[a] => 18
[b] => 6
)
)
)
)
The initial return is fine but the calls further up the chain aren't getting this result. In your else you need to return the result:
if($level==sizeof($candidates)-1){
echo "omgggggg";
return true;
}else{
return findloop($level+1, $pair['b']);
}
I am having an array like this.
Array
(
[0] => Array
(
[0] => a~226
[1] => a~228
)
[1] => Array
(
[0] => b~123
[1] => b~209
)
[2] => Array
(
[0] => c~161
[1] => c~140
)
)
I want to explode this array using ~ symbol and i want value to be a key in php array.i want an array like this.Kindly help me write the code.
Array
(
[0] => Array
(
[a] => 226
[a] => 228
)
[1] => Array
(
[b] => 123
[b] => 209
)
[2] => Array
(
[c] => 161
[c] => 140
)
)
Thanks in advance...
You cannot have such an array.
The keys must be unique (Like Mark Baker say).
You can have something like this:
Array
(
[a] => Array
(
[0] => 226
[1] => 228
)
[b] => Array
(
[0] => 123
[1] => 209
)
[c] => Array
(
[0] => 161
[1] => 140
)
)
The code to do this:
$array = array(
array("a~226", "a~228"),
array("b~123", "b~209"),
array("c~161", "c~140")
);
$result = array();
foreach($array as $inner_array) {
foreach($inner_array as $value) {
$spitted = explode("~", $value);
$result[$spitted[0]][] = end($spitted);
}
}
An working example: http://codepad.viper-7.com/znhhqB
try this
$arr_new = array();
foreach($arr_main as $key=>$arr)
{
foreach($arr as $k=>$val)
{
$str = explode("~",$val);
$arr_new[$key][$str[0].$k]=$str[1];
}
}
will maintain the index as a0, a1