How should I sort this array? - php

I'm currently trying to implement a messaging functionality in my system. My goal right now is having a jQuery popup dialog which displays the name of users that have messaged the other person and then clicking on that person's name will show the new messages. I've done the popup dialog bit so my next step is getting and sorting the messages from database.
What I've done now is retrieve the new messages from database. I am unsure of how to proceed as my array of unread messages contains the element of the sender's id more than once. I do not want to loop through my unread messages and then get the same sender id again and again and as such retrieve their details from database again and again.
Here's an example of my array:
array(8) {
[6]=>
array(7) {
["id"]=>
string(2) "52"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(10) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561645"
["messagetype"]=>
string(6) "direct"
}
[7]=>
array(7) {
["id"]=>
string(2) "53"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "bye"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561648"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
[3]=>
array(7) {
["id"]=>
string(2) "42"
["useridfrom"]=>
string(3) "243"
["useridto"]=>
string(3) "139"
["message"]=>
string(4) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1335517653"
["messagetype"]=>
string(6) "direct"
}
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
array(8) {
[6]=>
array(7) {
["id"]=>
string(2) "52"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(10) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561645"
["messagetype"]=>
string(6) "direct"
}
[7]=>
array(7) {
["id"]=>
string(2) "53"
["useridfrom"]=>
string(3) "330"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "bye"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336561648"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
[3]=>
array(7) {
["id"]=>
string(2) "42"
["useridfrom"]=>
string(3) "243"
["useridto"]=>
string(3) "139"
["message"]=>
string(4) "test"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1335517653"
["messagetype"]=>
string(6) "direct"
}
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
[2]=>
array(7) {
["id"]=>
string(2) "10"
["useridfrom"]=>
string(3) "138"
["useridto"]=>
string(3) "139"
["message"]=>
string(54) "Hi Emma thank you for submitting your homework - Jenny"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1309122193"
["messagetype"]=>
string(6) "direct"
}
}
This is what I've done in PHP so far:
$m = new Messaging();
$json = $m->getUnreadMessages($uid);
/** sort messages by useridfrom **/
uasort($json, array($m, 'messageCompare'));
foreach($json as $j)
{
//do sorting of messages here ??
}
echo json_encode($json);
exit;
I guess ideally, I would want to create a new array out of this where the key is useridfrom (sender) and then their messages and then associated with each message the timecreated. What is the best way to do this?
Thanks in advance.
EDIT
As you can see from the array example there are duplicate useridfrom elements where useridfrom = 241 twice. This means that the user 241 has sent two messages, if there were 3 useridfrom 241 elements then that means he/she sent 3 messages. My goal is two group the useridfrom together as keys in an array so there are no duplicates then associated with that key we have all their messages (message) and the time the message was created (timecreated).
[4]=>
array(7) {
["id"]=>
string(2) "46"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(8) "sdsdfsdf"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336139572"
["messagetype"]=>
string(6) "direct"
}
[5]=>
array(7) {
["id"]=>
string(2) "47"
["useridfrom"]=>
string(3) "241"
["useridto"]=>
string(3) "139"
["message"]=>
string(13) "8528528285285"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1336143958"
["messagetype"]=>
string(6) "direct"
}
[1]=>
array(7) {
["id"]=>
string(2) "30"
["useridfrom"]=>
string(3) "329"
["useridto"]=>
string(3) "139"
["message"]=>
string(243) "Hi Bob"
["format"]=>
string(1) "0"
["timecreated"]=>
string(10) "1330942044"
["messagetype"]=>
string(6) "direct"
}
So from this example, it should lead to an array of something like what mariomario has suggested:
array{
['241'] => array(
[0] => array(time=>'time1', message => "hello i am 241"),
[1] => array(time=>'time2', message => "nice to meet you"),
),
['330'] => array(
[0] => array(time=>'time1', message => "hello i am 330"),
[1] => array(time=>'time2', message => "goodbye"),
)
}

EDITED:
After a little pondering I'd do this:
$data = array();
foreach ($json as $j) {
if (!array_key_exists($j['useridfrom'], $data)) {
$data[$j['useridfrom']] = array();
}
if (!array_key_exists($j['useridto'], $data[$j['useridfrom']])) {
$data[$j['useridfrom']][$j['useridto']] = array();
}
$data[$j['useridfrom']][$j['useridto']][] = array(
'message' => $j['message'],
'timestamp' => $j['timecreated']
);
}
Keep your data ordered like so ORDER BY timecreated DESC and that concats everything like you want ordered after the time.
This should net an array like so:
'sender1' => (
'recipient1' = (
(
'message' => 'MESSAGE HERE',
'timecreated' => 'TIME HERE'
),
(
'message' => 'ANOTHER MESSAGE HERE',
'timecreated' => 'ANOTHER TIME HERE'
)
),
'recipient2' = (
(
'message' => 'MESSAGE HERE',
'timecreated' => 'TIME HERE'
)
)
),
'sender2' => (
)

wouldn't you do something like this
$newArray = array()
foreach($json as $j)
{
$array[$j['useridfrom']][] = array('time'=>$j['timecreated'],
'message'=>$j[message]);
}
they you will have a array like this:
array{
['user1'] => array(
[0] => array(time=>'time1', message => message1),
[1] => array(time=>'time2', message => message2),
),
['user2'] => array(
[0] => array(time=>'time1', message => message1),
[1] => array(time=>'time2', message => message2),
)
}
but maybe I am understanding you not correct.

Use array_multisort in PHP:
http://php.net/manual/en/function.array-multisort.php

Related

PHP array sorting associative array using a specific key value

I have an array which need to sort using a key value of "odbyx"
Here is the array var_dump
array(12) {
["id"]=> array(7) {
[0]=> string(1) "8"
[1]=> string(1) "7"
[2]=> string(1) "3"
[3]=> string(1) "6"
[4]=> string(1) "5"
[5]=> string(1) "2"
[6]=> string(1) "1"
}
["subject"]=> array(7) {
[0]=> string(14) "ticke tick sbj"
[1]=> string(13) "new tick test"
[2]=> string(15) "fdsfdsfdsfdsfds"
[3]=> string(12) "test subject"
[4]=> string(4) "test"
[5]=> string(4) "test"
[6]=> string(12) "test subject"
}
["msg"]=> array(7) {
[0]=> string(9) "test+tick"
[1]=> string(4) "test"
[2]=> string(9) "dfdsfdsfd"
[3]=> string(12) "test+tcikets"
[4]=> string(4) "test"
[5]=> string(12) "test+message"
[6]=> string(7) "tssssss"
}
["department"]=> array(7) {
[0]=> string(10) "Technician"
[1]=> string(5) "Admin"
[2]=> string(5) "Admin"
[3]=> string(10) "Technician"
[4]=> string(10) "Technician"
[5]=> string(5) "Admin"
[6]=> string(5) "Admin"
}
["priorety"]=> array(7) {
[0]=> string(3) "Low"
[1]=> string(6) "Normal"
[2]=> string(3) "Low"
[3]=> string(3) "Low"
[4]=> string(4) "High"
[5]=> string(6) "Normal"
[6]=> string(3) "Low"
}
["status"]=> array(7) {
[0]=> string(4) "open"
[1]=> string(8) "answered"
[2]=> string(8) "answered"
[3]=> string(4) "open"
[4]=> string(4) "open"
[5]=> string(4) "open"
[6]=> string(6) "closed"
}
["dateAded"]=> array(7) {
[0]=> string(19) "2017-10-01 12:34:56"
[1]=> string(19) "2017-09-27 13:41:09"
[2]=> string(19) "2017-09-17 13:53:04"
[3]=> string(19) "2017-09-25 15:00:48"
[4]=> string(19) "2017-09-23 10:41:24"
[5]=> string(19) "2017-09-17 13:31:56"
[6]=> string(19) "2017-09-17 12:37:22"
}
["dateClosed"]=> array(7) {
[0]=> string(19) "0000-00-00 00:00:00"
[1]=> string(19) "0000-00-00 00:00:00"
[2]=> string(19) "0000-00-00 00:00:00"
[3]=> string(19) "0000-00-00 00:00:00"
[4]=> string(19) "0000-00-00 00:00:00"
[5]=> string(19) "2017-09-30 13:41:09"
[6]=> string(19) "2017-09-17 13:40:53"
}
["dateActivity"]=> array(7) {
[0]=> string(19) "2017-10-01 12:34:56"
[1]=> string(19) "2017-10-01 07:49:20"
[2]=> string(19) "2017-09-26 10:35:36"
[3]=> string(19) "2017-09-25 15:00:48"
[4]=> string(19) "2017-09-23 10:41:24"
[5]=> string(19) "2017-09-17 13:41:21"
[6]=> string(19) "2017-09-17 13:40:53"
}
["userId"]=> array(7) {
[0]=> string(1) "4"
[1]=> string(1) "4"
[2]=> string(1) "3"
[3]=> string(1) "4"
[4]=> string(1) "7"
[5]=> string(1) "3"
[6]=> string(1) "2"
}
["res"]=> string(4) "true"
["odbyx"]=> array(7) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
[3]=> int(2)
[4]=> int(3)
[5]=> int(3)
[6]=> int(3)
}
}
Logic behind the array
$array = array
(
"id" => $id,
"subject" => $subject,
"msg" => $msg,
"department" => $department,
"priorety" => $priorety,
"status" => $status,
"dateAded" => $dateAded,
"dateClosed" => $dateClosed,
"dateActivity" => $dateActivity,
"userId" => $userId,
"res" => $res,
"odbyx"=>$odbyx
);
I tried sort using below code but it seems not working
array_multisort(array_column($array, 'odbyx'), SORT_DESC, $array);
odbyx field hold a priority values (1,2,3).
i need to sort main array using that values, so i can display top priority fields at the beginning soo on a table
I want to sort the $array using "odbyx" descending order
Example Output of id array, the other arrays also need to sort at once.
["id"]=> array(7) { [0]=> string(1) "6" [1]=> string(1) "5" [2]=> string(1) "4" [3]=> string(1) "3" [4]=> string(1) "7" [5]=> string(1) "6" [6]=> string(1) "8" }
proper use of usort
See also: Sort Multi-dimensional Array by Value
your example is invalid
Expected first entry in result: 0 => '6'
Possible interpretation odbyx-index == id-index
Should indices in odbyx match to indices in id?
i.e.: $array['id'][3] should be sorted by $array['odbyx'][3]
If this is the case, then your provided code should either yield 0 => '8' for the first index (odbyx 1 higher-priority than 3), or 0 => '1' (3 is higher).
Possible interpretation odbyx-index == id-value
Should the index in odbyx match to the id values in id?
i.e.: The value of $array['odbyx'][1] determines the sort for $array['id'][6] = '1'
In this case, the result should be 0 => '2'
None of these possible interpretations even match the very first result in your example. The lesson here is specification, i.e.: carefully define and describe the specific conditions required to solve your problem, on stackoverflow or anywhere else.
Here's a place to start
Since the problem you are asking to be solved is complex, poorly defined, would require a significant amount of coding and testing, and has significant performance implications, I'll leave you with this tidbit of a solution to one of the impossible interpretations above. Good luck.
Class SimpleSorter
{
private $orderBy;
private $sortMe;
public static function sortByIndexedOrderField(array $sortMe, array $byMe)
{
$sorter = new self($sortMe);
return $sorter->applyIndexedOrder($byMe);
}
public function __construct(array $sortMe)
{
$this->sortMe = $sortMe;
}
public function applyIndexedOrder(array $byMe): array
{
$this->orderBy = $byMe;
$keys = array_keys($this->sortMe);
// sort, first by odbyx, then by value
usort($keys, function($a,$b){
$odbyx = 0;
if (array_key_exists($a, $this->orderBy) && array_key_exists($b, $this->orderBy)) {
$odbyx = $this->orderBy[$b] <=> $this->orderBy[$a];
}
if (0 !== $odbyx) {
return $odbyx;
}
return $this->sortMe[$a] <=> $this->sortMe[$b];
});
// reorder by new key order
$result = [];
foreach($keys as $key) {
$result[$key] = $this->sortMe[$key];
}
return $result;
}
}
$array = [];
$array["id"] = [
0 => '8',
1 => '7',
2 => '3',
3 => '6',
4 => '5',
5 => '2',
6 => '1',
];
$array["odbyx"] = [
0 => 1,
1 => 2,
2 => 3,
3 => 2,
4 => 3,
5 => 3,
6 => 3,
];
$idsSorted = SimpleSorter::sortByIndexedOrderField($array["id"], $array["odbyx"]);
print_r($idsSorted);

using array_unique, I appear to have stripped out the records that were duplicated?

Within available_options I have somehow stripped out Express when I just wanted to keep one of them?
The array looks like this
["options"]=>
array(9) {
[0]=>
array(8) {
["id"]=>
string(2) "79"
["product_id"]=>
string(2) "15"
["sku"]=>
string(9) "CSR-FTC4S"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "35.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "219"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(19) "Five Ten C4 Stealth"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(5) "FTC4S"
["user_value"]=>
string(25) "Five Ten C4 Stealth 5.5mm"
["sequence"]=>
string(1) "0"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "220"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
[2]=>
array(6) {
["id"]=>
string(3) "221"
["product_options_base_id"]=>
string(2) "79"
["option_id"]=>
string(2) "23"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(16) "Rand Toe Patches"
["name"]=>
string(3) "RTP"
["sku"]=>
string(3) "RTP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "10.000"
}
}
}
}
}
[1]=>
array(8) {
["id"]=>
string(2) "80"
["product_id"]=>
string(2) "15"
["sku"]=>
string(10) "CSR-FTONYX"
["status"]=>
string(1) "1"
["is_default"]=>
string(1) "0"
["option_price"]=>
string(6) "37.000"
["sequence"]=>
string(4) "9999"
["available_options"]=>
array(3) {
[0]=>
array(6) {
["id"]=>
string(3) "222"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "16"
["option_data_id"]=>
string(1) "2"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "2"
["admin_name"]=>
string(13) "Five Ten Onyx"
["name"]=>
string(11) "Resole Type"
["sku"]=>
string(6) "FTONYX"
["user_value"]=>
string(19) "Five Ten Onyx 4.5mm"
["sequence"]=>
string(1) "1"
["status"]=>
string(1) "1"
["option_price"]=>
string(5) "0.000"
}
}
}
[1]=>
array(6) {
["id"]=>
string(3) "223"
["product_options_base_id"]=>
string(2) "80"
["option_id"]=>
string(2) "12"
["option_data_id"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["option_data"]=>
array(1) {
[0]=>
array(8) {
["id"]=>
string(1) "1"
["admin_name"]=>
string(7) "Express"
["name"]=>
string(7) "Express"
["sku"]=>
string(3) "EXP"
["user_value"]=>
string(1) "1"
["sequence"]=>
string(4) "9999"
["status"]=>
string(1) "1"
["option_price"]=>
string(6) "25.000"
}
}
}
and my code goes like this
foreach($this->_data as &$data) {
foreach($data['options'] as &$option) {
$option['available_options'] = array_unique($option['available_options']);
}
}
It's working apart from it's stripped out the duplicates rather than showing them once?
array_unique does not work recursively, you need to go inside your array to apply it on option_data directly.
foreach($this->_data as &$data) {
foreach ($data['options'] as &$option) {
foreach ($option['available_options'] as &$available_option) {
foreach ($available_option['option_data'] as &$option_data) {
$option_data = array_unique($option_data);
}
}
}
}
This way, the last option_data looks like
'option_data' => [
[
'id' => '1',
'admin_name' => 'Express',
'sku' => 'EXP',
'sequence' => '9999',
'option_price' => '25.000'
]
]
But as you can see, the value Express only appear once, but user_value and status are removed too, because there value is 1, like id.

Creating multidimensional array PHP

I'm attempting to create a multidimensional array which should have the ID and quantity from the $_POST array. At the moment it seems to put every quantity into each an element with each ID.However I want it to take the first elements from each array and then add them together to a new array and so on.
Whereas it should be
ID 1 - Quantity 100
ID 2 - Quantity 50
etc
But at the moment I get this
array(16) {
[0]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(2) "50"
}
[1]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[2]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[3]=>
array(2) {
["id"]=>
string(1) "1"
["quantity"]=>
string(3) "100"
}
[4]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(2) "50"
}
[5]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[6]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[7]=>
array(2) {
["id"]=>
string(2) "12"
["quantity"]=>
string(3) "100"
}
[8]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(2) "50"
}
[9]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[10]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[11]=>
array(2) {
["id"]=>
string(1) "2"
["quantity"]=>
string(3) "100"
}
[12]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(2) "50"
}
[13]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
[14]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
[15]=>
array(2) {
["id"]=>
string(1) "6"
["quantity"]=>
string(3) "100"
}
}
Here is my PHP code.
foreach($_POST['sweetids'] as $id) {
foreach($_POST['quantites'] as $quantity) {
$stock_array[] = array(
"id"=> $id,
"quantity" => $quantity
);
}
}
I think this is what you're trying to achieve:
foreach($_POST['sweetids'] as $key=>$id) {
$stock_array[] = array(
"id"=> $id,
"quantity" => $_POST['quantities'][$key]
);
}
You're iterating $_POST['quantities'] for every $_POST['sweetids'] which is probably not what you intend. When you iterate both, your result will be every combination of sweetids and quantities, not each pair of them.
I'm guessing you meant something more like:
// Assuming you already verified that $_POST['quantities'] and $_POST['sweetids'] exist
// and that both of them have the same number of elements
for ( $i = 0, $len = count($_POST['sweetids']); $i < $len; $i++ ) {
$stock_array[] = array(
'id' => $_POST['sweetids'][$i],
'quantity' => $_POST['quantities'][$i]
);
}

I need to get NID from the array

array(1) {
[0] => object(stdClass)#53 (14)
{
["cart_item_id"]=> string(2) "64"
["cart_id"]=> string(1) "1"
["nid"]=> string(3) "204"
["qty"]=> string(1) "1"
"changed"]=> string(10) "1340948878"
["data"]=> array(3)
{
["shippable"]=> string(1) "1"
["restrict_qty"]=> string(1) "0"
["module"]=> string(10) "uc_product"
}
["title"]=> string(5) "songs"
["vid"]=> string(3) "204"
["cost"]=> string(9) "123.00000"
["price"]=> string(9) "123.00000"
["weight"]=> string(1) "0"
["weight_units"]=> string(2) "lb"
["module"]=> string(10) "uc_product"
["model"]=> string(1) "1"
}
}
Please help how do get only nid value in new variable.
get via
$output = $array[0]->nid
access object by ->
access array by [index]

I got many duplicates with this function

I know something wrong with this function. It is supposed to return an array with 12 keys and values but it returns 60. The problem should be the foreach loop. Everytime the if loop is false, it passes a value to $result. Can anyone tell me how to make it right?
function getFinaluserElearningLesson($array1, $array2){
$result = array();
for($i = 0; $i<count($array1); $i++){
foreach($array2 as $lesson2){
if($array1[$i]['lesson'] == $lesson2['lesson_id'] && $array1[$i]['user_id'] == $lesson2['user_id']){
$result[] = array(
'first_name' => $array1[$i]['first_name'],
'last_name' => $array1[$i]['last_name'],
'date_of_birth' => $array1[$i]['date_of_birth'],
'user_id' => $array1[$i]['user_id'],
'client_id' => $array1[$i]['client_id'],
'lesson' => $array1[$i]['lesson'],
'lessonName' => $array1[$i]['lessonName'],
'completion' => 'Completed'
);
}else{
$result[] = array(
'first_name' => $array1[$i]['first_name'],
'last_name' => $array1[$i]['last_name'],
'date_of_birth' => $array1[$i]['date_of_birth'],
'user_id' => $array1[$i]['user_id'],
'client_id' => $array1[$i]['client_id'],
'lesson' => $array1[$i]['lesson'],
'lessonName' => $array1[$i]['lessonName'],
'completion' => 'Incomplete'
);
}
}
}
return $result;
}
The output of array1 and array2 is:
array(12) {
[0]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "222"
["lessonName"]=>
string(6) "Asthma"
}
[1]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(2) "69"
["lessonName"]=>
string(23) "Cardiac Catheterization"
}
[2]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "308"
["lessonName"]=>
string(38) "CyberKnife Radiosurgery For Brain AVMs"
}
[3]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(2) "50"
["lessonName"]=>
string(47) "Durable Power of Attorney for Health Care (DPA)"
}
[4]=>
array(7) {
["first_name"]=>
string(3) "Bob"
["last_name"]=>
string(6) "Gorsky"
["date_of_birth"]=>
string(10) "1954-04-27"
["user_id"]=>
string(7) "1701011"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(2) "91"
["lessonName"]=>
string(12) "Heart Attack"
}
[5]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(10) "Wilczewski"
["date_of_birth"]=>
string(10) "1989-05-27"
["user_id"]=>
string(7) "2602204"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "500"
["lessonName"]=>
string(33) "Abdominal Aortic Aneurysm Surgery"
}
[6]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(10) "Wilczewski"
["date_of_birth"]=>
string(10) "1989-05-27"
["user_id"]=>
string(7) "2602204"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(1) "1"
["lessonName"]=>
string(19) "Cholesterol Control"
}
[7]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(10) "Wilczewski"
["date_of_birth"]=>
string(10) "1989-05-27"
["user_id"]=>
string(7) "2602204"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(1) "5"
["lessonName"]=>
string(16) "Types of Doctors"
}
[8]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "500"
["lessonName"]=>
string(33) "Abdominal Aortic Aneurysm Surgery"
}
[9]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "116"
["lessonName"]=>
string(4) "Acne"
}
[10]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "122"
["lessonName"]=>
string(5) "Burns"
}
[11]=>
array(7) {
["first_name"]=>
string(4) "Alex"
["last_name"]=>
string(2) "Hu"
["date_of_birth"]=>
string(10) "1983-09-14"
["user_id"]=>
string(7) "2685058"
["client_id"]=>
string(2) "71"
["lesson"]=>
string(3) "546"
["lessonName"]=>
string(60) "Peripherally Inserted Central Catheter (PICC) for Outpatient"
}
}
array(5) {
[0]=>
array(2) {
["user_id"]=>
string(6) "951301"
["lesson_id"]=>
string(2) "35"
}
[1]=>
array(2) {
["user_id"]=>
string(7) "1700981"
["lesson_id"]=>
string(2) "35"
}
[2]=>
array(2) {
["user_id"]=>
string(7) "2585455"
["lesson_id"]=>
string(2) "25"
}
[3]=>
array(2) {
["user_id"]=>
string(7) "2602204"
["lesson_id"]=>
string(3) "500"
}
[4]=>
array(2) {
["user_id"]=>
string(7) "2642677"
["lesson_id"]=>
string(3) "180"
}
}
You could simplify the inner if() DRASTICALLY by doing this:
if($array1[$i]['lesson'] == $lesson2['lesson_id'] && $array1[$i]['user_id'] == $lesson2['user_id']){
$completion = 'Complete';
} else {
$completion = 'Incomplete';
}
$result[] = array(
'first_name' => $array1[$i]['first_name'],
'last_name' => $array1[$i]['last_name'],
'date_of_birth' => $array1[$i]['date_of_birth'],
'user_id' => $array1[$i]['user_id'],
'client_id' => $array1[$i]['client_id'],
'lesson' => $array1[$i]['lesson'],
'lessonName' => $array1[$i]['lessonName'],
'completion' => $completion // <---- the **ONLY** difference between the two versions
);
As far as your "duplication" problem... I'm not sure what you're trying to accomplish with this. You're running a nested loop, with 5 outer and 12 inner members, which gives your 60 array entries. Since you're assigning something to $result inside the inner loop, no matter WHAT the if() condition returns, that'll produce 60 assignments.
If you don't want the 'false' ones to happen, then don't do anything in the else clause of your code.

Categories