order by max value multidimension array/object - php

I need to order this array/object by max value desc grossConversions
from max to min
max 10 results
I have this output to order
array(19) {
[0] => object(stdClass) #355 (8) { ["campaign_name"]= > string(27)
"Scrub Viso Granuli Vegetali" ["campaign_id"] => int(14)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [1] => object(stdClass) #356 (8) { ["campaign_name"]= > string(33)
"Crema Viso/collo Acido Jaluronico" ["campaign_id"] => int(16)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [2] => object(stdClass) #357 (8) { ["campaign_name"]= > string(13)
"Home Training" ["campaign_id"] => int(5)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [3] => object(stdClass) #358 (8) { ["campaign_name"]= > string(25)
"4x Libri di apprendimento" ["campaign_id"] => int(4)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(1)["pendingConversions"] => int(0)["grossConversions"] => int(2)["payout"] => int(15)["pendingPayout"] => int(0)
} [4] => object(stdClass) #359 (8) { ["campaign_name"]= > string(26)
"Tin-UP - Abbronzante Spray" ["campaign_id"] => int(11)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [5] => object(stdClass) #360 (8) { ["campaign_name"]= > string(23)
"Aspirapolvere Ciclonica" ["campaign_id"] => int(22)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [6] => object(stdClass) #361 (8) { ["campaign_name"]= > string(15)
"Centurion Power" ["campaign_id"] => int(12)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [7] => object(stdClass) #362 (8) { ["campaign_name"]= > string(27)
"Tonico Te' Bianco Te' Rosso" ["campaign_id"] => int(10)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [8] => object(stdClass) #363 (8) { ["campaign_name"]= > string(10)
"Air Freeze" ["campaign_id"] => int(13)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(1)["grossConversions"] => int(3)["payout"] => int(0)["pendingPayout"] => int(16)
} [9] => object(stdClass) #364 (8) { ["campaign_name"]= > string(17)
"Monopattino Urban" ["campaign_id"] => int(25)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
}
}
how Can I order by max value grossConversions DESC?
I have try
function cmp($a, $b) {
if ($a['grossConversions'] == $b['grossConversions']) {
return 0;
}
return ($a['grossConversions'] < $b['grossConversions']) ? 1 : -1;
}
uasort($getReportCampaign->records, 'cmp');
but I get this error
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in

All your lines of your array are standard object. I guess you have done json_decode
So instead of this
$a['grossConversions']
do that
$a->grossConversions
Or if you want to use array, do
json_decode(json_encode($my_array, true), true)
it will "convert" your stdClass as an associative array thanks to the true param of json_decode (cf docs)

Related

Convert CSV to object array in PHP

Hi I am creating a function that pulls my uploaded CSV via ajax then I would like to form an json object that can be posted to the woocommerce API.
The first loop in the while loop creates my labels array and every loop through it is using the labels as a key.
if ($file !== FALSE) {
while (($data = fgetcsv($file, 0, ",")) !== FALSE):
// print_r($data);
if($row == 1) {
$labels = $data;
} else {
$new_data = array_combine($labels, $data);
array_push($csv_data, $new_data);
}
$row++;
endwhile;
foreach($csv_data as $data){
echo "New Customer: ";
print_r($data);
}
fclose($file);
}
When I run this code I get this result:
New Customer: Array
(
[Name] => Test Dude
[Username] => Test
[Email] => test#johndeer.com
[Orders] => 1
[proceed_id] => 765
[AOV] => 765
[Country / Region] => US
[City] => Testerton
[Region] => AR
[Postal Code] => 71601
[Address] => John Drive
)
New Customer: Array
(
[Name] => John Deer
[Username] => johndeer
[Email] => johndeer#jonnydeers.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => CA
[City] => Testerton
[Region] => AB
[Postal Code] => T6M 2V5
[Address] => John Street
)
New Customer: Array
(
[Name] => John Doe
[Username] => john.doe
[Email] => john.doe#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] => John Drive
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe
[Email] => johndoe#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe1
[Email] => johndoe123#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe7
[Email] => johndoe1234#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe5
[Email] => johndoe5#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe6
[Email] => johndoe6#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe8
[Email] => johndoe8#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
0
It displays New Customer: in each loop just to show that is one loop cycle. However when I try to select "Name" or "proceed_id" it returns null. I've tried to use both of these syntaxes $data['Name] and $data->Name. How do I select by key inside this foreach?
foreach($csv_data as $data){
//$data is the individual rows
$proceed_id = $data['proceed_id'];
}
EDIT 1:
Here is the vardump of $csv_data outside loop.
array(9) {
[0]=>
array(11) {
["Name"]=>
string(9) "Test Dude"
["Username"]=>
string(4) "Test"
["Email"]=>
string(17) "test#johndeer.com"
["Orders"]=>
string(1) "1"
["proceed_id"]=>
string(3) "765"
["AOV"]=>
string(3) "765"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(9) "Testerton"
["Region"]=>
string(2) "AR"
["Postal Code"]=>
string(5) "71601"
["Address"]=>
string(10) "John Drive"
}
[1]=>
array(11) {
["Name"]=>
string(9) "John Deer"
["Username"]=>
string(8) "johndeer"
["Email"]=>
string(23) "johndeer#jonnydeers.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "CA"
["City"]=>
string(9) "Testerton"
["Region"]=>
string(2) "AB"
["Postal Code"]=>
string(7) "T6M 2V5"
["Address"]=>
string(11) "John Street"
}
[2]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "john.doe"
["Email"]=>
string(20) "john.doe#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(10) "John Drive"
}
[3]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(7) "johndoe"
["Email"]=>
string(19) "johndoe#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[4]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe1"
["Email"]=>
string(22) "johndoe123#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[5]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe7"
["Email"]=>
string(23) "johndoe1234#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[6]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe5"
["Email"]=>
string(20) "johndoe5#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[7]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe6"
["Email"]=>
string(20) "johndoe6#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[8]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe8"
["Email"]=>
string(20) "johndoe8#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
}
0
Var_export() =
array (
0 =>
array (
'Name' => 'Test Dude',
'Username' => 'Test',
'Email' => 'test#johndeer.com',
'Orders' => '1',
'proceed_id' => '765',
'AOV' => '765',
'Country / Region' => 'US',
'City' => 'Testerton',
'Region' => 'AR',
'Postal Code' => '71601',
'Address' => 'John Drive',
),
1 =>
array (
'Name' => 'John Deer',
'Username' => 'johndeer',
'Email' => 'johndeer#jonnydeers.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'CA',
'City' => 'Testerton',
'Region' => 'AB',
'Postal Code' => 'T6M 2V5',
'Address' => 'John Street',
),
2 =>
array (
'Name' => 'John Doe',
'Username' => 'john.doe',
'Email' => 'john.doe#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => 'John Drive',
),
3 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe',
'Email' => 'johndoe#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
4 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe1',
'Email' => 'johndoe123#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
5 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe7',
'Email' => 'johndoe1234#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
6 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe5',
'Email' => 'johndoe5#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
7 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe6',
'Email' => 'johndoe6#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
8 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe8',
'Email' => 'johndoe8#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
)

Why my array does not sort correctly

I have two arrays $men, $sub , I want want to sort them with this condition: if($men["titre"] == $sub["menuParent"]) I put the content of the second array that contains $sub["menuParent"] in the first one.
Case:
//Array1 ($menu)
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
["menu2"] => array(3) {
["titre"] => string(6) "Title3"
["lien"] => string(10) "google.com"
["order"] => string(1) "3"
}
["menu3"] => array(3) {
["titre"] => string(6) "Title1"
["lien"] => string(0) ""
["order"] => string(1) "1"
}
}
//Array2 ($submenu)
array(3) {
["submenu1"] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
["submenu2"] => array(3) {
["titre"] => string(9) "SubTitle3"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "3"
}
["submenu3"] => array(3) {
["titre"] => string(9) "SubTitle1"
["menuParent"] => string(6) "Title1"
["order"] => string(1) "1"
}
}
Here for exemple in:
array1 ["menu1"]["titre"] => string(6) "Title2"
array2 ["submenu1"]["menuParent"] => string(6) "Title2"
Now while ["menu1"]["titre"] & ["submenu1"]["menuParent"] values are the same "Title2", So I push :
["submenu1"] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
in:
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
as child like this:
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
["child"] => array(3) {
[0] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
}
So I do all this for whole elements`
Here is my try, but It push all the elements in ["menu2"] instead of ["submenu1"], ["submenu2"] IN ["menu1"] and ["submenu3"] IN ["menu3"]
My Try:
/*Injection submenu to Menu*/
foreach($menu as $men) {
foreach($submenu as $sub) {
if($men["titre"] == $sub["menuParent"]){
$key = key($menu);
$menu[$key]['child'][] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
//My array_push doesn't work also
array_push($menu, array($sub["titre"], $sub["menuParent"], $sub["order"]))
}
}
}
Result:
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
["menu2"] => array(4) {
["titre"] => string(6) "Title3"
["lien"] => string(10) "google.com"
["order"] => string(1) "3"
["child"] => array(3) {
[0] => array(3) {
[0] => string(9) "SubTitle2"
[1] => string(6) "Title2"
[2] => string(1) "2"
}
[1] => array(3) {
[0] => string(9) "SubTitle3"
[1] => string(6) "Title2"
[2] => string(1) "3"
}
[2] => array(3) {
[0] => string(9) "SubTitle1"
[1] => string(6) "Title1"
[2] => string(1) "1"
}
}
}
["menu3"] => array(3) {
["titre"] => string(6) "Title1"
["lien"] => string(0) ""
["order"] => string(1) "1"
}
}
Where I'm at fault, If anyone can explain to me why and if I took the good way.
The problem is where your picking the key up, you can use the foreach ( as $key->$value) method to get the key of each item (foreach works on it's own copy of the array and so picking the key from the original array isn't the same as the current one). Also you could use &$men, the & allows you to alter the original array element rather than create a new entry and trying to push the new element into the array...
foreach($menu as $key => $men) {
foreach($submenu as $key1=>$sub) {
if($men["titre"] == $sub["menuParent"]){
$menu[$key]['child'][$key1] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
}
}
}
gives...
Array
(
[menu1] => Array
(
[titre] => Title2
[lien] =>
[order] => 2
[child] => Array
(
[submenu1] => Array
(
[0] => SubTitle2
[1] => Title2
[2] => 2
)
[submenu2] => Array
(
[0] => SubTitle3
[1] => Title2
[2] => 3
)
)
)
[menu2] => Array
(
[titre] => Title3
[lien] => google.com
[order] => 3
)
[menu3] => Array
(
[titre] => Title1
[lien] =>
[order] => 1
[child] => Array
(
[submenu3] => Array
(
[0] => SubTitle1
[1] => Title1
[2] => 1
)
)
)
)
Update:
Code using &$men, note that in setting the data it no longer uses the key, but uses the $men variable.
foreach($menu as &$men) {
foreach($submenu as $key=>$sub) {
if($men["titre"] == $sub["menuParent"]){
$men['child'][$key] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
}
}
}
unset($men); // Good practice to ensure reference is reset

Check if value exists in array with php

I'm working on a project with an API.
I gather data from a form and send this to their website.
The data is gathered through an array and then encoded to json format:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
Then the post request.
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
I can also do a get request, this get request returns a multi dimensional array:
$tet = $SimplicateApi->makeApiCall('GET','/crm/person?q[first_name]=Kevin1');
If i do a var_dump on $tet i get this:
array(3) {
["data"] => array(2) {
[0] => array(11) {
["id"] => string(39)
"person:067af3bd2045824e62ac579e634623b8" ["interests"] => array(1) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}
}["simplicate_url"] => string(51)
"https://emark.simplicate.nl/crm/person/view?id=3552" ["avatar"] => array(2) {
["initials"] => string(2)
"Kt" ["color"] => string(7)
"#03e084"
}["linked_as_contact_to_organization"] => array(1) {
[0] => array(7) {
["id"] => string(46)
"contactperson:0f16f418f1845749c79bebf9e1e753e5" ["organization_id"] => string(45)
"organization:8632b86ba41637262e0871767f96f43e" ["name"] => string(9)
"testing12" ["work_email"] => string(24)
"ma#e-marketingsupport.nl" ["work_phone"] => string(8)
"06269684" ["work_mobile"] => string(8)
"06269684" ["interests"] => array(16) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:456e8b19c0079647" ["name"] => string(11)
"Twinkle 100"
}[1] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:a70e69b83382e85a" ["name"] => string(17)
"Bekend merk in NL"
}[2] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:ce50f1b5593ac180" ["name"] => string(15)
"Cross Border 30"
}[3] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f40eca1b281969d6" ["name"] => string(20)
"Meerdere vestigingen"
}[4] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:7435d7409a07cefb" ["name"] => string(26)
"Meer dan 100k in Duitsland"
}[5] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:cc072cea856ea23a" ["name"] => string(17)
"B2B leadgeneratie"
}[6] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}[7] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:9bbeb23d17283595" ["name"] => string(10)
"Exporteert"
}[8] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:97ed988af66b1abc" ["name"] => string(8)
"Debiteur"
}[9] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:3e31ffca2394bc38e1bb3149bee8b668" ["name"] => string(9)
"Marketing"
}[10] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:706fa5fa92c56081e1bb3149bee8b668" ["name"] => string(6)
"Amazon"
}[11] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:05f1a5da1c4c7df2e1bb3149bee8b668" ["name"] => string(3)
"Jur"
}[12] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:477554ee16a0c738e1bb3149bee8b668" ["name"] => string(11)
"Vertalingen"
}[13] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:f5f1b7512245a3b5e1bb3149bee8b668" ["name"] => string(5)
"Adres"
}[14] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:d1123dfaa0073c82e1bb3149bee8b668" ["name"] => string(4)
"GmbH"
}[15] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:7d3458131ea89afbe1bb3149bee8b668" ["name"] => string(3)
"Web"
}
}
}
}["gender"] => string(7)
"Unknown" ["first_name"] => string(6)
"Kevin1" ["family_name"] => string(7)
"testing" ["full_name"] => string(14)
"Kevin1 testing" ["email"] => string(24)
"ma#e-marketingsupport.nl" ["phone"] => string(8)
"06269684"
}[1] => array(11) {
["id"] => string(39)
"person:067af3bd2045824ea8c16e7ea0baf9d6" ["interests"] => array(1) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}
}["simplicate_url"] => string(51)
"https://emark.simplicate.nl/crm/person/view?id=3553" ["avatar"] => array(2) {
["initials"] => string(2)
"Kt" ["color"] => string(7)
"#dce1f3"
}["linked_as_contact_to_organization"] => array(1) {
[0] => array(7) {
["id"] => string(46)
"contactperson:f48fdcaaff0211e728a2e4ccf197900b" ["organization_id"] => string(45)
"organization:8632b86ba41637262e0871767f96f43e" ["name"] => string(9)
"testing12" ["work_email"] => string(24)
"ma#e-marketingsupport.nl" ["work_phone"] => string(8)
"06269684" ["work_mobile"] => string(8)
"06269684" ["interests"] => array(16) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:456e8b19c0079647" ["name"] => string(11)
"Twinkle 100"
}[1] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:a70e69b83382e85a" ["name"] => string(17)
"Bekend merk in NL"
}[2] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:ce50f1b5593ac180" ["name"] => string(15)
"Cross Border 30"
}[3] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f40eca1b281969d6" ["name"] => string(20)
"Meerdere vestigingen"
}[4] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:7435d7409a07cefb" ["name"] => string(26)
"Meer dan 100k in Duitsland"
}[5] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:cc072cea856ea23a" ["name"] => string(17)
"B2B leadgeneratie"
}[6] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}[7] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:9bbeb23d17283595" ["name"] => string(10)
"Exporteert"
}[8] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:97ed988af66b1abc" ["name"] => string(8)
"Debiteur"
}[9] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:3e31ffca2394bc38e1bb3149bee8b668" ["name"] => string(9)
"Marketing"
}[10] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:706fa5fa92c56081e1bb3149bee8b668" ["name"] => string(6)
"Amazon"
}[11] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:05f1a5da1c4c7df2e1bb3149bee8b668" ["name"] => string(3)
"Jur"
}[12] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:477554ee16a0c738e1bb3149bee8b668" ["name"] => string(11)
"Vertalingen"
}[13] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:f5f1b7512245a3b5e1bb3149bee8b668" ["name"] => string(5)
"Adres"
}[14] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:d1123dfaa0073c82e1bb3149bee8b668" ["name"] => string(4)
"GmbH"
}[15] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:7d3458131ea89afbe1bb3149bee8b668" ["name"] => string(3)
"Web"
}
}
}
}["gender"] => string(7)
"Unknown" ["first_name"] => string(6)
"Kevin1" ["family_name"] => string(7)
"testing" ["full_name"] => string(14)
"Kevin1 testing" ["email"] => string(24)
"ma#e-marketingsupport.nl" ["phone"] => string(8)
"06269684"
}
}["errors"] => NULL["debug"] => NULL
}
I dont want double values. For example if 'first_name' => $_POST['billing_first_name'],(in the $pers_payload variable) exists in $tet as a value. then dont run
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
else run:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
I tried this from another question i asked:
foreach (array_keys($tet['data']) as $key) {
if (array_key_exists('first_name', $tet['data'][$key])
&& (strcasecmp($tet['data'][$key]['first_name'], 'Kevin1') == 0) ) {
echo "found key 'first_name' with value '" . $tet['data'][$key]['first_name'] . "'\n";
} else {
// perform your post request
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
}
This doesn't run the post request at all even if the value of first_name doesnt exist
I found the solution.
I accomplished this with a simple if statement:
if(strtolower($_POST['billing_last_name']) == strtolower($getOrg['data']['0']['linked_persons_contacts']['0']['family_name'])) {
// do nothing
} else {
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}

remove duplicates from the two array in php

I have stuck to the following problem of removing the duplcation of array
$result=array(2) { [0]=> object(stdClass)#489 (5) { ["id"]=> string(2) "64" ["block_from_id"]=> string(3) "117" ["block_to_id"]=> string(3) "329" ["block_status"]=> string(1) "0" ["username"]=> string(5) "pppoo" } [1]=> object(stdClass)#490 (5) { ["id"]=> string(2) "65" ["block_from_id"]=> string(3) "117" ["block_to_id"]=> string(3) "590" ["block_status"]=> string(1) "0" ["username"]=> string(3) "Pet" } }
$customerlist= array(7) { [0]=> object(stdClass)#491 (5) { ["customer_name"]=> string(4) "User" ["profile_image"]=> string(47) "http://pet.huarisnaque.com/pet/upload/90113.png" ["jid"]=> string(18) "user128#canopus-pc" ["customer_id"]=> string(3) "128" ["phone"]=> string(10) "4784784784" } [1]=> object(stdClass)#494 (5) { ["customer_name"]=> string(6) "Khatru" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/61694.png" ["jid"]=> string(20) "khatru321#canopus-pc" ["customer_id"]=> string(3) "321" ["phone"]=> string(10) "9686838386" } [2]=> object(stdClass)#495 (5) { ["customer_name"]=> string(5) "pppoo" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/35210.png" ["jid"]=> string(17) "yyy329#canopus-pc" ["customer_id"]=> string(3) "329" ["phone"]=> string(10) "9525538835" } [3]=> object(stdClass)#496 (5) { ["customer_name"]=> string(7) "Xitxitx" ["profile_image"]=> NULL ["jid"]=> string(21) "xitxitx330#canopus-pc" ["customer_id"]=> string(3) "330" ["phone"]=> string(10) "6535383535" } [4]=> object(stdClass)#497 (5) { ["customer_name"]=> string(25) "The Following Document yf" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/13712.png" ["jid"]=> string(39) "the following document yf589#canopus-pc" ["customer_id"]=> string(3) "589" ["phone"]=> string(10) "9535383535" } [5]=> object(stdClass)#498 (5) { ["customer_name"]=> string(3) "Pet" ["profile_image"]=> NULL ["jid"]=> string(17) "pet590#canopus-pc" ["customer_id"]=> string(3) "590" ["phone"]=> string(10) "6560530537" } [6]=> object(stdClass)#499 (5) { ["customer_name"]=> string(10) "Sanjay Pra" ["profile_image"]=> NULL ["jid"]=> string(24) "sanjay pra599#canopus-pc" ["customer_id"]=> string(3) "599" ["phone"]=> string(10) "2828282822" } }
there are two arrays,we need to remove duplicate records from the array containing two elements from result having elements from the customerlist.
here is my approach
for($i=0;$i<count($customerslist);$i++)
{
for($j=0;$j<count($result);$i++)
{
// if($result[$j]->block_to_id==$customerslist[$i]->customer_id)
{
unset($customerslist[$i]);
}
echo $result[$j]->block_to_id."<br/>";
}
}
You can unset the values after you find the indexes that has the duplicate. Because if you unset it inside the for loop of checking your duplicates it will produce this error because the size/count of the array changes and the index does not match anymore:
Notice: Undefined offset: 2
So my solution is you can put the matching indexes on another array then unset them on another for loop shown bellow:
$result=array(
array('id' => 64,
"block_from_id" => 117,
"block_to_id" => 329,
"block_status" => 0,
"username" => "pppoo"),
array("id"=> 65,
"block_from_id"=> 117,
"block_to_id"=> 590,
"block_status"=> 0,
"username"=> "Pet"
)
);
$customerlist= array(
array("customer_name" => "User" ,"profile_image" => "http://pet.huarisnaque.com/pet/upload/90113.png" ,"jid" => "user128#canopus-pc" ,"customer_id" => "128" ,"phone" => "4784784784"),
array("customer_name" => "Khatru" ,"profile_image" => "http://smartpetmanagement.com/upload/61694.png" ,"jid" => "khatru321#canopus-pc" ,"customer_id" => "321" ,"phone" => "9686838386"),
array("customer_name" => "pppoo" ,"profile_image" => "http://smartpetmanagement.com/upload/35210.png" ,"jid" => "yyy329#canopus-pc" ,"customer_id" => "329" ,"phone" => "9525538835"),
array("customer_name" => "Xitxitx" ,"profile_image " => NULL ,"jid" => "xitxitx330#canopus-pc" ,"customer_id" => "330" ,"phone" => "6535383535"),
array("customer_name" => "The Following Document yf" ,"profile_image" => "http://smartpetmanagement.com/upload/13712.png" ,"jid" => "the following document yf589#canopus-pc" ,"customer_id" => "589" ,"phone" => "9535383535"),
array("customer_name" => "Pet" ,"profile_image " => NULL ,"jid" => "pet590#canopus-pc" ,"customer_id" => "590" ,"phone" => "6560530537"),
array("customer_name" => "Sanjay Pra" ,"profile_image " => NULL ,"jid" => "sanjay pra599#canopus-pc" ,"customer_id" => "599" ,"phone" => "2828282822")
);
echo "Before:". sizeof($customerlist) ."<br>";
print_r($customerlist);
echo "<br>";
echo "<br>";
$match = array();
for ($i=0; $i < sizeof($result) ; $i++) {
for ($ii=0; $ii < sizeof($customerlist) ; $ii++) {
if ($result[$i]['block_to_id'] == $customerlist[$ii]['customer_id']) {
$match[] = $ii;
echo " Match INDEX on result $i == customerlist $ii<br>";
}
}
}
for ($i=0; $i < sizeof($match) ; $i++) {
$ii = $match[$i];
unset($customerlist[$ii]);
}
echo "<br>";
echo "After: ". sizeof($customerlist) ."<br>";
print_r($customerlist);
OUTPUT:
Before:7
Array ( [0] => Array ( [customer_name] => User [profile_image] => http://pet.huarisnaque.com/pet/upload/90113.png [jid] => user128#canopus-pc [customer_id] => 128 [phone] => 4784784784 ) [1] => Array ( [customer_name] => Khatru [profile_image] => http://smartpetmanagement.com/upload/61694.png [jid] => khatru321#canopus-pc [customer_id] => 321 [phone] => 9686838386 ) [2] => Array ( [customer_name] => pppoo [profile_image] => http://smartpetmanagement.com/upload/35210.png [jid] => yyy329#canopus-pc [customer_id] => 329 [phone] => 9525538835 ) [3] => Array ( [customer_name] => Xitxitx [profile_image ] => [jid] => xitxitx330#canopus-pc [customer_id] => 330 [phone] => 6535383535 ) [4] => Array ( [customer_name] => The Following Document yf [profile_image] => http://smartpetmanagement.com/upload/13712.png [jid] => the following document yf589#canopus-pc [customer_id] => 589 [phone] => 9535383535 ) [5] => Array ( [customer_name] => Pet [profile_image ] => [jid] => pet590#canopus-pc [customer_id] => 590 [phone] => 6560530537 ) [6] => Array ( [customer_name] => Sanjay Pra [profile_image ] => [jid] => sanjay pra599#canopus-pc [customer_id] => 599 [phone] => 2828282822 ) )
Match INDEX on result 0 == customerlist 2
Match INDEX on result 1 == customerlist 5
After: 5
Array ( [0] => Array ( [customer_name] => User [profile_image] => http://pet.huarisnaque.com/pet/upload/90113.png [jid] => user128#canopus-pc [customer_id] => 128 [phone] => 4784784784 ) [1] => Array ( [customer_name] => Khatru [profile_image] => http://smartpetmanagement.com/upload/61694.png [jid] => khatru321#canopus-pc [customer_id] => 321 [phone] => 9686838386 ) [3] => Array ( [customer_name] => Xitxitx [profile_image ] => [jid] => xitxitx330#canopus-pc [customer_id] => 330 [phone] => 6535383535 ) [4] => Array ( [customer_name] => The Following Document yf [profile_image] => http://smartpetmanagement.com/upload/13712.png [jid] => the following document yf589#canopus-pc [customer_id] => 589 [phone] => 9535383535 ) [6] => Array ( [customer_name] => Sanjay Pra [profile_image ] => [jid] => sanjay pra599#canopus-pc [customer_id] => 599 [phone] => 2828282822 ) )

array key value pair at a specified point in array

I have an associative array , i would like to add some more key and values
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/31/2011
)
[1] => Array
(
[NUMBER] => 87
[TYPE] => something
[DATE] => 3/28/2011
)
[2] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/2/2011
)
)
In Above array i want to add another key named STATUS and value before DATE
so that finally iget
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[STATUS] => waiting
[DATE] => 3/31/2011
)
}
canPlease give me proper direction
$arr = Array(
0 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/32/2011'),
1 => Array('NUMBER' => 87, 'TYPE' => something, 'DATE' => '3/28/2011'),
2 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/2/2011')
);
foreach($arr as $key => $value) {
$arr[$key] = array_slice($value, 0, 2) +
array('Status' => 'waiting') +
array_slice($value, -1);
}
var_dump($arr);
gives the following array:
array(3) {
[0]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/32/2011"
}
[1]=>
array(4) {
["NUMBER"]=>
int(87)
["TYPE"]=>
string(9) "something"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/28/2011"
}
[2]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(8) "3/2/2011"
}
}

Categories