Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => User Object
(
[original:protected] => Array
(
[user_id] => 123456
[active] => 1
[name] => ABC XYZ
[first_name] => ABC
[last_name] => XYZ
[email] => abc#xyz.com
[username] => abcxyz
[secret_code] => S4#$sdD
)
)
[1] => User Object
(
[original:protected] => Array
(
[user_id] => 987654
[active] => 1
[name] => CBD IHK
[first_name] => CBD
[last_name] => IHK
[email] => abc#xyz.com
[username] => seCdils
[secret_code] => S4#$sdD
)
)
)
)
Identify both object array has same secret_code return boolean if exist true else false tried array_count_values return only string and number please guide thanks
I just want to check if detect secret_code same in array give me true else false
<?php
$dupes = []; // keep track of duplicates
foreach ($users as $user1) { // iterate over all items
$dupeCount = 0; // because we iterate over the same array, we always find at least the item itself (1 dupe minimum)
foreach ($users as $user2) { // check the array again
if ($user1 === $user2) { // if they are exactly the same: http://php.net/manual/en/language.oop5.object-comparison.php
$dupeCount++;
}
if ($dupeCount > 1) { // because we always find at least 1, push only when we find more than that
array_push($dupes, $user1); // add it to the result
}
}
}
However for you comment
I just want to check if detect secret_code same in array give me true
else false
which should be in your question to begin with.
collect($users)->unique(function ($item) {
return $item['secret_code'];
});
Related
I am trying to using the in_array function to check the value exists in the second array or not. I want to search 556729685 from the following array:
$_SESSION["cart_item"] =
Array
(
[cart_item] => Array
(
[0] => Array
(
[product_name] => White Sakura Necktie
[id] => 11
[product_auto_id] => 556729685
[quantity] => 2
[product_regular_price] => 95
[product_sale_price] => 95
[product_image] => 556729680Black_Sakura_Necktie.jpg
)
[1] => Array
(
[product_name] => hhhad ba bhdbh
[id] => 10
[product_auto_id] => 951790801
[quantity] => 2
[product_regular_price] => 20
[product_sale_price] =>
[product_image] => 951790801hhhad_ba_bhdbh_.jpg
)
)
)
I am using following functions to check but it is giving wrong out put:
in_array(556729685, array_keys($_SESSION["cart_item"]));
I have tried this one also:
in_array(556729685, array_values($_SESSION["cart_item"]));
None is working so help me to solve this issue.
Associative array consist of key, column and values. So, in order to check existence of value you need to reach key of array.
for($i=0;$i<count($_SESSION["cart_item"]);$i++)
{
if( in_array( 556729685 ,$_SESSION["cart_item"][$i] ) )
{
echo "exist in id:".$i.'<br/>';
}
}
I am trying to add/update a new $key and $value to an existing array.
Form input:
<input name="flyer_packages[55][price][custom_price]" value="1000">
Current Array:
Array (
[55] => Array (
[date] => 10 October
[pricing_option] => true
[price] => Array (
[price_amount] => 3 000
[price_descriptor] => None
WP function to add new meta:
if (!empty ($_POST['flyer_packages'])) {
$flyer_packages = get_post_meta($pid, 'flyer_packages', true);
foreach ($flyer_packages as $flyer_package) {
foreach ($flyer_package[price] as $key => $value) {
update_post_meta( $pid, 'flyer_packages' , $_POST['flyer_packages']);
}
}
}
Expected Result:
Array (
[55] => Array (
[date] => 10 October
[pricing_option] => true
[price] => Array (
[price_amount] => 3 000
[price_descriptor] => None
[custom_price] => 1 000
Actual Result:
Array (
[55] => Array (
[price] => Array (
[custom_price] => 1 000
As you can see the result adds the new key and value but deletes all other keys and values in the array.
Can anybody please advise, much appreciated.
It happens because you're replacing the value, you have to merge arrays first
if (!empty ($_POST['flyer_packages'])) {
$flyer_packages = get_post_meta($pid, 'flyer_packages', true);
$new_value = $_POST['flyer_packages'];
custom_keys_recursive($new_value, $flyer_packages);
update_post_meta( $pid, 'flyer_packages', $flyer_packages);
}
function custom_keys_recursive($value, &$array) {
foreach ($value as $k=>$v) {
if (is_array($v)) {
custom_keys_recursive($v, $array[$k]);
} else {
$array[$k] = $v;
}
}
}
I have 2 table booking and message , now I want to show booking request and message in inbox at a time.
$this->paginate = array(
'conditions' => $conditions,'limit' =>10,
'order'=>array('Booking.created'=>'DESC'));
$bookings = $this->paginate('Booking');
$this->paginate = array(
'conditions' => $conditions,'limit' =>10,
'order'=>array('MessageDetail.created'=>'DESC'));
$messages = $this->paginate('MessageDetail');
i have merge both table data ( array_merge($bookings, $messages); )
now i want to it sort date wise (or any conditions)
Array
(
[0] => Array
(
[Booking] => Array
(
[id] => 4
[host_id] => 21
[place_id] => 10
[room_id] => 13
[user_id] => 12
[message_detail_id] => 16
[created] => 2013-04-23 14:44:03
[accept_date] =>
[cancel_date] =>
)
)
[1] => Array
(
[Booking] => Array
(
[id] => 3
[host_id] => 21
[place_id] => 10
[room_id] => 13
[user_id] => 12
[message_detail_id] => 13
[created] => 2013-04-15 14:10:59
[accept_date] => 2013-04-15 14:40:47
[cancel_date] =>
)
)
[2] => Array
(
[MessageDetail] => Array
(
[id] => 17
[message_id] => 2
[user_id] => 12
[sender_id] => 21
[unread] => 0
[created] => 2013-04-24 12:11:47
)
)
[3] => Array
(
[MessageDetail] => Array
(
[id] => 15
[message_id] => 2
[user_id] => 12
[sender_id] => 21
[booking_id] => 3
[unread] => 0
[created] => 2013-04-15 15:01:12
)
)
)
Thanks in advance.
Option #1:
Create a third Model named "BookingAndMessage". You could use the Model's afterSave method (on both Booking and Message) to create a duplicate record in your new model. You could then query the BookingAndMessage in the proper sort order.
Option #2: To solve the problem, as it stands, you will want to use PHP's usort function (also explained here PHP Sort a multidimensional array by element containing date).
<?php
$BookingsAndMessages = array_merge($bookings, $messages);
function date_compare($a, $b)
{
$modelKeyA = array_key_exists('Booking',$a) ? 'Booking' : 'MessageDetail';
$modelKeyB = array_key_exists('Booking',$b) ? 'Booking' : 'MessageDetail';
$t1 = strtotime($a[$modelKeyA]['created']);
$t2 = strtotime($b[$modelKeyB]['created']);
return $t1 - $t2;
}
usort($BookingsAndMessages, 'date_compare');
// Would sort the merged records by `created` date
?>
The downside to option 2 is, you are going to have to create rules for each field (and sort direction).
Perhaps you should Union your queries first, the Order.
$bookings = $this->Bookings->find('all', [
'conditions' => $conditions,'limit' =>10,
]);
$messages = $this->find('all', [
'conditions' => $conditions,'limit' =>10
]);
$data = $bookings->union($messages);
// This works if your first selected field is the one you want to sort.
$data->epilog('ORDER BY 1 ASC');
reference:
How do you modify a UNION query in CakePHP 3?
So here is my array, what I want to do is unset the [detail][$x] keys leaving just the lowest total. Can anyone assist? Thank you in advance..
There is alot of Products to loop though, what I mean is that its not just one item in the array.
[1] => Array
(
[name] => Product Name 1
[detail] => Array
(
[1] => Array
(
[total] => 10.14
)
[2] => Array
(
[total] => 12.18
)
[3] => Array
(
[total] => 9.90
)
You can find out the lowest total and overwrite the whole detail. Something like that:
$lowestValue = false;
foreach ($array[1]['detail'] as $detail) {
if ($lowestValue === false || $lowestValue > $detail['total']) {
$lowestValue = $detail['total'];
}
}
$array[1]['detail'] = array(0 => array('total' => $lowestValue));
I have a page for editing records of the Venue model in my app. This page was working at some stage, but is now broken.
in the controller action, debugging $this->data gives the expected array of form values. However, in the Venue model, debugging $this->data in beforeSave gives only the values for fields from a related (HABTM) model, Category:
app/models/venue.php (line 89)
Array
(
[Category] => Array
(
[Category] => Array
(
[0] => 1
[1] => 2
[2] => 8
)
)
)
What could be happening to this data between the form being submitted to the controller action, and the call to beforeSave? Where should I be looking to debug this?
THanks
Edit - here's what's in $this->data in the controller (actual data changed to remove phone numbers, addresses etc).
app/controllers/venues_controller.php (line 63)
Array
(
[Venue] => Array
(
[id] => 19
[city_id] => 1
[user_id] => 130
[name] => Acme Zoo
[email] => events#acmezoo.org.uk
[description] =>
Some text...
[blurb] => Truncated description...
[contact_id] =>
[address_1] => Acme Zoo
[address_2] => Some Road
[postcode] => PP9 4DD
[telephone] => 010101010101
[website] =>
[latitude] => 55.21222
[longtitude] => -2.111111
[featured] => 0
[active] => 1
[flagged] => 0
[smg] => 0
[smg_custom_icon] => 1
[listings] => 1
[send_email] => 0
[file] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
[Category] => Array
(
[Category] => Array
(
[0] => 3
[1] => 6
[2] => 10
)
)
)
And here's my code to save the data...
if (!empty($this->data)) {
if ($this->Venue->save($this->data)) {
$this->Session->setFlash('The venue has been saved','success');
$countryId = $this->Venue->City->field('country_id',array('id'=>$this->data['Venue']['city_id']));
if (!empty($this->data['Venue']['send_email'])){
$this->_emailVenue($this->Venue->id,'venue_added',$countryId);
}
$this->redirect(array('action' => 'index','city'=>$this->data['Venue']['city_id']));
} else {
$this->Session->setFlash('The venue could not be saved. Please, try again.','failure');
}
}
I think i found a solution to this but I am really unsure if this should be considered a "good" solution.
I backup the request data before the save and then restore it if it fails.
$temp = $this->request->data;
if ($this->Post->save($this->request->data)) {
}else{
$this->request->data = $temp;
}
Maybe a stupid question, but do you pass the content of controller $data to the model when you call the save() method ?
$this->Venue->save($this->data)
Are you trying to save an entry to the categories table at the same time? If so, you can use $this->Venue->saveAll($this->data) instead of save(). If you just want to save the Venue data, just pass that in to save() instead of the entire $this->data like this: $this->Venue->save($this->data['Venue']);