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' => '',
),
)
Related
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)
This question already has answers here:
How to push both value and key into PHP array
(21 answers)
Closed 10 months ago.
Please note that this is not a duplicate - but an extension of these questions below)
PHP array_push one array into another
array_push into a multi-dimensional array
I am trying to array_push into a multidimensional array but want to keep the array key of the second array.
Example:
<?php
$samplearray = array(
array('name' => "Joe Bloggs", 'age' => "30", 'sex' => "Male", 'title' => "Mr" ),
array('name' => "Jane Bloggs", 'age' => "30", 'sex' => "Female", 'title' => "Mrs" ),
array('name' => "Little Bloggs", 'age' => "10", 'sex' => "Male", 'title' => "Master" ),
);
array_push ($samplearray[0],"Inserted Value");
print_r($samplearray);
?>
The output for this is:
array(3) {
[0]=>
array(5) {
["name"]=>
string(10) "Joe Bloggs"
["age"]=>
string(2) "30"
["sex"]=>
string(4) "Male"
["title"]=>
string(2) "Mr"
[0]=>
string(14) "Inserted Value" <-- INSERTED VALUE
}
[1]=>
array(4) {
["name"]=>
string(11) "Jane Bloggs"
["age"]=>
string(2) "30"
["sex"]=>
string(6) "Female"
["title"]=>
string(3) "Mrs"
}
[2]=>
array(4) {
["name"]=>
string(13) "Little Bloggs"
["age"]=>
string(2) "10"
["sex"]=>
string(4) "Male"
["title"]=>
string(6) "Master"
}
}
I want to insert a key along with the value but when I try that - it fails. Can you please advice
array_push ($samplearray[0]['insertedvalue'],"Inserted Value");
returns a NULL value for the inserted key on local server but fails on PHPfiddle.
is this what you are looking for?
$samplearray = array(
array('name' => "Joe Bloggs", 'age' => "30", 'sex' => "Male", 'title' => "Mr" ),
array('name' => "Jane Bloggs", 'age' => "30", 'sex' => "Female", 'title' => "Mrs" ),
array('name' => "Little Bloggs", 'age' => "10", 'sex' => "Male", 'title' => "Master" ),
);
$samplearray[0]['othername'] = 'lalala';
echo '<pre>';
print_r($samplearray);
and this should print:
Array
(
[0] => Array
(
[name] => Joe Bloggs
[age] => 30
[sex] => Male
[title] => Mr
[othername] => lalala
)
[1] => Array
(
[name] => Jane Bloggs
[age] => 30
[sex] => Female
[title] => Mrs
)
[2] => Array
(
[name] => Little Bloggs
[age] => 10
[sex] => Male
[title] => Master
)
)
This must be what you need.
$samplearray[0]['insertedvalue'] = "Inserted Value";
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 ) )
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"
}
}
I want to sort all php arrays based on first main array.
This is the main array which I want to use to sort all other arrays:
Array (
[0] => 10
[1] => 14
[2] => 15
[3] => 20
[4] => 21
[5] => 24
[6] => 25
[7] => 28
[8] => 30
[9] => 45
[10] => 60
[11] => 90
[12] => 120
[13] => 150
[14] => 180
[15] => 210
[16] => 240
[17] => 270
[18] => 365
)
This are arrays which need to be sorted :
Array (
[0] => Array
(
[14] => 49.21
[20] => 71.04
[25] => 89.58
[30] => 100.00
)
[1] => Array
(
[180] => 412.00
[150] => 347.00
[120] => 285.00
[90] => 224.00
[60] => 165.00
[30] => 100.00
[14] => 47.00
)
)
I need that final result look like this:
Array (
[0] => Array
(
[10] => n/a
[14] => 49.21
[15] => n/a
[20] => 71.04
[21] => n/a
[24] => n/a
[25] => 89.58
[28] => n/a
[30] => 100.00
[45] => n/a
[60] => n/a
[90] => n/a
[120] => n/a
[150] => n/a
[180] => n/a
[210] => n/a
[240] => n/a
[270] => n/a
[365] => n/a
)
[1] => Array
(
[10] => n/a
[14] => 71.04
[15] => n/a
[20] => n/a
[21] => n/a
[24] => n/a
[25] => n/a
[28] => n/a
[30] => 100.00
[45] => n/a
[60] => 165.00
[90] => 224.00
[120] => 285.00
[150] => 347.00
[180] => 412.00
[210] => n/a
[240] => n/a
[270] => n/a
[365] => n/a
)
)
Thanks.
Assuming your initial array is $source, and $todo is your second array with the two subsets, then:
$keys = array_flip($todo);
$keys = array_map(function() { return 'n/a'; }, $keys); // set all values to be "n/a";
foreach($todo as $idx => $do) {
$todo[$idx] = $do + $keys; // merge without renumbering.
}
If I understand it right from your question, you want to make both arrays of the same length and set keys not in them to "n/a".
Variant with array_merge (Demo):
$shorten = array(
0 => 10,
1 => 14,
2 => 15,
3 => 20,
4 => 21,
5 => 24,
6 => 25,
7 => 28,
8 => 30,
9 => 45,
10 => 60,
11 => 90,
12 => 120,
13 => 150,
14 => 180,
15 => 210,
16 => 240,
17 => 270,
18 => 365,
);
$data = array(
0 => array(
14 => '49.21',
20 => '71.04',
25 => '89.58',
30 => '100.00',
),
1 => array(
180 => '412.00',
150 => '347.00',
120 => '285.00',
90 => '224.00',
60 => '165.00',
30 => '100.00',
14 => '47.00',
),
);
// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
foreach($data as &$array) {
// merge to get set members
$array = array_merge($shorten, $array);
}
unset($array);
var_dump($data);
Result:
array(2) {
[0]=>
array(23) {
[0]=>
string(3) "n/a"
[1]=>
string(3) "n/a"
[2]=>
string(3) "n/a"
[3]=>
string(3) "n/a"
[4]=>
string(3) "n/a"
[5]=>
string(3) "n/a"
[6]=>
string(3) "n/a"
[7]=>
string(3) "n/a"
[8]=>
string(3) "n/a"
[9]=>
string(3) "n/a"
[10]=>
string(3) "n/a"
[11]=>
string(3) "n/a"
[12]=>
string(3) "n/a"
[13]=>
string(3) "n/a"
[14]=>
string(3) "n/a"
[15]=>
string(3) "n/a"
[16]=>
string(3) "n/a"
[17]=>
string(3) "n/a"
[18]=>
string(3) "n/a"
[19]=>
string(5) "49.21"
[20]=>
string(5) "71.04"
[21]=>
string(5) "89.58"
[22]=>
string(6) "100.00"
}
[1]=>
array(26) {
[0]=>
string(3) "n/a"
[1]=>
string(3) "n/a"
[2]=>
string(3) "n/a"
[3]=>
string(3) "n/a"
[4]=>
string(3) "n/a"
[5]=>
string(3) "n/a"
[6]=>
string(3) "n/a"
[7]=>
string(3) "n/a"
[8]=>
string(3) "n/a"
[9]=>
string(3) "n/a"
[10]=>
string(3) "n/a"
[11]=>
string(3) "n/a"
[12]=>
string(3) "n/a"
[13]=>
string(3) "n/a"
[14]=>
string(3) "n/a"
[15]=>
string(3) "n/a"
[16]=>
string(3) "n/a"
[17]=>
string(3) "n/a"
[18]=>
string(3) "n/a"
[19]=>
string(6) "412.00"
[20]=>
string(6) "347.00"
[21]=>
string(6) "285.00"
[22]=>
string(6) "224.00"
[23]=>
string(6) "165.00"
[24]=>
string(6) "100.00"
[25]=>
string(5) "47.00"
}
}
Variant with mapping function (Demo):
$shorten = array(
0 => 10,
1 => 14,
2 => 15,
3 => 20,
4 => 21,
5 => 24,
6 => 25,
7 => 28,
8 => 30,
9 => 45,
10 => 60,
11 => 90,
12 => 120,
13 => 150,
14 => 180,
15 => 210,
16 => 240,
17 => 270,
18 => 365,
);
// overload $shorten array with a mapping function
$shorten = function(array $a) use ($shorten)
{
$r = array();
foreach($shorten as $i => $k)
{
$r[$k] = isset($a[$k]) ? $a[$k] : 'n/a';
}
return $r;
};
$data = array(
0 => array(
14 => '49.21',
20 => '71.04',
25 => '89.58',
30 => '100.00',
),
1 => array(
180 => '412.00',
150 => '347.00',
120 => '285.00',
90 => '224.00',
60 => '165.00',
30 => '100.00',
14 => '47.00',
),
);
// apply mapping function to $data
$data = array_map($shorten, $data);
var_dump($data); # result
Result:
array(2) {
[0]=>
array(19) {
[10]=>
string(3) "n/a"
[14]=>
string(5) "49.21"
[15]=>
string(3) "n/a"
[20]=>
string(5) "71.04"
[21]=>
string(3) "n/a"
[24]=>
string(3) "n/a"
[25]=>
string(5) "89.58"
[28]=>
string(3) "n/a"
[30]=>
string(6) "100.00"
[45]=>
string(3) "n/a"
[60]=>
string(3) "n/a"
[90]=>
string(3) "n/a"
[120]=>
string(3) "n/a"
[150]=>
string(3) "n/a"
[180]=>
string(3) "n/a"
[210]=>
string(3) "n/a"
[240]=>
string(3) "n/a"
[270]=>
string(3) "n/a"
[365]=>
string(3) "n/a"
}
[1]=>
array(19) {
[10]=>
string(3) "n/a"
[14]=>
string(5) "47.00"
[15]=>
string(3) "n/a"
[20]=>
string(3) "n/a"
[21]=>
string(3) "n/a"
[24]=>
string(3) "n/a"
[25]=>
string(3) "n/a"
[28]=>
string(3) "n/a"
[30]=>
string(6) "100.00"
[45]=>
string(3) "n/a"
[60]=>
string(6) "165.00"
[90]=>
string(6) "224.00"
[120]=>
string(6) "285.00"
[150]=>
string(6) "347.00"
[180]=>
string(6) "412.00"
[210]=>
string(3) "n/a"
[240]=>
string(3) "n/a"
[270]=>
string(3) "n/a"
[365]=>
string(3) "n/a"
}
}