Using PHP I'm trying to remove an element from an array based on the value of the element.
For example with the following array:
Array
(
[671] => Array
(
[0] => 1
[1] => 100
[2] => 1000
)
[900] => Array
(
[0] => 15
[1] => 88
}
)
I'd like to be able to specify a value of on of the inner arrays to remove. For example if I specified 100 the resulting array would look like:
Array
(
[671] => Array
(
[0] => 1
[2] => 1000
)
[900] => Array
(
[0] => 15
[1] => 88
}
)
My first thought was to loop through the array using foreach and unset the "offending" value when I found it, but that doesn't seem to reference the original array, just the loop variables that were created.
Thanks.
foreach($array as $id => $data){
foreach($data as $index => $offending_val){
if($offending_val === 100){
unset($array[$id][$index]);
}
}
}
You can use:
array_walk($your_array, function(&$sub, $key, $remove_value) {
$sub = array_diff($sub, array($remove_value));
}, 100);
Couple of ideas:
You could try array_filter, passing in a callback function that returns false if the value is the one you want to unset. Using your example above:
$new_inner_array = array_filter($inner_array, $callback_that_returns_false_if_value_100)
If you want to do something more elaborate, you could explore the ArrayIterator class of the SPL, specifically the offsetUnset() method.
Related
Array
(
[0] => Array
(
[0] => 17
[1] => 111
)
[1] => Array
(
[0] => 17
[1] => 10
)
[2] => Array
(
[0] => 20
[1] => 111
)
)
I want to convert this array as:
array
(
[17]=>121
[20]=>111
)
is there any php array function which can do it easily. I know the other way, but want to know if any ready made function can do that or not??
Please Help.
Here I actually wanted to convert
[0] => Array
(
[0] => 17
[1] => 111
)
17 to key and take 111 as value then in next array
[1] => Array
(
[0] => 17
[1] => 10
)
do the same thing get first value 17 as key and add 10 into previous value 111
which is 121 and then
[2] => Array
(
[0] => 20
[1] => 111
)
take 20 as key and assign value 111 to that key so basically, I want first value as a key and second value as value and for all same keys I want to add values as I stated before.
I thought there might be any PHP ready-made function for that as I have seen there are lots of array processing functions available in PHP. Now, I realized there is no any such kind of function available. I can do my own custom code for this purpose but was looking for good logical solution.
No builtin function for that but it is a simple foreach loop. Assume your array is stored in variable $arr;
$return = array();
foreach ($arr as $a) {
$return[$a[0]] += $a[1];
}
echo "<pre>"; print_r($return);
if you are calling multiple times then you can easily write your own function
$arr[0]= array(17,111);
$arr[1]= array(17,10);
$arr[2]= array(20,111);
$return = subArr($arr);
echo "<pre>"; print_r($return);
function subArr($arr) {
$result = array();
foreach ($arr as $a) {
$result[$a[0]] += $a[1];
}
return $result;
}
The output I have is this:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 26
)
My current code to try and update is:
updateProduct($product->id, array('categories[108]'));
When I run this function, it overwrites the existing array, giving me an output like this:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 108
)
What I am trying to achieve would look like this:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 26
[1] => 108
)
But.... in the event that 108 already exists, I would hope that it would just ignore it. I don't want:
[price_hidden_label] => 1
[categories] => Array
(
[0] => 26
[1] => 108
[2] => 108
)
Any idea how I do that?
You can push elements to the array using
$arr[] = $new_var;
and check if a value already exists by
if(in_array($new_var, $arr))
In example (trying to follow your input-output):
$categories = array();
$categories[] = 26;
$input_value = 108;
if(!in_array($input_value, $categories)) {
$categories[] = $input_value;
}
edit. in functions (my PHP is bit rusty, so there might be small tweaks that need to be done but you get the basic idea)
function updateProduct($arr, $input) {
if(!in_array($input, $arr)) {
$arr[] = $input;
}
}
and then call it as
updateProduct($categories, $input_value);
You can use array_push to add items to an array, and you can use in_array to check if it already exists, or you can use array_unique to remove duplicates after your done the rest of your processing too.
My input array :
Array
(
[0] => Array
(
[id] => 1
[status_name] => Released
)
[1] => Array
(
[id] => 2
[status_name] => Under Construction
)
)
I want the output result :
Array (
[1] => Released
[2] => Under Construction
)
USe sub array id as output array key value and status_name as value array.
This is built into php as array_column. You would have:
$status_names = array_column($data, 'status_name', 'id');
print_r($status_name);
Bonus points on question as I had no idea this existed until looking for an answer for you.
Try the following:
function reOrderArray($input_array)
{
$result = array();
foreach ($input_array as $sub_array)
{
$result[$sub_array['id']] = $sub_array['status_name'];
}
return $result;
}
There might be a built-in php function to do this, array functions in php are quite powerful. I am, however, woefully unaware of one.
I was using this code, but If I used array_push() it was inserting values with null, I was using array_push to enter values in the array
foreach ($_POST['record_num'] as $check_rec_num) {
if(!in_array($check_rec_num, $_SESSION['selected_record'][$pageno])) {
array_push($_SESSION['selected_record'][$pageno][], $check_rec_num);
}
}
but when I used this it was automatically adding values in the array, without using array_push why is that so?
foreach ($_POST['rec_num'] as $check_rec_num) {
if(!in_array($check_rec_num, $_SESSION['selected_record'][$pageno])) {
$_SESSION['selected_record'][$pageno][] = $check_rec_num;
}
}
1st example
Array ( [1] => Array ( [0] => 36 [1] => 35 ) [2] => )
2nd example (without bar brackets)
Array ( [1] => Array ( [0] => 36 [1] => 35 [2] => 34 ) [2] => Array ( [0] => ) )
Array Design 3rd example without using array_push how the hell it is adding the values automatically at the end of the array without array_push?
Array (
[1] => Array (
[0] => 36
[1] => 35
)
[2] => Array (
[0] => 33
[1] => 32
)
)
You have an extra [] in
array_push($_SESSION['selected_record'][$pageno][], $check_rec_num);
This will do it:
$_SESSION['selected_record'][$pageno] = array();
array_push($_SESSION['selected_record'][$pageno], $check_rec_num);
See the manual on array_push.
Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.
Yet, you should better use $_SESSION['selected_record'][$pageno][] since
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
That's because of the extra []
Remove it and it'll work:
array_push($_SESSION['selected_record'][$pageno], $check_rec_num);
This question already has answers here:
How to re-index all subarray elements of a multidimensional array?
(6 answers)
Closed 2 years ago.
I am trying to filter and reindex this array. My original array is $_SESSION['ShowingRequests'].
I've tried
array_values(array_filter($_SESSION['ShowingRequests']))
and
array_values(array_filter($_SESSION['ShowingRequests']['ListingKey']))
array_values(array_filter($_SESSION['ShowingRequests']['Key']))
but it won't reach the second level of the array.
I want it to go from this
Array
(
[ListingKey] => Array
(
[1] => 97826889139
[2] => 97820967049
[4] => 97825243774
[5] => 97824864611
)
[Key] => Array
(
[1] => 2
[2] => 3
[4] => 5
[5] => 6
)
)
to this
Array
(
[ListingKey] => Array
(
[0] => 97826889139
[1] => 97820967049
[2] => 97825243774
[3] => 97824864611
)
[Key] => Array
(
[0] => 2
[1] => 3
[2] => 5
[3] => 6
)
)
PHP arrays are not indexed, because they are not real arrays. They are in fact ordered hashmaps and as such you should not really care about the keys here. Iterating over these arrays is trivial and does not require using array_values at all.
foreach ($_SESSION['ShowingRequests']['ListingKey'] as $key => $value) {
echo "$key => $value\n";
}
Would give you...
1 => 97826889139
2 => 97820967049
4 => 97825243774
5 => 97824864611
Where you get the name of the key and the value for each element in the array using the foreach construct.
In any case you have to remember that both array_values and array_filter are non-destructive functions. They return a new array. They do not modify the array by reference. As such you must assign the return value if you want to modify the existing array. They also do not work recursively.
$_SESSION['ShowingRequests']['ListingKey'] = array_values(array_filter($_SESSION['ShowingRequests']['ListingKey']));
$_SESSION['ShowingRequests']['Key'] = array_values(array_filter($_SESSION['ShowingRequests']['Key']));
$_SESSION['ShowingRequests'] = array_values(array_filter($_SESSION['ShowingRequests']));
Is your issue to assign the filtered values back to the same key?
foreach (['ListingKey', 'Key'] as $key)
{
$_SESSION['ShowingRequests'][$key] = array_values(
array_filter($_SESSION['ShowingRequests'][$key])
);
}
Here some demonstration how it works with a helper function just to make that more visible
function array_filter_values($var) {
return array_values(array_filter($var));
}
foreach (['ListingKey', 'Key'] as $key)
{
$_SESSION['ShowingRequests'][$key] =
array_filter_values($_SESSION['ShowingRequests'][$key])
;
}
Here is an example without that helper function but with an alias of which I had thought it might make that more easy to understand but apparently not:
foreach (['ListingKey', 'Key'] as $key)
{
$var = &$_SESSION['ShowingRequests'][$key];
$var = array_values(array_filter($var));
unset($var);
}
This example code is using an alias to the variable you want to change (here the part of the array you want to change). This probably makes it more visible how it works. Because it is an alias, unset is used to remove the alias, it should not be re-used in the next iteration or after the iteration has finished.