Get the value from the given array - arrays

I have this given array ,
How can get the value of email and status in loop .
Array
(
[0] => Array
(
[brandlogo_id] => 1
[email] => upendtu#gmail.com
[status] => 2
[created_time] => 2017-11-13 14:00:10
[update_time] => 2017-11-13 14:00:10
)
[1] => Array
(
[brandlogo_id] => 2
[email] => upen3592#gmail.com
[status] => 1
[created_time] => 2017-11-13 13:58:46
[update_time] => 2017-11-13 13:58:46
)
[2] => Array
(
[brandlogo_id] => 3
[email] => ankigupta9492#gmail.com
[status] => 1
[created_time] => 2017-11-13 13:57:07
[update_time] => 2017-11-13 13:57:07
)
)
please let me know how can i get this value
thank you.

Be it any language, the method/ also will be more or less the same apart from the syntax. Search the syntax for the language you are using to parse JSON data.
Giving examples.
For Objective - C:
NSArray *resultDataSet=[NSArray array];
while([results next]) {
NSString *name = [results stringForColumn:#"brand_name"];
resultDataSet = [resultDataSet arrayByAddingObject:name ];
}
For Python :
for array in data:
print array['email']
For PHP :
foreach($arr as $item) { //foreach element in $array
$stat = $item['status']; //etc
}
Hope that helps.

Assuming that you are using PHP or python (as this is an associative array) and the name of the array is "arr".
This code shall do the job.
For PHP
foreach($arr as $subarray) {
echo $subarray["email"]." ".$subarray["status"]."<br>";
}
For Python
for subarray in arr:
print subarray["email"], " ", subarray["status"]
It will display output similar to
xyz#gmail.com 2
abc#yahoo.com 3

Related

Check duplicate entry into object array php laravel

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'];
});

Array reduce find the small distance and return id

I use google matrix for calculate the best distance with the multiple address.
I want display the id of a small distance
My array return by the API
$com = Array(
[0] => Array (
[id] => 12
[km] => 833 km
)
[1] => Array (
[id] => 4
[km] => 546km
)
[1] => Array (
[id] => 45
[km] => 1200km
)
)
I want display the ID of small distance ( for this exemple the id is "4")
I have tried this :
$min = array_reduce($commercant,
function ($min, $item) {
if ($item['km'] < $min['km']) {
return $item;
}
return $min;
},
array('id' => -1, 'km' => PHP_INT_MAX));
echo $min['id'];
This code doesn't work and i don't know why !
if you have an idea....
Thx
use this code
usort($com, function($a, $b) {
return $a['km'] - $b['km'];
});
echo !empty($com[0]['id']) ? $com[0]['id'] : "";
it will sort your array in desc order of distance, from this sorted array you can pick first element of array.
Hope this will help you!
With ordinary sorting, you can achieve result.
http://php.net/manual/en/function.usort.php
$x = [
['id' => 12, 'km' => '833km'],
['id' => 4, 'km' => '546km'],
['id' => 45, 'km' => '1200km']
];
function cmp_by_kms($arr1, $arr2) {
$km1 = intval($arr1['km']);
$km2 = intval($arr2['km']);
if ($km1 == $km2) { return 0; }
return ($km1<$km2) ? -1 : 1;
}
usort($x, "cmp_by_kms");
print_r($x[0]);
#=> Array
(
[id] => 4
[km] => 546km
)

A hash::combine array

I have this array from a sql query :
[0] => Array
(
[T1] => Array
(
[First] => A
[Second] => Apples
[LastChild] => F
)
[0] => Array
(
[LastChildNb] => 23
)
)
I would like to have this result :
[0] => Array
(
[0] => Array
(
[First] => A
[Second] => Apples
[LastChild] => F
[LastChildNb] => 23
)
)
How do I do this ? I think I should use "hash::combine", but what would the code be ?
You could do something like this, with $arr being your array above:
$arr = array_reduce($arr, function(&$arr, $v) {
return array_merge($arr, (array) $v);
}, array());
You can do
array_merge($arr[0]['T1'], $arr[0][0])
Where $arr is defined as follows:
$arr = [0 => Array
(
'T1' => Array
(
'First' => 'A',
'Second' => 'Apples',
'LastChild' => 'F'
),
0 => Array
(
'LastChildNb' => 23
)
)];
For multiple records in $arr, you can simply cycle over all records and do this merge manually.
However, assuming you're getting the array as a result of a find() method, I suggest you consider using T1__LastChildNb as an alias within the 'fields' of your condition. Simply said, if you have a find like following:
$this->T1->find('all', ['fields' => 'T1.*, (SOME SUBQUERY) AS LastChildNb']);
then modifying it to be
$this->T1->find('all', ['fields' => 'T1.*, (SOME SUBQUERY) AS T1__LastChildNb']);
might be what you're looking for since it will return desired array directly (tested on 2.6).
Let me know if you're interested in more information.

how to sort array date wise in cakephp

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?

Convert 3 dimension to 2 dimension

I got a query result like this : -
Array(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => Japan
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => Nepal
)
)
}
How can I convert it to
Array
(
[0] => Japan
[1] => Nepal
)
The query used to get first array is 'select id , name from country;'
Please help me.
foreach ($element as $array) {
$element = $element[0]["name"];
}
Or,
function extractName($element) {
return $element[0]["name"];
}
$newArray = array_map("extractName", $array);
Extract!
$newArray = Set::extract($oldArray, '{n}.0.name');
More here.

Resources