I got the array below and I would like to keep recent added record which is [2] and delete others
Array (
[0] => Array ( [id] => 1 [is_sub] => 0 [product] => New [quantity] => 1 [price] => [total_item_price] => 0 [comments] => )
[1] => Array ( [id] => 1 [is_sub] => 0 [product] => Old [quantity] => 1 [price] => [total_item_price] => 0 [comments] => )
[2] => Array ( [id] => 4 [is_sub] => 0 [product] => Mix [quantity] => 1 [price] => [total_item_price] => 0 [comments] => ) )
i got an answer thanks for helping.
$last = array_slice($orders, -1, 1, true);
Shorter:
$last = array_pop($orders);
$last_data = array_pop($data);
print_r($last_data);
Related
hi there i am new to php and i want to access isMultiple element of array how can i do that then i want to show if the value of isMultiple is 1 then it should show yes.. any help would be appreciated and it would be great......!!.
Array
(
[result] => Array
(
[0] => Array
(
[pkFeatureTypeId] => 72
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[1] => Array
(
[pkFeatureTypeId] => 32
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[2] => Array
(
[pkFeatureTypeId] => 33
[Name] =>
[Status] => 0
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
[3] => Array
(
[pkFeatureTypeId] => 35
[Name] =>
[Status] => 1
[isMultiple] => 1
[isSpecial] => 1
[Order] => 0
)
)
)
Try this.
foreach($data['result'] as $item){
if($item['isMultiple'] == '1'){
echo 'Yes';
}
}
I have the following array:
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
[2] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 3
[category_id] => 1
[amount] => 15
[cost] => 2000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:58
)
[0] => Array
(
[total_sell] => 6
)
)
[3] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 4
[category_id] => 1
[amount] => 50
[cost] => 8000
[paid] => 0
[comment] =>
[created] => 2015-06-23 01:10:10
)
[0] => Array
(
[total_sell] => 6
)
)
)
I want to remove duplicate entry of [Import][product_id]. So my expected result is :
Array
(
[0] => Array
(
[Import] => Array
(
[product_id] => 1
[id] => 1
[category_id] => 1
[amount] => 50
[cost] => 8320
[paid] => 0
[comment] => transportation and others cost: 100
[created] => 2015-06-22 12:09:20
)
[0] => Array
(
[total_sell] => 6
)
)
[1] => Array
(
[Import] => Array
(
[product_id] => 2
[id] => 2
[category_id] => 2
[amount] => 15
[cost] => 3000
[paid] => 0
[comment] =>
[created] => 2015-06-22 12:10:36
)
[0] => Array
(
[total_sell] => 1
)
)
)
Would you write a function to filter this type of array and produce expected result. I have been googling for 2 days but no luck.
This is a handy one liner that should do the trick:
$unique= array_map("unserialize", array_unique(array_map("serialize", $original)));
If the underlying arrays are not identical, that won't work, in which case I think you could do:
$unique = array_intersect_key($original ,
array_unique(
array_map(function($item) {
return $item['Import']['product_id'];
}, $original)
)
);
Tested: http://sandbox.onlinephpfunctions.com/code/8aee5cbd614e0ddd1a03dfaa7e98c72fbbe7d68d
Here is a quick stable sort and reduce which runs in linearithmic time. First-encountered product Id's are kept, and entries with duplicate product Id's are ignored.
// Stable sort
sort($in);
// Reduce
$out = array_reduce($in, function(&$acc, &$item){
if($item['Import']['product_id'] !== #$acc[sizeof($acc)-1]['Import']['product_id']) {
$acc[] = $item;
}
return $acc;
}, []);
Demo: http://ideone.com/BP0eUJ
Update: Here is an even better linear-time algorithm that does the same as above using a fast "hash table" lookup. Again, the first-encountered product Id is kept and subsequent ones of the same Id are ignored.
$out = [];
$hashTable = [];
foreach($in as $item) {
$pid = $item['Import']['product_id'];
if(!isset($hashTable[$pid])) {
$out[] = $item;
$hashTable[$pid] = true;
}
}
Demo: http://ideone.com/5RF0og
I have the following array structure:
[parents] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[created] => 2014-11-09 13:47:37
[content] => This is a test discussion
[status] => 1
[parent] => 0
[project_id] => 1
)
[1] => Array
(
[id] => 4
[user_id] => 1
[created] => 2014-11-09 13:52:02
[content] => 456789
[status] => 1
[parent] => 0
[project_id] => 1
)
)
[children] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 1
[created] => 2014-11-09 13:47:53
[content] => This is a test reply....
[status] => 1
[parent] => 1
[project_id] => 1
)
[1] => Array
(
[id] => 3
[user_id] => 1
[created] => 2014-11-09 13:48:13
[content] => This is a test reply....!!!
[status] => 1
[parent] => 1
[project_id] => 1
)
[2] => Array
(
[id] => 5
[user_id] => 1
[created] => 2014-11-09 13:52:17
[content] => 8765432
[status] => 1
[parent] => 4
[project_id] => 1
)
)
I would like to merge them into a parent/child relationship so it looks like follows:
[parents] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 1
[created] => 2014-11-09 13:47:37
[content] => This is a test discussion
[status] => 1
[parent] => 0
[project_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 2
[user_id] => 1
[created] => 2014-11-09 13:47:53
[content] => This is a test reply....
[status] => 1
[parent] => 1
[project_id] => 1
)
[1] => Array
(
[id] => 3
[user_id] => 1
[created] => 2014-11-09 13:48:13
[content] => This is a test reply....!!!
[status] => 1
[parent] => 1
[project_id] => 1
)
)
)
)
How could I go about doing that with PHP?
Assuming that all ids are unique in your parents array, you can do this:
// build a new array using parent's ID values as the key,
// to simplify searching for parent IDs.
foreach ($parents as $key => $value){
$merged[$value['id']] = $value;
}
foreach ($children as $child){
$parentID = $child['parent'];
if (array_key_exists( $parentID, $merged )) {
// add the child array to its parent's ['children'] value
$merged[$parentID]['children'][] = $child;
}
}
In the resulting array $merged, the key of each parent item will be set to its id, and all children will be nested under their corresponding parents.
I have a php variabl $purchase_data which when I did
print_r('purchase_data');
I got the output as
Array
(
[downloads] => Array
(
[0] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
)
[fees] => Array
(
)
[subtotal] => 1
[discount] => 0
[tax] => 0
[price] => 1
[purchase_key] => a8d14e34ba425f9de6afe3ad4809587e
[user_email] => esh#test.com
[date] => 2014-05-11 20:07:22
[user_info] => Array
(
[id] => 1
[email] => eshtest.com
[first_name] => esh
[last_name] =>
[discount] => none
[address] =>
)
[post_data] => Array
(
[edd_email] => esh#test.com
[edd_first] => esh
[edd_last] =>
[edd_phone] => 919995871693
[edd-user-id] => 1
[edd_action] => purchase
[edd-gateway] => sample_gateway
)
[cart_details] => Array
(
[0] => Array
(
[name] => Shirt
[id] => 28
[item_number] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
[item_price] => 1
[quantity] => 1
[discount] => 0
[subtotal] => 1
[tax] => 0
[price] => 1
)
)
[gateway] => sample_gateway
[card_info] => Array
(
[card_name] =>
[card_number] =>
[card_cvc] =>
[card_exp_month] =>
[card_exp_year] =>
[card_address] =>
[card_address_2] =>
[card_city] =>
[card_state] =>
[card_country] =>
[card_zip] =>
)
)
How can i store the value to edd_phone [edd_phone] into variable $mobileNo ?
Sorry for ths kind of a question.But badly need help
If you want to assign the value of [edd_phone] to $mobileNo , use this :
$mobileNo = $purchase_data['post_data']['edd_phone'];
As a side note : Your array is very complexly nested. If I were you, I would avoid such complex arrays.
I've used
unset($quotes_array[0]['methods'][0]);
$quotes_array[0]['methods'] = array_values($quotes_array[0]['methods']);
to remove the first element of an array, but the selection form where the array is used no longer responds correctly to the radio button selected by the user.
The original array looked like this:
Array
(
[0] => Array
(
[id] => advshipper
[methods] => Array
(
[0] => Array
(
[id] => 1-0-0
[title] => Trade Shipping
[cost] => 20
[icon] =>
[shipping_ts] =>
[quote_i] => 0
)
[1] => Array
(
[id] => 2-0-0
[title] => 1-2 working days
[cost] => 3.2916666666667
[icon] =>
[shipping_ts] =>
[quote_i] => 1
)
[2] => Array
(
[id] => 4-0-0
[title] => 2-3 working days
[cost] => 2.4916666666667
[icon] =>
[shipping_ts] =>
[quote_i] => 2
)
[3] => Array
(
[id] => 8-0-0
[title] => Click & Collect
[cost] => 0
[icon] =>
[shipping_ts] =>
[quote_i] => 3
)
)
[module] => Shipping
[tax] => 20
)
)
And the modified array looks like this:
Array
(
[0] => Array
(
[id] => advshipper
[methods] => Array
(
[0] => Array
(
[id] => 2-0-0
[title] => 1-2 working days
[cost] => 3.2916666666667
[icon] =>
[shipping_ts] =>
[quote_i] => 1
)
[1] => Array
(
[id] => 4-0-0
[title] => 2-3 working days
[cost] => 2.4916666666667
[icon] =>
[shipping_ts] =>
[quote_i] => 2
)
[2] => Array
(
[id] => 8-0-0
[title] => Click & Collect
[cost] => 0
[icon] =>
[shipping_ts] =>
[quote_i] => 3
)
)
[module] => Shipping
[tax] => 20
)
)
I suspect the problem is caused by the fact that in the modified array, [quote_i] now starts at 1, and not 0 as in the original. So i have [quote_i] as 1, 2 then 3 but it should be 0, 1, then 2.
I've tried using array_walk to correct this, but not been successful.
Any suggestions on a solution to this?
The trick basically would be to correct quote_i
$counter = 0;
foreach ($quotes_array[0]['methods'] as $key => $value)
{
$quotes_array[0]['methods'][$key]['quote_i'] = $counter;
$counter++;
}
Using array_walk with example code that should match your use case
<?php
foreach ($quotes_array[0]['methods'] as $a) {
$a = array(
array('quote_i' => 1),
array('quote_i' => 2),
array('quote_i' => 3)
);
array_walk($a, function(&$item, $key) {
$item['quote_i'] = $item['quote_i'] - 1;
});
var_dump($a);
// array([0] => array('quote_i' => 0), [1] => array('quote_i' => 1), [2] => array('quote_id' => 2))
}