How can i remove duplicate object from my array in php? [duplicate] - php

This question already has answers here:
array_unique for objects?
(15 answers)
PHP Removing duplicate objects from array
(3 answers)
Remove objects with duplicate values in array of stdClass objects
(3 answers)
Closed 4 months ago.
here I need to remove the same week id object array from my main array in PHP
[{"week_id":""},
{"week_id":"23","location_schedule":"Tuesday","location_date_from":"10\/18\/2022 4:30 PM"},
{"week_id":"23","location_schedule":"Tuesday","location_date_from":"10\/18\/2022 4:30 PM"},
{"week_id":"24","location_schedule":"Tuesday","location_date_from":"10\/25\/2022 3:30 PM"},
{"week_id":"24","location_schedule":"Tuesday","location_date_from":"10\/25\/2022 3:30 PM"}]
$weeksArray = array(array("week_id"=>""));
$i=0;
foreach($orderweek as $weeks)
{
$weeksArray[]=[
'week_id'=>$weeks['week_id'],
'location_schedule'=>$weeks['location_schedule'],
'location_date_from'=>$weeks['location_date_from']
];
echo "<br>";
// }
$i++;
}
// $weeksArray = array(array("week_id"=>"25"));
echo json_encode($weeksArray);

You already tried array_unique()?
If not Put array_unique($weeksArray, SORT_REGULAR) before json_encode
Here is documentation.
https://www.php.net/manual/en/function.array-unique.php

Related

How do I extracting the multidimensional array value in php? [duplicate]

This question already has answers here:
Looping a multidimensional array in php
(3 answers)
Output multidimensional array with keys and values
(3 answers)
Closed 4 months ago.
How do I get the value of the associative arrays for the year and quarter in a loop in php? I want to extract the numbers like 2021 4, the other value doesn't matter.
$this->financials[2021][4] = 5;
$this->financials[2022][1] = 7;
$this->financials[2022][2] = 9;
$this->financials[2022][3] = 11;
I attempted this method, but it didn't work, but hopefully gives you a better idea of what I am trying to achieve. I was hoping for the result e.g. 20214
foreach($this->financials as $f[$y][$q]) {
echo $y.$q;
}
$q in your foreach loop is an associative array so you have to break it down with another loop.
foreach ($this->financials as $year => $quarterArray) {
foreach ($quarterArray as $quarter => $value) {
echo $year.$quarter;
}
}

Can I make the contents of my array all lowercase? [duplicate]

This question already has answers here:
How to convert array values to lowercase in PHP?
(10 answers)
Closed 5 years ago.
I have this array and I'm currently researching on how to make the contents of the output lowercase.
foreach ($city_info['links'] as $bikes => $url) {
echo '<span> '.$bikes.'</span>';
}
echo '<span> '. strtolower($bikes) .'</span>';
https://www.w3schools.com/php/func_string_strtolower.asp

Sort 1 array from another array [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 7 years ago.
I have below 2 array values.
Array 1 - 2,1,3,0
Array 2 - 7,1,5,10.
Now i want 2 array like below.
Array 1 - 0,1,2,3
Array 2 - 10,1,7,5
You could try sorting by value the first array, but maintain the index association. Then use the new order of keys to sort the other array:
asort($arr1);
$sorted_arr2 = [];
foreach($arr1 as $key=>$val) {
array_push($sorted_arr2, $arr2[$key]);
}

How to do a for each from end of the array in PHP [duplicate]

This question already has answers here:
Iterate in reverse through an array with PHP - SPL solution?
(11 answers)
Closed 7 years ago.
How to do a "for each" from the end of an array in PHP ?
The end function can only return me one element http://php.net/manual/fr/function.end.php
Reverse the array and do a normal foreach?
$newArray = array_reverse($array);
foreach ($newArray as $key => $value) {
...
}

How to add a prefix before an array [duplicate]

This question already has answers here:
Add a prefix to each item of a PHP array
(7 answers)
Closed 9 years ago.
I have an array in PHP:
$pbx01_connection = array("customer/voip_extensions.php");
how can i add a prefix and suffix to each item in the array?
for example,
$pbx01_connection = '/admin/'.array("customer/voip_extensions.php");
so /admin/ is added before each item in the array?
Use array_map():
<?php
function addPrefix($value)
{
return '/admin/' . $value
}
$new_array = array_map("addPrefix", $array);
print_r($new_array);
?>

Categories