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)
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' => '',
),
)
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
I have an
array() {
[0] =>
string(1) "id1"
[1] =>
string(14) "Gucci"
[2] =>
string(1) "id1"
[3] =>
string(24) "bag1"
[4] =>
string(4) "id2"
[5] =>
string(14) "Gucci"
[6] =>
string(4) "id2"
[7] =>
string(25) "Gucci bag2"
[8] =>
string(4) "id3"
[9] =>
string(14) "Gucci"
[10] =>
string(4) "id3"
[11] =>
string(27) "bag3"
..more
the key 0, 2, 4, ...are the id
the key 1, 5 .. are the brand
the key 3, 6 are the name
i want to loop trough the array to check if $array[1] is in $array[3], and then $array[5] is in $array[7]
how can i have the iteration of
if (preg_match("/$array[$i+1]/i", "$array[$i+3]")) {
echo "$array[$i]";
} else {
echo $array[$i+1] . ' ' . $array[$i+3];
}
Thank you
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"
}
}