This question already has answers here:
Better way to unset multiple array elements
(6 answers)
Closed 4 years ago.
I have an array of this form :
Array
(
[1] => A
[2] => B
[3] => C
)
I want to delete values where key = 1 and 2...
I have tried this
unset($array['1']);
unset($array['2']);
But I need to call 2 times unset... It's possible without 2 calls ?
Thanks, have a good day!
You can try like
$keyToRemove = array('1', '2');
foreach($keyToRemove as $key) {
unset($arr[$key]);
}
Also you can do it like this way too
$arr = array_diff_key($arr, array_flip($keyToRemove));
Similar answer you can check it here
Related
This question already has answers here:
split a string into two based on a substring with all combinations
(2 answers)
Closed 7 months ago.
hello everyone i am getting value like Array ( [2] => FAB(CBY-DJ9)--9 ), i want two separate value like this FAB(CBY-DJ9) and 9 by loop, kindly help me i am a beginner.
hello everyone i am getting value like Array ( [2] => FAB(CBY-DJ9)--9 ), i want two separate value like this FAB(CBY-DJ9) and 9 by loop, kindly help me i am a beginner.hello everyone i am getting value like Array ( [2] => FAB(CBY-DJ9)--9 ), i want two separate value like this FAB(CBY-DJ9) and 9 by loop, kindly help me i am a beginner
With explode() you can seperate them.
$item = 'FAB(CBY-DJ9)--9';
list($code, $number) = explode('--', $item);
This question already has answers here:
How can I add all of my array values together in PHP?
(4 answers)
Closed 3 years ago.
I have an array and I want to sum up the values of the array such as [0][1][2] and assign the result of the sum to a variable
My array
Array ( [0] => 1 [1] => 2 [2] => 0 )
Can anyone provide me a solution that would be really helpful?
Use php array_sum() function.
$val = array_sum($yourArray);
It will sum up all of your array elements into a variable.
This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I know this should be a relatively simple thing but I have not been able to do it yet. I have look hi and low and every example I try it fails I am sure it is fairly simple
Here is my array. I need to get the value of name last and filenames
Any help would be most appreciated.
Thanks!!
Array
(
[formData] => Array
(
[name] => TEST
[last] => TEST1
[filenames] => Array
(
[0] => /ocdata/uploads/export-1511887767.csv
)
)
)
Really simple method to see your content:
foreach($array as $k => $v)
{
echo $k . $v . PHP_EOL; // see content of your array
}
Or use directly the values:
$array['formData']['name'];
$array['formData']['last'];
$array['formData']['filenames'];
This question already has answers here:
How to combine two arrays together?
(3 answers)
Closed 6 years ago.
I am having two arrays like this
Array
(
[0] => username
[1] => avatar
)
Array
(
[0] => name
[1] => 4.jpg
)
Here, I need to get these values in the following format
'username'=>name,'avatar'=>4.jpg
i.e., merge the same key values in the above format..
How should I do this,..Someone help me..
If you think that my title is wrong,Please change it into correct format.
Thanks,
Use array_combine()
$final_array = array_combine($fist_array,$second_array);
Reference:- http://php.net/manual/en/function.array-combine.php
use $c = array_combine($a, $b);
This question already has answers here:
How to find the foreach index?
(14 answers)
Closed 8 years ago.
So I may just be completely overcomplicating this for myself but I am trying to get the index of an array in a multidimensional array.
Hopefully showing it makes more sense.
Array
(
[1234] => Array
(
[Name] => Test
)
[5435] => Array
(
[Name] => Test
)
)
I have a large array with different numbers as the index and I need to do a foreach through them, but I need that index number. (1234,5435)
Is there any easy way of doing this?
Yes there is.
foreach ($arr as $key=>$val)
{
//do stuff
}
The $key is the key you need