I have a array called $data['job_list'] which is as follows
Array
(
[0] => Array
(
[job_id] => 2
[job_title] => JQuery developer
[job_desc] => Developer
[job_slug] => 2-JQuery-developer
[job_type] => 191
[job_skill] => 2
[job_salary] => 2
[job_experience] => 1
[company_name] => IGUTS
[company_desc] => IGUTS is a fresh company
[company_industry] => 24
[company_address] => 35 Lawrence Street
[company_state] => 35
[company_city] => 650
[user_id] => 1
[concerned_fname] => Saswat
[concerned_lname] => Routroy
[contact] => 8961287928
[date_of_post] => 26-04-2014
[job_timestamp] => 1398517810
[industry_id] => 191
[industry_title] => Web Designer/Developer
[p_cid] => 24
[industry_slug] => 191-Web-Designer-Developer
[industry_desc] =>
[industry_image] =>
[industry_priority] => 0
[industry_timestamp] => 1396535046
)
[1] => Array
(
[job_id] => 1
[job_title] => PHP developer
[job_desc] => Developer
[job_slug] => 1-PHP-developer
[job_type] => 191
[job_skill] => 1,2
[job_salary] => 1
[job_experience] => 1
[company_name] => IGUTS
[company_desc] => IGUTS Company
[company_industry] => 24
[company_address] => 35 Lawrence Street
[company_state] => 35
[company_city] => 650
[user_id] => 1
[concerned_fname] => Saswat
[concerned_lname] => Routroy
[contact] => 8961287928
[date_of_post] => 18-04-2014
[job_timestamp] => 1397842605
[skill_id] => 2
[skill_title] => JQuery
[skill_slug] => 2-JQuery
[industry] => 24
[skill_timestamp] => 1397395987
)
[2] => Array
(
[job_id] => 2
[job_title] => JQuery developer
[job_desc] => Developer
[job_slug] => 2-JQuery-developer
[job_type] => 191
[job_skill] => 2
[job_salary] => 2
[job_experience] => 1
[company_name] => IGUTS
[company_desc] => IGUTS is a fresh company
[company_industry] => 24
[company_address] => 35 Lawrence Street
[company_state] => 35
[company_city] => 650
[user_id] => 1
[concerned_fname] => Saswat
[concerned_lname] => Routroy
[contact] => 8961287928
[date_of_post] => 26-04-2014
[job_timestamp] => 1398517810
[skill_id] => 2
[skill_title] => JQuery
[skill_slug] => 2-JQuery
[industry] => 24
[skill_timestamp] => 1397395987
)
)
[job_id] => 2 is present twice
What I want is that, the row with duplicate job_id => 2 should be removed
How can I achieve that??
$jobIds = array();
foreach ($jobs as $key => $job) {
if (in_array($jobIds, $job['job_id'])) {
unset($jobs[$key]);
continue;
}
$jobIds[] = $job['job_id'];
}
print_r($jobs);
I would cycle though each "job" and create an array of Job IDs. If I see the same ID twice then I would remove the duplicate and store it somewhere else if needed.
As Matei Mihai stated, i made some minor changes in his script and bingo it solved..
Matei Mihai made a mistake regarding the data-type of arguments of the in_array() function
I fixed that.
here's my code
$jobIds = array();
for($i = 0;$i < count($data['job_list']); $i++ )
{
if (in_array($data['job_list'][$i]['job_id'], $jobIds))
{
unset($data['job_list'][$i]);
continue;
}
$jobIds[] = $data['job_list'][$i]['job_id'];
}
Related
I have an array below. What i need to do is unset one array inside "Urunler" than updating a new array. Very crucial one is how can push new array by keeping same key value which is same deleted one.
stdClass Object
(
[Urun] => Array
(
[0] => stdClass Object
(
[Id] => 1384
[Urun_kodu] => UKy74GhoD1LZ
[Urun_adi] => Bizim Ayçiçek Yağı Teneke 5 Lt
[Urun_resim] => uploads/urunler_v/UKy74GhoD1LZ/637b4-bizim-aycicek-yagi-tenekelxwbmlgpdh.jpg
[Esas_tutar] => 27.00
[Esas_tutar_kdvsiz] => 25.00
[Kdv_tutar] => 2.00
[Kdv_tutar_adetli] => 6.00
[Adetli_tutar] => 81.00
[Adetli_tutar_kdvsiz] => 75.00
[Urun_adeti] => 3
[Urun_kuru] => TRY
[Urun_ds] => 100
)
[1] => stdClass Object
(
[Id] => 4518
[Urun_kodu] => UK6FfrWzDPLE
[Urun_adi] => Besler Ton Balığı 2x160 Gr
[Urun_resim] => uploads/urunler_v/UK6FfrWzDPLE/bfeec-besler-ton-baligi-2x160-grqrgy6fl2hv.jpg
[Esas_tutar] => 10.80
[Esas_tutar_kdvsiz] => 10.00
[Kdv_tutar] => 0.80
[Kdv_tutar_adetli] => 2.40
[Adetli_tutar] => 32.40
[Adetli_tutar_kdvsiz] => 30.00
[Urun_adeti] => 3
[Urun_kuru] => TRY
[Urun_ds] => 32
)
[2] => stdClass Object
(
[Id] => 4518
[Urun_kodu] => UK6FfrWzDPLE
[Urun_adi] => Besler Ton Balığı 2x160 Gr
[Urun_resim] => uploads/urunler_v/UK6FfrWzDPLE/bfeec-besler-ton-baligi-2x160-grqrgy6fl2hv.jpg
[Esas_tutar] => 10.80
[Esas_tutar_kdvsiz] => 10.00
[Kdv_tutar] => 0.80
[Kdv_tutar_adetli] => 2.40
[Adetli_tutar] => 32.40
[Adetli_tutar_kdvsiz] => 30.00
[Urun_adeti] => 3
[Urun_kuru] => TRY
[Urun_ds] => 32
)
)
)
Below, you can see my steps when pushing new array
$Urun = $SepetBilgisi->Urun;
$MevcutUrunSayisi=count($Urun);
$YeniUrun=[];
foreach($Urun as $Key => $Value)
{
if($Value->Id== $UrunId){ // My target is $UrunId=4518 that means $Urun[Urun][1] one
unset($Urun[$Key]);
if(count($Urun)< $MevcutUrunSayisi){
//Şimdi Yeni Array Buraya Ekleyelim
$YeniUrun[$Key]=[
"Id"=> $Value->Id,
"Urun_kodu" => $Value->Urun_kodu,
"Urun_resim" => $Value->Urun_resim,
"Esas_tutar" => $Value->Esas_tutar,
"Esas_tutar_kdvsiz" => $Value->Esas_tutar_kdvsiz,
"Kdv_tutar" => $Value->Kdv_tutar,
"Kdv_tutar_adetli" => $Value->Kdv_tutar_adetli,
"Adetli_tutar" => $Value->Adetli_tutar,
"Adetli_tutar_kdvsiz" => $Value->Adetli_tutar_kdvsiz,
"Urun_adeti" => $YeniAdet,
"Urun_kuru" => $Value->Urun_kuru,
"Urun_ds" => $Value->Urun_ds
];
array_push($Urun , $YeniUrun);
}else{
return false;
}
}
}
Using 'array_push' only put new array end of main array as you know.
You want to replace the key, not push to the end of the array:
$Urun = $SepetBilgisi->Urun;
$MevcutUrunSayisi=count($Urun);
$YeniUrun=[];
foreach($Urun as $Key => $Value)
{
if($Value->Id== $UrunId){ // My target is $UrunId=4518 that means $Urun[Urun][1] one
if(count($Urun)< $MevcutUrunSayisi){
//Şimdi Yeni Array Buraya Ekleyelim
$replace=[
"Id"=> $Value->Id,
"Urun_kodu" => $Value->Urun_kodu,
"Urun_resim" => $Value->Urun_resim,
"Esas_tutar" => $Value->Esas_tutar,
"Esas_tutar_kdvsiz" => $Value->Esas_tutar_kdvsiz,
"Kdv_tutar" => $Value->Kdv_tutar,
"Kdv_tutar_adetli" => $Value->Kdv_tutar_adetli,
"Adetli_tutar" => $Value->Adetli_tutar,
"Adetli_tutar_kdvsiz" => $Value->Adetli_tutar_kdvsiz,
"Urun_adeti" => $YeniAdet,
"Urun_kuru" => $Value->Urun_kuru,
"Urun_ds" => $Value->Urun_ds
];
$Urun[$Key] = $replace;
}else{
return false;
}
}
}
i want array_diff using this code i am also getting output but same time error occur
"Array to string conversion"
I am using codeigniter, i am getting post value in second array(listingdata) with method $this->input->post(); , but my first array getting some dynamic key and value then this two array diff give me output but some time getting error.
Please Help me
Any solution always welcome.
if(isset($_POST)){
foreach($_POST as $key => $value) {
$arr[$key] = $value; // making array
}
}
$result = array_diff($arr,$listingdata);
print_r($result);
print_r($arr);
print_r($listingdata);
My $arr array
Array
(
[itemTitle] => fdafdas
[subtitle] => fadsfdas
[quantity] => 12
[itemDescription] => fdas
[hide_ebay_id] => 89
[ebay_user] => Array
(
[0] =>
)
[ebay_category] => 2984
[e_sub_cat] => 20433
[e_second_child_sub_cat] => 117027
[e_third_child_sub_cat] =>
[ebay_upc] => 31231241341
[Brand] => Unbranded
[MPN] => Does_Not_Apply
[Model] => fsdf
[Country/Region_of_Manufacture] => Unknown
[listingType] => FixedPriceItem
[listingduration] => Days_5
[buy_it_now_price] => 20.00
[shippingtype] => Flat
[fshippingservice] => USPSPriorityFlatRateBox
[shippingservicecost] => 2.0
[shippingsac] => 3.0
[internationalhc] =>
[paypalemailaddress] => sam#jaff.in
[dispatchtimemax] => 3
[location] => CA
[ebaycountry] =>
[ebay_paypalemail] =>
[ReturnsAccepted] => ReturnsAccepted
[returnswithin] => Days_14
[refundoption] => MoneyBack
[shippingcostpaidbyoption] => Seller
[policydescription] => wqdewvfdgbfdggrbf
)
This is my another Array
Array
(
[itemTitle] => fdafdas
[subTitle] => fadsfdas
[categoryId] => 117027
[itemDescription] => fdas
[listingType] => FixedPriceItem
[listingDuration] => Days_5
[startPrice] =>
[buyItNowPrice] => 20.00
[quantity] => 12
[upc] => 31231241341
[paypalEmailAddress] => sam#jaff.in
[returnWithin] => Days_14
[RefundOption] => MoneyBack
[ShippingCostPaidByOption] => Seller
[returnsAccepted] => ReturnsAccepted
[shippingType] => Flat
[cshippingService] =>
[fshippingService] => USPSPriorityFlatRateBox
[dcPostalcode] =>
[ShippingServiceCost] => 2.0
[dshippingPackage] =>
[shippingHandlingcost] =>
[shippingServiceAdditionalCost] => 3.0
[currency] => USD
[country] =>
[location] => CA
[dispatchTimeMax] => 3
[pictureUrl] => Array
(
[0] => http://jaftech.in/ashprey/uploads/69.jpg
)
[policyDescription] => wqdewvfdgbfdggrbf
[internationalShipping] =>
[internationalShippingType] =>
[internationalShippingServiceCost] =>
[internationalshippingServiceAdditionalCost] =>
[cinternationalshippingService] =>
[finternationalshippingService] =>
[internationalShipToLocation] =>
[internationaloriginatingPostalCode] =>
[internationalshippingHandlingcost] =>
[ishippingPackage] => PackageThickEnvelope
[pid] => 89
)
This is because array_diff compares values after typecasting both compared values to string (see documentation notes).
Both of your arrays contain another array (ebay_user and second pictureUrl).
See this answer for recursive array_diff, which should work in your case.
I got the following response output from curl request.
Array
(
[code] => 200
[message] => Message history obtained successfully.
[data] => Array
(
[0] => Array
(
[id] => 3390257
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784285
[ext_message_id] => 152127f68b2000031bf6584fa8494a7b
[send_date] => 2016-01-05 09:54:17
[dest_address] => 8476099160
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
[1] => Array
(
[id] => 3390261
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784289
[ext_message_id] => 152127f6c36000171df5874efba398b4
[send_date] => 2016-01-05 09:54:17
[dest_address] => 6188892860
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
[2] => Array
(
[id] => 3390265
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784293
[ext_message_id] => 152127f65dd000031bf6584fa84949af
[send_date] => 2016-01-05 09:54:17
[dest_address] => 2032415751
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
[3] => Array
(
[id] => 3390270
[aggregator_id] => 25
[user_id] => 184279092
[message_id] => 7784298
[ext_message_id] => 152127f6a2a0001a8d8e4ef0eebbbe5d
[send_date] => 2016-01-05 09:54:17
[dest_address] => 3106193605
[dest_country] => 1
[dest_type] => 1
[source_address] => 8
[source_country] => 1
[source_type] => 2
[message] => Heres how others are earning up to an extra $300/day simply by using their phone or working from home Click Here now: http://bit.ly/1PIiVZl
[transaction_id] =>
[message_status] => 14
[message_schedule_id] => 59335
[last_modified_date] => 2016-01-05 09:54:52
[last_modified_user_id] => 184279092
[keyword_id] => 1459
)
)
)
Now I want to get the array value like $keyword_id = 1459 But I can't access the array.
Assuming this response is stored in a variable called $response, here are some examples of accessing array elements...
$response['data'][0]['id'] // 3390257 (id of 1st data element)
$response['data'][1]['dest_address'] // 6188892860 (dest_address of 2nd data element)
$response['data'][3]['keyword_id'] // 1459 (keyword_id of 4th data element)
You could also iterate over the data elements and print the ids as follows...
foreach ($response['data'] as $i => $element) {
echo "The id of element " . $i . " is " . $element['id'] . "\n";
}
you can get keyword_id through foreach loop. run the foreach loop on your array and get data through keyword_id index.
foreach($array as $value){
$keyword_id = $value['keyword_id'];
}
this will give you keyword_id.
I am using a wrapper that makes it very simple to grab a search term and convert it into an array.
I am having trouble parsing this array. How do I create a foreach() loop to get the values from this array?
This is the simple function that is grabbing the search term and converting to an array. The print_r() simply shows me what is being returned.:
require_once (get_template_directory() . '/Classes/itunes.php');
function itunesSearch() {
$search_term = $_GET['s'];
$results = iTunes::search($search_term, array(
'country' => 'US'
))->results;
print_r($results);
}
For example, let's say I search for Adele. Here is the array that is being returned:
Array ( [0] => stdClass Object ( [wrapperType] => track [kind] => song [artistId] => 262836961 [collectionId] => 1051394208 [trackId] => 1051394215 [artistName] => Adele [collectionName] => 25 [trackName] => Hello [collectionCensoredName] => 25 [trackCensoredName] => Hello [artistViewUrl] => https://itunes.apple.com/us/artist/adele/id262836961?uo=4 [collectionViewUrl] => https://itunes.apple.com/us/album/hello/id1051394208?i=1051394215&uo=4 [trackViewUrl] => https://itunes.apple.com/us/album/hello/id1051394208?i=1051394215&uo=4 [previewUrl] => http://a1912.phobos.apple.com/us/r1000/170/Music6/v4/68/34/f1/6834f1f8-8fdb-4247-492a-c0caea580082/mzaf_3920281300599106672.plus.aac.p.m4a [artworkUrl30] => http://is5.mzstatic.com/image/thumb/Music6/v4/8c/91/5d/8c915d9b-d9e4-f735-1b91-81ca1b6e6312/source/30x30bb.jpg [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music6/v4/8c/91/5d/8c915d9b-d9e4-f735-1b91-81ca1b6e6312/source/60x60bb.jpg [artworkUrl100] => http://is5.mzstatic.com/image/thumb/Music6/v4/8c/91/5d/8c915d9b-d9e4-f735-1b91-81ca1b6e6312/source/100x100bb.jpg [collectionPrice] => 10.99 [trackPrice] => 1.29 [releaseDate] => 2015-11-20T08:00:00Z [collectionExplicitness] => notExplicit [trackExplicitness] => notExplicit [discCount] => 1 [discNumber] => 1 [trackCount] => 11 [trackNumber] => 1 [trackTimeMillis] => 295502 [country] => USA [currency] => USD [primaryGenreName] => Pop [radioStationUrl] => https://itunes.apple.com/station/idra.1051394215 [isStreamable] => )
[1] => stdClass Object ( [wrapperType] => track [kind] => song [artistId] => 262836961 [collectionId] => 420075073 [trackId] => 420075084 [artistName] => Adele [collectionName] => 21 [trackName] => Rolling in the Deep [collectionCensoredName] => 21 [trackCensoredName] => Rolling in the Deep [artistViewUrl] => https://itunes.apple.com/us/artist/adele/id262836961?uo=4 [collectionViewUrl] => https://itunes.apple.com/us/album/rolling-in-the-deep/id420075073?i=420075084&uo=4 [trackViewUrl] => https://itunes.apple.com/us/album/rolling-in-the-deep/id420075073?i=420075084&uo=4 [previewUrl] => http://a818.phobos.apple.com/us/r1000/115/Music/3d/fd/74/mzm.dqadcdcf.aac.p.m4a [artworkUrl30] => http://is5.mzstatic.com/image/thumb/Music/v4/cf/7e/47/cf7e47a8-bb18-9156-43d0-7591d0e0855e/source/30x30bb.jpg [artworkUrl60] => http://is5.mzstatic.com/image/thumb/Music/v4/cf/7e/47/cf7e47a8-bb18-9156-43d0-7591d0e0855e/source/60x60bb.jpg [artworkUrl100] => http://is5.mzstatic.com/image/thumb/Music/v4/cf/7e/47/cf7e47a8-bb18-9156-43d0-7591d0e0855e/source/100x100bb.jpg [collectionPrice] => 10.99 [trackPrice] => 1.29 [releaseDate] => 2011-02-22T08:00:00Z [collectionExplicitness] => notExplicit [trackExplicitness] => notExplicit [discCount] => 1 [discNumber] => 1 [trackCount] => 12 [trackNumber] => 1 [trackTimeMillis] => 228293 [country] => USA [currency] => USD [primaryGenreName] => Pop [radioStationUrl] => https://itunes.apple.com/station/idra.420075084 [isStreamable] => 1 ) ...
This is what I've tried, and several variations of this:
$results_thing = $results['stdClass']['Object'];
foreach ($results_thing as $result) {
echo $result['wrapperType'];
I am getting the error:
Warning: Invalid argument supplied for foreach()
Say that the $results holds the array you given above. Then the code for getting 'wrapperType'...
foreach($results as $result) {
$wrapper_type = $result->wrapperType;
}
I'm so close to this I could just scream.
Here's what I'm after. I have two arrays. The first one is a follows:
array("id", "txtLname", "txtFname");
The second is as follows:
Array
(
[0] => Array
(
[id] => 220
[RecordGUID] => 1233C9-1F7A15-E8A447-C56CB2-227C20-2829E0
[txtEmplid] => 5469857
[txtLname] => Jones
[txtFname] => Richard
[txtMname] =>
[txtEmail] => email address
[Reg_Pass] => umbra1234
[Reg_User] => user
[txtSecEmail] => user#gmail.com
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-15 18:03:18
[Status] => 0
[Approvalcode] => 17dc8e7336f0e9fd3411a4d9617efe865c5744ac
[Approvaldate] => 2013-09-15 18:03:47
[Approved] => 1
)
[1] => Array
(
[id] => 221
[RecordGUID] => DD1E72-368879-68CFE2-E03010-ECE1B1-0974E9
[txtEmplid] => 4454688
[txtLname] => Mathews
[txtFname] => Richard
[txtMname] =>
[txtEmail] => user2#gmail.com
[Reg_Pass] => umbra1234
[Reg_User] => user
[txtSecEmail] => user3#gmail.com
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-16 12:28:08
[Status] => 0
[Approvalcode] => 7182769e45dc38a3a747c4bdb128e2f0a8c658e4
[Approvaldate] =>
[Approved] => 0
)
[2] => Array
(
[id] => 222
[RecordGUID] => 40D8E7-04C600-30A829-8E26CC-498BBE-9D3DF6
[txtEmplid] =>
[txtLname] =>
[txtFname] =>
[txtMname] =>
[txtEmail] =>
[Reg_Pass] =>
[Reg_User] =>
[txtSecEmail] =>
[dtDOB] => 1979-02-28
[drpStatus] => STUDENT
[lstWaive] => 1
[ENTERED] => 2013-09-16 12:28:24
[Status] => 0
[Approvalcode] => 8d2c33f7b7d6ef620811dbc4358e8103346bfb59
[Approvaldate] =>
[Approved] => 0
)
)
All I need is to loop through the second array and remove the items that do not match the first. The result would be as follows:
Array
(
[0] => Array
(
[id] => 220
[txtLname] => Method
[txtFname] => Richard
)
[1] => Array
(
[id] => 221
[txtLname] => Method
[txtFname] => Richard
)
[2] => Array
(
[id] => 222
[txtLname] => Method
[txtFname] => Richard
)
)
Here's what I've done so far.
foreach ($r as $reg) {
foreach($reg as $k => $v) {
if ($k == !in_array($k, $f)) {
echo $reg[$k];
}
}
}
The echo response I get is a listing of all of the data from the first array, minus the fields that are in the first array. So, it is removing items that
I want removed, but I can't seem to get it into the proper format.
Thanks in advance for your help.
$arr1 = array("id"=>1, "txtLname"=>1, "txtFname"=>1)
$result = array();
// $arr2 = array 2 (check your code)
foreach ($arr2 as $el) {
$result[] = array_intersect_key($el, $arr1);
}
var_dump($result);
replace echo $reg[$k]; with unset($reg[$k]);