Resetting position of items in array PHP - php

I have a string of 7 numbers in an array looks like 4,1,2,56,7,9,10 however sometimes these elements are empty ,,,56,7,9,10 for example. What I would like to do is reorder the array so it looks like 56,7,9,10,,,

try this:
$null_counter = 0;
foreach($array as $key => $val) {
if($val == null) {
$null_counter++;
unset($array[$key]);
}
}
for($x=1;$x<=$null_counter;$x++) {
$array[] = null;
}

Use unset in loop to remove null value and shift the values Up.
foreach($yourarray as $key=>$val )
{
if($yourarray[$key] == '')
{
unset($yourarray[$key]);
}
}

Related

remove value from array what the words is not fully match

How i can remove a value from array what is not fully match the letters.
Array code example:
$Array = array(
'Funny',
'funnY',
'Games',
);
How I can unset all values from this array what is 'funny'
I try via unset('funny'); but is not removing the values from array, is removed just if i have 'funny' on array but 'funnY' or 'Funny' not working
Maybe there is some sophisticated solution with array_intersect_key or something which could do this in one line but I assume this approach is more easily read:
function removeCaseInsensitive($array, $toRemove) {
$ret = [];
foreach ($array as $v) {
if (strtolower($v) != strtolower($toRemove))
$ret[] = $v;
}
return $ret;
}
This returns a new array that does not contain any case of $toRemove. If you want to keep the keys than you can do this:
function removeCaseInsensitive($array, $toRemove) {
$keep = [];
foreach ($array as $k => $v) {
if (strtolower($v) != strtolower($toRemove))
$keep[$k] = true;
}
return array_intersect_keys($array, $keep);
}
You can filter out those values with a loose filtering rule:
$array = array_filter($array, function($value) {
return strtolower($value) !== 'funny';
});

php update value in multidimensional array not by reference

I know that this is a similar question:
Updating a Multidimensional Array in PHP
but is different my problem:
I have a multidimensional array and I want to check if a value inside it is equal another, if tue update value.
I have tried in this way:
$room_id = '1205222__1763659__386572_1';
foreach($cart as $c){
if($c['item_type'] == 'hotel'){
foreach($c as $key => $value){
if(substr($key, 0, 7) == 'room_id'){
if($value == $room_id) {
$c[$key] = '';
}
}
}
}
}
This is an example of my array:
array() {
[0]=>
array() {
["item_type"]=> "hotel"
["room_id_0"]=> "1205222__1763659__386572_1"
["room_id_1"]=> "1205222__1763659__386572_2"
},
[1]=>
array() {
["item_type"]=> "hotel"
["room_id_0"]=> "1205222__1763659__386572_3"
["room_id_1"]=> "1205222__1763659__386572_4"
}
}
I have checked if the conditions work right and yes, but when I make the assignment and after print the array again the array isn't changed.
Can someone help me?
Thanks
this won't work as you are changing $c which is a copy of the array $cart. you would need to get the array key of the first array value and the array key of the second array and then update:
try this
foreach($cart as $a => $c){
if($c['item_type'] == 'hotel'){
foreach($c as $key => $value){
if(substr($key, 0, 7) == 'room_id'){
if($value == $room_id) {
$cart[$a][$key] = '';
}
}
}
}
}
You can call foreach with passing $c by reference, like this:
foreach($cart as &$c){
...
This should let you change the value of the item.
To take it a step further, you could also pass $value by reference, and then change its value by modifying $value:
// Notice &$c
foreach($cart as &$c){
if($c['item_type'] == 'hotel'){
// Notice &$value
foreach($c as $key => &$value){
if(substr($key, 0, 7) == 'room_id'){
if($value == $room_id) {
// Instead of $c[$key]
$value = '';
}
}
}
}
}

Function to search associative array keys

I am writing a shopping cart session handler class and I find myself repeating this certain chunk of code which searches a multidimensional associative array for a value match.
foreach($_SESSION['cart'] as $k => $v){
if($v['productID'] == $productID){
$key = $k;
$this->found = true;
}
}
I am repeating this when trying to match different values in the array.
Would there be an easy to to create a method whereby I pass the key to search and the value. (Sounds simple now I read that back but for some reason have had no luck)
Sounds like you want something like this:
function findKey(array $array, $wantedKey, $match) {
foreach ($array as $key => $value){
if ($value[$wantedKey] == $match) {
return $key;
}
}
}
Now you can do:
$key = findKey($_SESSION['cart'], 'productID', $productID);
if ($key === null) {
// no match in the cart
} else {
// there was a match
}

PHP if in array, do something with the value

I am trying to check if a value is in an array. If so, grab that array value and do something with it. How would this be done?
Here's an example of what I'm trying to do:
$the_array = array("buejcxut->10", "jueofi31->20", "nay17dtt->30");
if (in_array('20', $the_array)) {
// If found, assign this value to a string, like $found = 'jueofi31->20'
$found_parts = explode('->', $found);
echo $found_parts['0']; // This would echo "jueofi31"
}
This should do it:
foreach($the_array as $key => $value) {
if(preg_match("#20#", $value)) {
$found_parts = explode('->', $value);
}
echo $found_parts[0];
}
And replace "20" by any value you want.
you might be better off checking it in a foreach loop:
foreach ($the_array as $key => $value) {
if ($value == 20) {
// do something
}
if ($value == 30) {
//do something else
}
}
also you array definitition is strange, did you mean to have:
$the_array = array("buejcxut"=>10, "jueofi31"=>20, "nay17dtt"=>30);
using the array above the $key is the element key (buejcxut, jueofi31, etc) and $value is the value of that element (10, 20, etc).
Here's an example of how you can search the values of arrays with Regular Expressions.
<?php
$the_array = array("buejcxut->10", "jueofi31->20", "nay17dtt->30");
$items = preg_grep('/20$/', $the_array);
if( isset($items[1]) ) {
// If found, assign this value to a string, like $found = 'jueofi31->20'
$found_parts = explode('->', $items[1]);
echo $found_parts['0']; // This would echo "jueofi31"
}
You can see a demo here: http://codepad.org/XClsw0UI
if you want to define an indexed array it should be like this:
$my_array = array("buejcxut"=>10, "jueofi31"=>20, "nay17dtt"=>30);
then you can use in_array
if (in_array("10", $my_array)) {
echo "10 is in the array";
// do something
}

How to delete elements in array?

I have an array like this :
array() {
["AG12345"]=>
array() {
}
["AG12548"]=>
array() {
}
["VP123"]=>
array() {
}
I need to keep only arrays with keys which begin with "VP"
It's possible to do it with one function ?
Yes, just use unset():
foreach ($array as $key=>$value)
{
if(substr($key,0,2)!=="VP")
{
unset($array[$key]);
}
}
From a previous question: How to delete object from array inside foreach loop?
foreach($array as $elementKey => $element) {
if(strpos($elementKey, "VP") == 0){
//delete this particular object from the $array
unset($array[$elementKey]);
}
}
This works for me:
$prefix = 'VP';
for ($i=0; $i <= count($arr); $i++) {
if (strpos($arr[$i], $prefix) !== 0)
unset($arr[$i]);
}
Another alternative (this would be way simpler if it were values instead):
array_intersect_key($arr, array_flip(preg_grep('~^VP~', array_keys($arr))));
This is only a sample how to do this, you have probably many other ways!
// sample array
$alpha = array("AG12345"=>"AG12345", "VP12548"=>"VP12548");
foreach($alpha as $val)
{
$arr2 = str_split($val, 2);
if ($arr2[0] == "VP")
$new_array = array($arr2[0]=>"your_values");
}

Categories