how do i get array element isMultiple in php - php

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';
}
}

Related

Remove array1 value which dosnt exist in array2 in php associative arrays

I have five associative arrays in PHP which contain data from the database.
Here is an example of array 1 :
Array1
(
[0] => Array
(
[id] => 2
[status] => 0
[user] => 86
[project] => 0
[project1] => 0
[project2] => 0
[project3] => 0
[project4] => 0
[project5] => 0
[project6] => 0
[email] => info#mail.com
[day] => 06/30/2013
)
[1] => Array
(
[id] => 2
[status] => 0
[user] => 86
[project] => 0
[project1] => 0
[project2] => 0
[project3] => 0
[project4] => 0
[project5] => 0
[project6] => 0
[email] => infox#mail.com
[day] => 06/30/2013
)
)
Now here is array 2
Array
(
[0] => Array
(
[id] => 2
[status] => 0
[user] => 86
[project] => 0
[task] => 36
[email] => info#mail.com
[day] => 06/30/2013
)
)
I want to apply filter on my first array from array2 to show only this after applying filter in result array
result
(
[0] => Array
(
[id] => 2
[status] => 0
[user] => 86
[project] => 0
[project1] => 0
[project2] => 0
[project3] => 0
[project4] => 0
[project5] => 0
[project6] => 0
[email] => info#mail.com
[day] => 06/30/2013
)
)
You can simply use :
array_merge($array1,$array2);

How to reformat an array using php?

I have the following array:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 50
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[1] => Array
(
[Import] => Array
(
[id] => 2
[category_id] => 2
[product_id] => 2
[amount] => 15
[cost] => 3000
[comment] =>
[created] => 2015-06-22 18:10:36
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[2] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
I want to remove duplicate value of same product_id inside [Import][product_id] but want sum [Import][amount]. My expected array is:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 65
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[1] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
It will be really a gift if anyone give a function to solve this issue.
$filteredArray = [];
foreach ($array as $productData) {
if (isset($filteredArray[$productData['Import']['product_id']])) {
$filteredArray[$productData['Import']['product_id']]['Import']['amount'] += $productData['Import']['amount'];
}
else {
$filteredArray[$productData['Import']['product_id']] = $productData;
}
}
print_r($filteredArray);
Ah.. forgot to mention - $array is Your base array.
/**
* Removes duplicate summing amount from products array
*
* #param array $array
* #return array
*/
function removeDuplicates($array)
{
$idsCount = array();
foreach ($array as $key => $value) {
$idsCount[$value['Import']['product_id']]['count'] += 1;
$idsCount[$value['Import']['product_id']]['sum'] += $value['Import']['amount'];
if ($idsCount[$value['Import']['product_id']]['count'] > 1) {
unset($array[$key]);
$array[$idsCount[$value['Import']['product_id']]['key']]['Import']['amount'] = $idsCount[$value['Import']['product_id']]['sum'];
} else {
$idsCount[$value['Import']['product_id']]['key'] = $key;
}
}
return $array;
}
i know it looks crazy but is formatted on your specific array.

Array - how to replace record with newly added

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);

Convert flat PHP array to multidimensional array

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.

Store a variable from the array of object php

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.

Categories