Reputation: 48
So, I am returning 2 different multi-dimensional arrays from 2 separate db calls.(Can't use a join because of our specific API for db calls) I need to search through 2nd array and if a value matches that of a value in the first array, I need to append the related data to that row in first array for display in datatable. I have tried numerous things to just get the values to show. Here is what my arrays look like and where I am at with it.
Array 1
{"status":"SUCCESS","message":"Successfully read","data":
[{"ordnbr":"12345",
"custid":"CUSTID #",
"custname":"My Customers Name",
"custordnbr":"54321",
"slsperid":"ID",
"orddate":"2015-05-14",
"shipdate":"2015-06-03",
"dtrdy":"2015-05-29",
"carrier":"Some Carrier",
"totord":"21703",
"shipped":"4341",
"notes":"These are notes",
"siteid":"Some Site",
"orderstatus":"RDY"},
{"ordnbr":"23456",
"custid":"Anoter Customer",
"custname":"Another Customers Name",
"custordnbr":"65432",
"slsperid":"ID2",
"orddate":"2015-05-19",
"shipdate":"2015-06-05",
"dtrdy":"2015-06-05",
"carrier":"Another Carrier",
"totord":"98875",
"shipped":"0",
"notes":" ",
"siteid":"Another Site",
"orderstatus":"GR"},
etc... The list goes on as there are numerous records.
Second array looks kind of like this(var_dump) I realize this is an array of objects but am casting each line to array.
array(41) {
[0]=>
object(stdClass)#2923 (15) {
["trknbr"]=>
string(12) "7890"
["siteid"]=>
string(3) "This Site"
["flatrate"]=>
string(1) "0"
["ratemile"]=>
string(1) "0"
["miles"]=>
string(3) "822"
["covered"]=>
string(1) "N"
["dateship"]=>
string(10) "0000-00-00"
["trkco"]=>
string(0) ""
["totalchrg"]=>
string(1) "0"
["fuelchrg"]=>
string(1) "0"
["tarp"]=>
string(1) "N"
["shipdirection"]=>
string(3) "out"
["comments"]=>
NULL
["custname"]=>
string(30) "CustName"
["detail"]=>
array(1) {
[0]=>
object(stdClass)#2922 (7) {
["trknbr"]=>
string(12) "BR549"
["ordnbr"]=>
string(6) "12345"
["dtrelease"]=>
string(10) "2015-06-04"
["custid"]=>
string(6) "Custid"
["shipto"]=>
string(13) "Some City"
["slsperid"]=>
string(2) "WE"
["comments"]=>
string(21) "Truck # 2 of 5
$2360"
}
}
}
[1]=>
object(stdClass)#2916 (15) {
["trknbr"]=>
string(12) "34563"
["siteid"]=>
string(3) "My Other Site"
["flatrate"]=>
string(1) "0"
["ratemile"]=>
string(1) "0"
["miles"]=>
string(3) "244"
["covered"]=>
string(1) "N"
["dateship"]=>
string(10) "0000-00-00"
["trkco"]=>
string(0) ""
["totalchrg"]=>
string(1) "0"
["fuelchrg"]=>
string(1) "0"
["tarp"]=>
string(1) "N"
["shipdirection"]=>
string(3) "out"
["comments"]=>
NULL
["custname"]=>
string(25) "Another Customer"
["detail"]=>
array(1) {
[0]=>
object(stdClass)#2830 (7) {
["trknbr"]=>
string(12) "34563"
["ordnbr"]=>
string(6) "34578"
["dtrelease"]=>
string(10) "2015-06-08"
["custid"]=>
string(6) "wwweee"
["shipto"]=>
string(12) "Another City"
["slsperid"]=>
string(3) "RRR"
["comments"]=>
string(31) "DO NOT SHIP BEFORE 6/8
$ 769P"
}
}
}
So what I need to do is, for example, if the ordnbr field is the same for any 2 arrays, I need to append a couple fields from the 2nd array onto the first array. Right now, I am just trying to get the data to return so I can make the comparison and it doesn't seem to be comparing. I have tried using a foreach loop and if array1->ordnbr == array2->ordnbr, then do something... That doesn't work. Tried using in_array and searching for array1->ordnbr in array2. Nothing I have done works. Can someone please tell me in what direction I should proceed to make these comparisons? Thanks in advance.
I may not have been real clear on what I need to do... Ok... With Array 1(which did start out as a JSON object but which I converted to multidimensional array using json_decode), I need all rows regardless of whether they match Array 2 or not. However, if ordnbr matches an ordnbr on a row in Array2, then I need to include some data from that row in Array2 into the corresponding row of Array1.
Upvotes: 0
Views: 61
Reputation: 1995
What you would do is to create 2 associative arrays, one for each array with key is ordnbr
and value is the whole object contain ordnbr
. For example:
$arr1 = array()
foreach ($input1["data"] as $row){
$arr1[$row["ordnbr"]] = $row;
}
The do the same for your second array $arr2
. After that, a simple foreach would do the job
foreach ($arr1 as $order_number => $row){
if in_array($order_number, $arr2){
// $row2 will have the same $order_number as $row1
$row2 = $arr2[$order_number];
// then do whatever you need here
// ...
}
}
Upvotes: 1