Remove, add, move array indexes - php

I have a string that I converted to a multi-dimensional array.
String: 13,4,3|65,1,1|27,3,2
I wanna be able to move 27,3,2 to index 1 for example, so it would become:
13,4,3|27,3,2|65,1,1
Or remove one of those sections.
I know I can unset(), but I'm not sure how to search for an index then move it or unset it.

You can try the below one for interchanging the position of last two elements
$array = [0 => array(13,4,3), 1=>array(65,1,1), 2 => array(27,3,2)];
foreach($array as $key => $value) {
if($key == count($array)-1) {
$array[$key] = $array[$key-1];
$array[$key-1] = $value;
}
}
This is for removing the second element.
$array = [0 => array(13,4,3), 1=>array(65,1,1), 2 => array(27,3,2)];
foreach($array as $key => $value) {
if($key == count($array)-1) {
$array[$key-1] = $value;
unset($array[$key]);
}
}

Loop through the array using foreach
foreach($array as $key => $value)
From key you can get the key and can do whatever you like.
Other wise, you can do this if you know the key
echo $array['pass_key_name_here'];

Related

Key of associative table

I have a little problem in my project : I would like display only the key of each array when I click on a element of my list. Dump of my table
For example when I click in "Paupiette", I would like display 0.
I partially succeeded but when I click in a element, I see only the last key (here "1").
Here,you can see the code
$carts = $cartService->getCart($user);
//dump($carts); die;
$mykey = 0;
foreach($carts as $key => $value) {
$mykey = $key;
}
dump($mykey);die;
If you have any idea, thank you a lot
You have multiple choice to display your $keys
the first one is :
$keys = array_keys($carts);
The second is :
foreach ($carts as $key => $value) {
dump($key);
}
if you will display all keys after loop
$myKeys = array();
foreach($carts as $key => $value) {
$myKeys[] = $key;
}
dump($myKeys);die;
If you just want to display the keys, following your coding "style":
foreach ($carts as $key => $value) {
dump($key);
}
If you want to display the keys as comma-separated string, again following your coding "style":
$keys = array_keys($carts); // Get the keys, eg, [0, 1];
$str = implode(', ', $keys); // Convert the array to string, eg, "0, 1"
dump($str);

Easy way to get comma seperate value from the array using PHP

I have this variable $sid and its output is below.
$sid = $_POST['sid'];
// sid consists values like this.
Array
(
[0] => 1
[1] => 2,3
)
now I need to run a query with this sid 1, 2 and 3. To do that I am using following code:
$ex = array();
foreach ( $sid as $key => $value) {
$ex[] = explode(',', $value);
}
foreach ($ex as $key => $value) {
foreach ($value as $k => $v) {
echo $v;
// my query where sid = $v
}
}
Is there any better way without multiple foreach loop?
Try this:
$ex = explode(',', implode(',', $sid));
foreach ($ex as $v) {
echo $v;
}
Basically, your input is an array of strings of comma-separated values, so, you can merge these strings into single string with comma as separator, and then split whole thing using single explode call.

Is there any special rule to use foreach and while loop(using each() function) to iterate over an array in php?

I am new in PHP.
My question is that when i use following script:
$arr1 = array('fname' => 'niraj','lname' => 'kaushal','city' => 'lucknow');
while(list($key, $value) = each($arr1)){
echo "$key has $value value <br>";
}
foreach($arr1 as $key => $value){
echo "$key:$value <br>";
}
it outputs
fname has niraj value
lname has kaushal value
city has lucknow value
fname:niraj
lname:kaushal
city:lucknow
but when i change order of foreach and while loop as follow
$arr1 = array('fname' => 'niraj','lname' => 'kaushal','city' => 'lucknow');
foreach($arr1 as $key => $value){
echo "$key:$value <br>";
}
while(list($key, $value) = each($arr1)){
echo "$key has $value value <br>";
}
it gives following output
fname:niraj
lname:kaushal
city:lucknow
Why second script doesn't display the output of while loop. What the reason behind it.
It is because each() only returns the current key/value and then advances the internal counter (where in the array you are currently). It does not reset it.
The first loop (foreach) sets the internal counter to the end of the array, so the second loop thinks it is already done and therefore does nothing.
You need to call reset() on the array before starting the loop using each():
reset($arr1);
while (list($key, $value) = each($arr1)){
echo "$key has $value value <br>";
}

Convert a string to an array in PHP

I have an array like such:
array('some_key' => 'some_value');
I would like to take that and transform it to, this should be done programatically
array('some_key' => array('some_value'));
This should be rather simple to do but the only think I can find is answers on string split and explode, to explode strings into arrays. I thought PHP, like other languages, had something called to_array or toArray.
I am assuming this is super easy to do?
If you're just doing the one array element, it's as simple as:
$newarray['some_key'] = array($sourcearray['some_key']);
Otherwise if your source array will have multiple entries, you can do it in a loop:
foreach($sourcearray AS $key => $value) {
$newarray[$key] = array($value);
}
Something as simple as $value = array($value) should work:
foreach ($array as &$value) {
$value = array($value);
}
unset($value); //Remove the reference to the array value
If you prefer to do it without references:
foreach ($array as $key => $value) {
$array[$key] = array($value);
}
You can try like this
<?php
$arr=array('some_key' => 'some_value');
foreach($arr as $k=>$v)
{
$newarr[$k] = array($v);
}
var_dump($newarr);

Unset array element inside a foreach loop

So here is my code:
<?php
$arr = array(array(2 => 5),
array(3 => 4),
array(7 => 10));
foreach ($arr as $v) {
$k = key($v);
if ($k > 5) {
// unset this element from $arr array
}
}
print_r($arr);
// now I would like to get the array without array(7 => 10) member
As you can see, I start with an array of single key => value arrays, I loop through this array and get a key of the current element (which is a single item array).
I need to unset elements of the array with key higher than 5, how could I do that? I might also need to remove elements with value less than 50 or any other condition. Basically I need to be able to get a key of the current array item which is itself an array with a single item.
foreach($arr as $k => $v) {
if(key($v) > 5) {
unset($arr[$k]);
}
}
It is safe in PHP to remove elements from an array while iterating over it using foreach loop:
foreach ($arr as $key => $value) {
if (key($value) > 5) {
unset($arr[$key]);
}
}
Use key() to get the first key from the sub-array.
foreach($arr as $k => $v) {
if(key($v) > 5) {
unset($arr[$k]);
}
}
It's not really safe to add or delete from a collection while iterating through it. How about adding the elements you want to a second array, then dumping the original?
To unset array element we Used unset() and php function like below:
foreach($array as $key=>$value)
{
if(key($value) > 5)
{
unset($array[$key]);
}
}

Categories