Im using cakephp 2.0 and i have data submitted that i want to cleanup, the array structure is below how do i delete the element (quoteitem) where the quantity=null?
i have this but it doesnt work;
foreach($this->request->data['Quoteitem'] as $qi) {
if($qi['quantity']==null){
echo 'quantity is null,delete this quote item from array';
unset($qi);
}
}
structure of array called ($this->request->data)
Array
(
[Quote] => Array
(
[customer_id] => 72
[user_id] => 104
)
[Range] => Array
(
[id] =>
)
[Quoteitem] => Array
(
[0] => Array
(
[product_id] =>
[unitcost] =>
[quantity] => 1
)
[1] => Array
(
[product_id] =>
[unitcost] =>
[quantity] => 22
)
[2] => Array
(
[product_id] => 339
[unitcost] => 5
[quantity] =>
)
)
)
You can remove it using the array keys:
foreach($this->request->data['Quoteitem'] as $key => $qi) {
if($qi['quantity'] == null){
echo 'quantity is null,delete this quote item from array';
unset($this->request->data['Quoteitem'][$key]);
}
}
Note that this will create gaps in the array (nonexistent indexes), usually this won't be a problem but if it is you can re-index the array with array_values().
Foreach makes a copy, try this:
foreach($this->request->data['Quoteitem'] as $key => $qi) {
if($qi['quantity']==null){
echo 'quantity is null,delete this quote item from array';
unset($this->request->data['Quoteitem'][$key]);
}
}
Related
I want to find one key of array value in another key in the same array.
Following is my array output.
Array
(
[0] => Array
(
[id] => 1187
[user_id] => 168
[content] => Thanks a lot man
[item_id] => 1182
[secondary_item_id] => 1186
)
[1] => Array
(
[id] => 1186
[user_id] => 222
[content] => Great Post
[item_id] => 1182
[secondary_item_id] => 1182
)
[2] => Array
(
[id] => 1183
[user_id] => 185
[content] => Amazing first post
[item_id] => 1182
[secondary_item_id] => 1182
)
[3] => Array
(
[id] => 1184
[user_id] => 179
[content] => Wonder Post
[item_id] => 1182
[secondary_item_id] => 1182
)
[4] => Array
(
[id] => 1185
[user_id] => 168
[content] => Rocking Thanks
[item_id] => 1182
[secondary_item_id] => 1183
)
)
Here you can see id & secondary_item_id key in array, So I want to find which array id is used in secondary_item_id key of array or how can i search id key of array is used in secondary_item_id key in array.
For example.. you can see 'Great Post' id key is used in 'Thanks a lot man' secondary_item_id key of array. So i want to search which array key id is used in secondary_item_id key of array.
I have tried using following way but not working.
$commentData = array();
foreach ($commentQuery as $key => $value) {
if(array_search($value['id'], array_column($commentQuery, 'secondary_item_id'))){
echo "list Found".$value['id'];
}else{
echo "list not Found".$value['id'];
}
}
It's only return list not Found1187.
i hope this works for you
$ch=true;
foreach ($commentQuery as $key => $value) {
foreach ($commentQuery as $key2 => $value2) {
if($value2['id']==$value['secondary_item_id']){
$ch=false;
echo "list Found ".$value['id']." in array key ".$key2;
break;
}
}
if($ch){
echo "list not Found".$value['id'];
}
$ch=true;
}
Try this:-only single foreach loop using
$search_column variable is out of foreach because every time this sentence execute and your code execute slowly
$search_column = array_column($commentQuery, 'secondary_item_id');
foreach ($commentQuery as $key => $value) {
$search = null;
$search = array_search($value['id'], $search_column);
if (gettype($search) == 'integer') {
echo "list Found" . $value['id'];
} else {
echo "list not Found" . $value['id'];
}
echo "<br>";
}
I have an array of 50000 arrays and i want to remove the "id" key-value pair from each of them.
I would rather not loop through 50k elements and was wondering if there was an efficient way to do it.
Array
(
[0] => Array
(
[id] => 713061
[market] => usd-btc
[price] => 3893.69
)
[1] => Array
(
[id] => 713056
[market] => usd-btc
[price] => 3893.69
)
[2] => Array
(
[id] => 713051
[market] => usd-btc
[price] => 3893.69
)
[3] => Array
(
[id] => 713046
[market] => usd-btc
[price] => 3893.69
)
[4] => Array
(
[id] => 713041
[market] => usd-btc
[price] => 3892.95
)
[5] => Array
(
[id] => 713036
[market] => usd-btc
[price] => 3892.95
)
I tried both the following but does not seem to be working:
// Remove ID
foreach($server_data as $sd)
{
unset($sd['id']);
}
unset($server_data['id']);
PRINT_R($server_data);
The $server_data is still returning the array with the $id element;
Any thoughts?
This creates a copy of the subarray, so when you change it, the main array is not affected:
foreach ($server_data as $sd)
{
unset($sd['id']);
}
You can unset from the original array:
foreach (array_keys($server_data) as $index)
{
unset($server_data[$index]['id']);
}
Or pass the subarray a reference so that the original is changed:
foreach ($server_data as &$sd)
{
unset($sd['id']);
}
Or, more tersely:
array_walk($server_data, function (&$item) { unset($item['id']); });
There's no reason I can think of to remove it (just ignore it), however you can run it through a callback that removes id and returns the rest:
$server_data = array_map(function($v) { unset($v['id']); return $v; }, $server_data);
I want to merge the 2 arrays of objects based on the 'id' field of Array1 and the 'itemVendorCode' of Array2. I also wanted to remove from the resulting arrays of object anything that didn't match.
Array1:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
)
[1] => stdClass Object
(
[id] => 89-575-2354
[qty] => 24
[price] => 230.35
)
[2] => stdClass Object
(
[id] => 89-605-1250
[qty] => 2
[price] => 230.35
)
)
Array2:
Array
(
[0] => Item Object
(
[internalId] => 14062
[itemVendorCode] => 89-605-1250
)
[1] => Item Object
(
[internalId] => 33806
[itemVendorCode] => 89-575-2354
)
[2] => Item Object
(
[internalId] => 64126
[itemVendorCode] => 26-295-1006
)
)
I was able to solve this by this code:
$indexed = array();
foreach($itemsArray as $value) {
$indexed[$value->itemVendorCode] = $value;
}
$results = array();
foreach($vendorItems as $obj) {
$value = $indexed[$obj->id];
if (isset($value)) {
foreach($value as $name => $val) {
$obj->$name = $val;
array_push($results, $obj);
}
}
}
print_r($results);
credits to the original poster. I just modified it a bit,
I was able to get the result like this:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[1] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[2] => stdClass Object
(
[id] => 14-102-1010
[qty] => 16
[price] => 3.2
[internalId] => 57033
[itemVendorCode] => 14-102-1010
)
)
I think you will have to use array_map function which provides you a callback function to execute on array(s).
In the callback function:
- declare your array1
- foreach the second array
- set an if statement to check that the current iteration with the id value matches the itemVendorCode of the array2 and return it
something like this:
// You have to specify to PHP to use a local copy of your $array2 to works with it into your callback
$cb = function ($obj1) use ($array2)
{
// you foreach this array
foreach ($array2 as $obj2) {
// if the value of id matches itemVendorCode
if ($obj1->id === $obj2->itemVendorCode) {
// you return the id
return $obj->id;
}
}
};
// this function will fill a new array with all returned data
$mergedArray = array_map($cb, $array1);
This code is a sample but doesn't provide you, your needled solution, try to update it to do what you exactly want ;)
I want to perform an intersection of two arrays that have different structures, but both have one key common (fid). I want a new (filtered second) array after intersection with first array. below is my code and two arrays :
first array:
Array
(
[0] => Array
(
[fid] => 1
)
[1] => Array
(
[fid] => 3
)
)
Second array:
Array
(
[0] => Array
(
[fid] => 9
[functionality] => testing
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[2] => Array
(
[fid] => 2
[functionality] => view functionality category
[funcat_id] => 1
[name] => functionality
)
[3] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
[4] => Array
(
[fid] => 4
[functionality] => edit functionality
[funcat_id] => 1
[name] => functionality
)
)
I want this Output :
Array
(
[0] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
)
I tried this code but I'm not getting the right answer:
$result=array_intersect($array1,$array2);
//Or this also
$result=recursive_array_intersect_key($array1,$array2);
Please let me know, if any one can do this ?
I do not know if a function does exists to do this outright, but alternatively, you can just loop them instead:
$result = array();
foreach($array2 as $val2) {
foreach ($array1 as $val1) {
if($val2['fid'] == $val1['fid']) {
$result[] = $val2;
}
}
}
echo '<pre>';
print_r($result);
Sample Output
Or if you're using PHP 5.5 or greater:
$val1 = array_column($array1, 'fid');
$result = array_filter($array2, function($val2) use($val1) {
return in_array($val2['fid'], $val1);
});
foreach($array2 as $val)
{
$i=0;
foreach($array1 as $val1)
{
if($val['fid']==$val1['fid'])
{
$i++;
}
}
if($i!=0)
{
$a[]=$val;
}
}
print_r($a);
I have a problem like i have a multidimensional array and i want to remove a parent array if an inner array contains 'edit'
the array is like this
[fields] => Array
(
[0] => Array
(
[header] => Array
(
[fieldName] => edit
[displayName] => Edit
[width] => 40
[group] => 0,1,2,3
[cssClass] => dgAssetsCentered
)
[visibility] => Array
(
[showOnStart] => 1
[editable] =>
)
[cell] => Array
(
[type] => link
[params] => Array
(
[label] => Edit
)
)
[sort] => Array
(
[sortable] => 1
)
[validator] => Array
(
[name] =>
[params] =>
)
)
[1] => Array
(
[header] => Array
(
[fieldName] => OFFERID
[displayName] => Offer Id
[group] => 0
i want to remove array 0 because inner array contains 'edit' i am able to delete [header] array not able to delete [0] array.please suggest.
thanks
alex.
unset(fields); will clear the entire array.
unset(fields[0]); will clear the element 0 inside fields array
foreach($fields as $key => $field)
{
if(isset($field['header']['fieldName']) && $field['header']['fieldName'] == 'edit')
unset($field[$key]);
}
UPD:
Also, if you wanna remove array, if any element equal 'edit', try this algorithm:
foreach($fields AS $key => $array)
{
foreach($array as $innered)
{
if(array_search('edit', $innered))
{
unset($fields[$key]);
break;
}
}
}