I am very new to PHP World and stuck with this problem from last three days.
Array
(
[0] => stdClass Object
(
[sim_id] => 15
[Tarrif_code] => D34
[rental] => USD 345
[service_provider] => Vodafone
[incoming_calls] => 3.2 /sec
[local_outgoing] => 3.2 /sec
[international_call] => 3.2 /sec
[company_name] => Airtel
[local_text] =>
[international_text] =>
[data] => 2G
[sim_type_id] => 1
[offer_short_descrption] =>
[validity] =>
[dialing_instruction] =>
[recharge_instruction] =>
[usage_warning] =>
[country_id] => 1
[type] => Prepaid
)
)
and the result should be
Array
(
[0] => stdClass Object
(
[sim_id] => 15
[Tarrif_code] => D34
[company_name] => Airtel
[rental] => USD 345
[service_provider] => Vodafone
[country_id] => 1
[type] => Prepaid
[1] => stdClass Object
(
[international_call] => 3.2 /sec
[incoming_calls] => 3.2 /sec
[local_outgoing] => 3.2 /sec)
[local_text] =>
[international_text] =>
[data] => 2G
[sim_type_id] => 1
[offer_short_descrption] =>
[validity] =>
[dialing_instruction] =>
[recharge_instruction] =>
[usage_warning] =>
)
)
Regarding data provided in question:
$arr[0] = new stdClass;
$arr[0]->id = 1;
$arr[0]->name = 2;
$arr[0]->state = 3;
$arr[0]->land = 4;
$arr2[0] = new stdClass;
foreach($arr[0] as $k => $v)
{
if (!in_array($k, array('id', 'land')))// array of keys for first object
{
$arr2[0]->{$k} = $v;
unset($arr[0]->{$k});
}
}
var_dump($arr[0]);
echo "<br>";
var_dump($arr2[0]);
exit;
Related
Array:
$ecodesAr = Array (
[0] => 1Z0-060
[1] => 98-375
[2] => 98-368
[3] => 98-367 )
for($k=0; $k<count($ecodesAr); $k++){
$arrayTB[$k] = $this->Functions->exam('title', $ecodesAr[$k]); }
Modal code:
public function exam($q, $d) {
$q = $this->db->where($q, $d)
->get('exams');
return $q->row();
}
Result:
Array (
[0] =>
[1] =>
[2] =>
[3] => stdClass Object ( [id] => 1091 [hot_exam] => 0 [top_exam] => 0 [category] => 114 [subcats] => 288 [slug] => 98-367 [sku] => OI5Ao [title] => 98-367 [name] => MTA Security Fundamentals [update] => 2021-09-16 [regular_price] => 130 [sale_price] => 59 [on_homepage] => 0 [on_request] => 0 [expired] => 0 [path_slug] => 98-367.pdf [questions] => 123 [demo_slug] => 98-367-demo.pdf [prc_price] => 65 [prc_demo] => 98-367-demo [prc_exam] => 98-367 [is_active] => 1 )
)
The First 3 values are skiped in the output and just the last value got, I want to all array data against values please help anyone!
$productsarry = Array (
[0] => Milk
[1] => Cream
[2] => Sugar
[3] => Yougert
);
for($k = 0; $k < count($productsarry); $k++) {
$arrayTB[$k] = query("select slug, qty, name, price from exams where title ='$ecode'")->row();
}
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;
}
}
}
If there are 2 posts for 3 pages, the function that fetches the posts, will return Array of [6] like this:
Array
(
[0] => Array
(
[post_text] => Test 3:30
[post_id] => 46
[concrete_id] => 5c3f55b5ebb084c078c92d13
[post_data_start] => 2019-02-06 15:31:00
[post_type] => II
[page_id] => 7
)
[1] => Array
(
[post_text] => Test 3:30
[post_id] => 46
[concrete_id] => fus98f67a9
[post_data_start] => 2019-02-06 15:31:00
[post_type] => II
)
[2] => Array
(
[post_text] => Test 3:30
[post_id] => 46
[concrete_id] => 5c3f55b5ebb084c078c94c0
[post_data_start] => 2019-02-06 15:31:00
[post_type] => II
)
[3] => Array
(
[post_text] => test
[post_id] => 43
[concrete_id] => 5c3f55b5ebb084c078c92d13
[post_data_start] => 2019-02-06 17:31:00
[post_type] => II
)
[4] => Array
(
[post_text] => test
[post_id] => 43
[concrete_id] => fus98f67a9
[post_data_start] => 2019-02-06 17:31:00
[post_type] => II
)
[5] => Array
(
[post_text] => test
[post_id] => 43
[concrete_id] => 5c581a33ebb084c078ceb6cc
[post_data_start] => 2019-02-06 17:31:00
[post_type] => II
)
)
In my cURL call I have to pass the concrete_id as an array (because it costs 10cents a call), in this case I have to make 6 cURL calls, but I could only do 2 calls passing concrete_id as Array. The API I use works that way (but the question has nothing to do with the API)
I want to get an array with unique id, but with array of the different values (or just concrete_id would do):
Array
(
[0] => Array
(
[post_text] => Test 3:30
[post_id] => 46
[concrete_id] =>
Array(
[0] => 5c3f55b5ebb084c078c92d13
[1] => fus98f67a9
[2] => 5c3f55b5ebb084c078c94c0
)
[post_data_start] => 2019-02-06 15:31:00
[post_type] => II
)
[1] => Array
(
[post_text] => test
[post_id] => 43
[concrete_id] =>
Array(
[0] => 5c3f55b5ebb084c078c92d13
[1] => fus98f67a9
[2] => 5c3f55b5ebb084c078c94c0
)
[post_data_start] => 2019-02-06 17:31:00
[post_type] => II
)
)
this is the best I could come up with, but my brain is hurting:
$tmpposts=$posts;
foreach($posts as $k=>$post){
foreach($tmpposts as $k2=>$post2){
if($post['post_id']==$post2['post_id']){
$post2['concrete_id']=array($post2['concrete_id']);
array_push($post2['concrete_id'],$post['concrete_id']);
$array[]=$post2;
array_splice($array,$k);
}
}
}
You may reduce the array with simple for-loop. Consider the follow:
$arr = []; // example just with "id" and "concrete_id" but you can add all other data
$arr[] = array("id" => 46, "concrete_id" => "5c3f55b5ebb084c078c92d13");
$arr[] = array("id" => 46, "concrete_id" => "fus98f67a9");
$arr[] = array("id" => 46, "concrete_id" => "5c3f55b5ebb084c078c94c0");
$arr[] = array("id" => 43, "concrete_id" => "5c3f55b5ebb084c078c92d13");
$arr[] = array("id" => 43, "concrete_id" => "fus98f67a9");
foreach($arr as $e) {
if (!isset($ans[$e["id"]])) {
$ans[$e["id"]] = $e; // element doesn't exist set first time
$ans[$e["id"]]["concrete_id"] = [$e["concrete_id"]]; // make the concrete_id as array with 1 element
} else { // the id already set just append the concrete_id
$ans[$e["id"]]["concrete_id"][] = $e["concrete_id"];
}
}
You can use array_values if you don't want the id as keys in the result as: $ans = array_values($ans);
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]);
I have an array that looks like
[products] => Array
(
[0] => stdClass Object
(
[order_product_id] => 91385
[order_id] => 5065
[nid] => 2140
[title] => Gi Treasure
[manufacturer] =>
[model] => giftcard
[qty] => 5
[cost] => 0.00000
[price] => 25.00000
[weight] => 0
[data] => Array
(
[gift_description] => HJello!
[gift_email] => dubccom
[gift_sender] => Hello
[gift_sendDate] => 2011-10-25
[gift_title] => Thesure
[gift_card] => 2130
[gift_price] => 25
[gift_qty] => 5
[gift_name] => Steveek
[module] => uc_product
[cert_code] => 8-x8mfqXyUYXze
)
[order_uid] => 1
)
[1] => stdClass Object
(
[order_product_id] => 91386
[order_id] => 5065
[nid] => 2140
[title] => Gift asure 2
[manufacturer] =>
[model] => giftcard
[qty] => 1
[cost] => 0.00000
[price] => 35.00000
[weight] => 0
[data] => Array
(
[gift_description] => Hello There!
[gift_email] => dubcaom
[gift_sender] => Hello
[gift_sendDate] => 2011-10-25
[gift_title] => The Holida
[gift_card] => 2134
[gift_price] => 35
[gift_qty] => 1
[gift_name] => Steven
[module] => uc_product
[cert_code] => 9-8xsxgDW9yrMq
)
[order_uid] => 1
)
)
And I want to get the data array from array of products where the order_product_id (so if it was 91385 I would get
[data] => Array
(
[gift_description] => Hello
[gift_email] => dubccom
[gift_sender] => Hello
[gift_sendDate] => 2011-10-25
[gift_title] => Thesure
[gift_card] => 2130
[gift_price] => 25
[gift_qty] => 5
[gift_name] => Steveek
[module] => uc_product
[cert_code] => 8-x8mfqXyUYXze
)
Any help how I could do so?
function search_products($id,$products)
{
$id = intval($id);
foreach($products as $product)
{
if($product->order_product_id == $id)
{
return($product->data);
}
}
return null;
}
An educated guess at what you're looking for. Call it like search_products(91385, $products). If it returns null, it hasn't found the product ID. I also added a call to intval so if you're relying on user input for this, it'll be an int regardless. If you already sanitize it to an int, this doesn't hurt.
EDIT: Misread original post. Updated from array syntax to object property syntax.
function data_by_order ($arr, $orderId) {
foreach ($arr as $item) { // Loop the array
if ($item->order_product_id == $orderId) { // Test this item for the right order id
return $item->data; // Return the data array
}
}
return FALSE; // Return false if we didn't find it
}
// Example usage:
$data = data_by_order($array,91385);
print_r($data);
$prod_data = false;
foreach ($products as $product)
{
if ($product->order_product_id == 91385)
{
$prod_data = $product->data;
break;
}
}
if ($prod_data) {
// knock yourself out
}
Quite similar to plain English, huh?