This question already has answers here:
PHP array delete by value (not key)
(20 answers)
Closed 3 years ago.
Following is my array and i want to remove specific value from array.
Array
(
[0] => Array
(
[name] => categoryfilter
[value] => 127
)
[1] => Array
(
[name] => price_min
[value] => sd
)
[2] => Array
(
[name] => price_max
[value] => sdsd
)
[3] => Array
(
[name] => action
[value] => myfilter
)
[4] => Array
(
[name] => quantity
[value] => 1
)
[5] => Array
(
[name] => quantity
[value] => 1
)
[6] => Array
(
[name] => quantity
[value] => 1
)
[7] => Array
(
[name] => quantity
[value] => 0
)
[8] => Array
(
[name] => quantity
[value] => 0
)
[9] => Array
(
[name] => quantity
[value] => 0
)
[10] => Array
(
[name] => quantity
[value] => 1
)
);
I want to remove all quantity key items from array .
I have tried using following way but not remove display same thing.
if (($key = array_search('quantity', $_POST['product'])) !== false) {
unset($_POST['product'][$key]);
}
echo "<pre>";print_r($_POST['product']);
Loop through the array.
foreach ($_POST['product'] as $k => $p) {
if ($p['name'] == 'quantity') {
unset($_POST['product'][$k];
}
}
Use array_filter with callback function
$f = array_filter($a, function($v){return $v['name'] != 'quantity';});
Working example : https://3v4l.org/DqoLj
Related
I have an array like below and i am trying to filter entries that have a certain label or that are empty (not set). This is however not working. I guess it is do the fact that it is multi-dimensional. Anyone?
My array:
Array
(
[0] => Array
(
[id] => app_i-have
[type] => checkbox
[props] => Array
(
[required] => 0
[label] => I have
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => mobile studio
)
[1] => Array
(
[baseline] => 0
[value] => makeup artist
)
)
)
)
[1] => Array
(
[id] => app_customers
[type] => select
[props] => Array
(
[required] => 0
[label] => Customers
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => Private
)
[1] => Array
(
[baseline] => 0
[value] => Business
)
)
)
)
[2] => Array
(
[id] => app_exclude
[type] => select
[props] => Array
(
[required] => 0
[label] => Exclude
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => option 1
)
[1] => Array
(
[baseline] => 0
[value] => option 2
)
)
)
)
[3] => Array
(
[id] => app_exclude-2
[type] => input_text
[props] => Array
(
[required] => 0
[label] => Exclude 2
[tip] =>
)
)
)
My code:
function get_listing_cfs() {
global $wpdb;
$serialized=$wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='va_form'");
$array=unserialize($serialized);
echo '<pre>'.print_r($array, true).'</pre>';
$source = array_filter($array, function($el) {
return !(
$el['label'] == 'Exclude' ||
$el['label'] == 'Exclude 2' ||
!isset($el['label']) ||
empty($el['value']) ||
!isset($el['value'])
);
});
echo '<pre>'.print_r($source, true).'</pre>';
}
So I am trying to filter out the last 2 entries within the array and also filter out any entries that have an empty label or an empty value. I am doing this within a function that i want to use in my wordpress installation. Who can help me out?
Array_filter loops over the old array and returns only the results that will return true.
Your single element will look like this:
Array
(
[id] => app_i-have
[type] => checkbox
[props] => Array
(
[required] => 0
[label] => I have
[tip] =>
[options] => Array
(
[0] => Array
(
[baseline] => 0
[value] => mobile studio
)
[1] => Array
(
[baseline] => 0
[value] => makeup artist
)
)
)
)
Which means that instead of $el['label'], you need to do $el['props']['label'].
//Looping internal options array
foreach ($el['props']['options'] as $sub){
if (empty($sub['value'])) //save the return value?
}
I'm have really lame question about PHP arrays, I'm trying to get all values of form[extras][], in this case they are 1,2,3,4, here is the output of print_r($array):
Array
(
[0] => Array
(
[name] => form[pickupDate][day]
[value] => 1
)
[1] => Array
(
[name] => form[pickupDate][month]
[value] => 10
)
[2] => Array
(
[name] => form[pickupTime][hour]
[value] => 0
)
[3] => Array
(
[name] => form[returnDate][day]
[value] => 1
)
[4] => Array
(
[name] => form[returnDate][month]
[value] => 1
)
[5] => Array
(
[name] => form[returnTime][hour]
[value] => 0
)
[6] => Array
(
[name] => form[car]
[value] => 1
)
[7] => Array
(
[name] => form[pickupAddress]
[value] =>
)
[8] => Array
(
[name] => form[agency]
[value] => 1
)
[9] => Array
(
[name] => form[extras][]
[value] => 1
)
[10] => Array
(
[name] => form[extras][]
[value] => 2
)
[11] => Array
(
[name] => form[extras][]
[value] => 3
)
[12] => Array
(
[name] => form[extras][]
[value] => 4
)
[13] => Array
(
[name] => form[specialPrice]
[value] =>
)
)
You can play with my data, by using this JSON string after converting it to PHP array like this:
$request = '[{"name":"form[pickupDate][day]","value":"1"},{"name":"form[pickupDate][month]","value":"8"},{"name":"form[pickupTime][hour]","value":"0"},{"name":"form[returnDate][day]","value":"1"},{"name":"form[returnDate][month]","value":"1"},{"name":"form[returnTime][hour]","value":"0"},{"name":"form[pickupAddress]","value":""},{"name":"form[agency]","value":"1"},{"name":"form[extras][]","value":"1"},{"name":"form[extras][]","value":"2"},{"name":"form[extras][]","value":"3"},{"name":"form[extras][]","value":"4"},{"name":"form[specialPrice]","value":""}]';
$array = json_decode($request,true);
I already tried with this, but it's resulting string(1) "4":
$result = array_column($array, null, 'name')['form[extras][]']['value'];
var_dump($result);
You have a multidimensional array, this is an array composed by several arrays.
array->[0]-[1]-[2]...
| |
| [name]-[value]
|
array-> [name]-[value]
That's why you have to go through each element, for instance with this function (you have one already defined in PHP > 5.5.0):
function array_column($array, $value)
{
$result = array();
foreach($array as $element)
{
$result[] = $element[$value];
}
return $result[];
}
Edit:
for getting the values of a subset of specific arrays based upon their name:
function getValuesOfTheArraysForExtras($array)
{
$result = array();
foreach($array as $element)
{
if($element['name']=='form[extras][]')
$result[] = $element['value'];
}
return $result;
}
I have one array in two format. I want to change array from
Array
(
[step_number] => 4
[app_id] => Array
(
[0] => 2
[1] => 3
)
[formdata] => Array
(
[0] => Array
(
[name] => app_id[]
[value] => 2
)
[1] => Array
(
[name] => app_id[]
[value] => 3
)
[2] => Array
(
[name] => fieldval[2][2][]
[value] => 1
)
[3] => Array
(
[name] => fieldval[3][3][]
[value] => 200
)
[4] => Array
(
[name] => fieldval[3][3][]
[value] => day
)
[5] => Array
(
[name] => title
[value] => new plan
)
[6] => Array
(
[name] => feature_plan
[value] => 3
)
[7] => Array
(
[name] => plan_type
[value] => free
)
[8] => Array
(
[name] => price
[value] =>
)
[9] => Array
(
[name] => sell_type
[value] => us
)
)
)
this format to
Array
(
[app_id] => Array
(
[0] => 2
[1] => 3
)
[fieldval] => Array
(
[2] => Array
(
[2] => Array
(
[0] => 1
)
)
[3] => Array
(
[3] => Array
(
[0] => 200
[1] => day
)
)
)
[title] => new plan
[feature_plan] => 3
[plan_type] => free
[price] =>
[sell_type] => us
)
these are are one array into two format. i have data in to first array format and i want to change that format to second array type format.
please tell me how i am trying this for 2 days but not succeed.
Here is a function you could use to produce that conversion:
function convert_formdata($input) {
$output = array();
foreach($input['formdata'] as $data) {
$keys = preg_split("#[\[\]]+#", $data['name']);
$value = $data['value'];
$target = &$output;
foreach($keys as $key) {
// Get index for "[]" reference
if ($key == '') $key = count($target);
// Create the key in the parent array if not there yet
if (!isset($target[$key])) $target[$key] = array();
// Move pointer one level down the hierarchy
$target = &$target[$key];
}
// Write the value at the pointer location
$target = $value;
}
return $output;
}
You would call it like this:
$output = convert_formdata($input);
See it run on eval.in for the given input. The output is:
array (
'app_id' =>
array (
0 => 2,
1 => 3,
),
'fieldval' =>
array (
2 =>
array (
2 =>
array (
0 => 1,
),
),
3 =>
array (
3 =>
array (
0 => 200,
1 => 'day',
),
),
),
'title' => 'new plan,',
'feature_plan' => 3,
'plan_type' => 'free',
'price' => NULL,
'sell_type' => 'us',
)
I'm trying to proceed datas that I get with PDO & fetchAll(PDO::FETCH_ASSOC).
Now when my function should return 2 results, only 1 is actually returned.
$tracksinpl is like a collection of array with a 'TrackId' column :
in my example I use :
Array
(
[0] => Array
(
[TrackId] => 4
)
[1] => Array
(
[TrackId] => 5
)
)
$tracks is like a collection of array with an 'id' column
in my example I use :
Array
(
[0] => Array
(
[id] => 1
[title] => a
)
[1] => Array
(
[id] => 2
[title] => abc
)
[2] => Array
(
[id] => 3
[title] => test
)
[3] => Array
(
[id] => 5
[title] => this is title
)
[4] => Array
(
[id] => 6
[title] => title1
)
[5] => Array
(
[id] => 7
[title] => title2
)
)
now the code that I use to proceed datas
$count=0
foreach($tracksinpl as $trinpl)
{
foreach($tracks as $track)
{
if($track['id'] == $trinpl['TrackId'])
{
$count= $count +1;
}
}
}
Now when count is returned, its value is 1 and should be 2, why ?
Thanks in advance !
This is my array:
Array
(
[0] => Array
(
[entity_id] => 1
[value] => new
[label] => New
)
[1] => Array
(
[entity_id] => 3
[value] => pending_payment
[label] => Pending Payment
)
[2] => Array
(
[entity_id] => 4
[value] => pending_paypal
[label] => Pending Paypal
)
[3] => Array
(
[entity_id] => 5
[value] => processing
[label] => Processing
)
[4] => Array
(
[entity_id] => 6
[value] => complete
[label] => Complete
)
[5] => Array
(
[entity_id] => 7
[value] => canceled
[label] => Canceled
)
[6] => Array
(
[entity_id] => 8
[value] => closed
[label] => Closed
)
[7] => Array
(
[entity_id] => 9
[value] => holded
[label] => Holded
)
[8] => Array
(
[entity_id] => 10
[value] => payment_review
[label] => Payment Review
)
[9] => Array
(
[entity_id] => 11
[value] => new
[label] => New
)
[10] => Array
(
[entity_id] => 13
[value] => pending_payment
[label] => Pending Payment
)
UPDATE This is the result of the print_r.
As you can see the array[0] with array[6] are the same. Also the array[7] with the array[1]. How can I get rid of one of them ? thx
I tried smth like this:
$input = $my_array
$temp = $input;
foreach ( $temp as &$data ) {
unset($data['id']);
}
$output = array_intersect_key($input, array_unique($temp));
but with no any result :( .
$result = array();
foreach ($myArray as $array) {
if (isset($result[$array['value']])) {
continue;
}
$result[$array['value']] = $array;
}
print_r($result);
Hey Attila this should help.
Your $my_array is renamed in this example into $wholeArray. And I thought it woulf be enough to compare the labels:
$index=0;
$length = count($wholeArray);
foreach($wholeArray as $arrayElement){
$temp = $arrayElement['label'];
$index++;
for($i = $index; $i < $length; $i++){
if($wholeArray[$i]['label'] == $temp){
unset($wholeArray[$i]);
$length --;
}
}
}
If you have any questions to the code feel free to ask :)