This question already has answers here:
Deleting an element from an array in PHP
(25 answers)
Closed 5 years ago.
How to change or update array value in PHP through array number?
Example:
$array1 = array("cat","dog","mouse","dog");
I want to change the value of dog in the 2nd array only
Use Variable variables:
$a = 'array' . '1';
$$a[1] = 'doggg'; #value changed
$b = 'array' . '2';
$$b[1] = 'newDogg'; #value changed
if you want to remove 2 index then
$array1 = array('dog','mouse','cat','mouse');
unset($array1[1]);
print_r($array1);
Output
Array ( [0] => dog [2] => cat [3] => mouse )
//if you are looking for remvoing duplicate value then you can use array_unique
$result=array_unique($array1);
print_r($result);
Output
Array ( [0] => dog [1] => mouse [2] => cat )
If you mean you have to remove an element from an array, probably this will help.
array_splice( $array1, OFFSET, 1 );
replace OFFSET with the index you want to remove.
Edit :-
To delete element use unset(arrayVar[key])
For eg :-
unset($array1[1]) - will delete the "mouse" at key/index 1 if you wish to delete 2nd occurance then use unset($array1[3])
Original :-
Explain your problem in more detail please !
What do you mean by 2nd array and where exactly is it ?
Do you mean to edit the 2nd occurance of "dog" in array ?
If so then it would be $array1[3]="someNewValue"
Related
This question already has answers here:
How to rearrange the array element from 0th index in the sequence to end?
(1 answer)
How to reindex an array?
(6 answers)
Closed 2 years ago.
I have a session variable like this:
$list = Session::get('list_id')
the $list variable value has a value of Array ( [0] => A, [1] => B,[2] => C, [4] => D)
when I am using unset($list [0]);
I am getting a value of Array ([1] => B,[2] => C, [4] => D)
The problem that I am getting is all of my parameters has a parameters of $list[0] I need the index 0 always, so that value that I am looking for after I remove the index 0 is Array ([0] => B,[1] => C, [2] => D)
so that I can store it again on my session.
is there a way to do this?
Use array_values function if you are sure your array has only numerical index keys.
See this link for more details; https://www.php.net/manual/en/function.array-values.php
Do not use unset.
array_shift — Shift an element off the beginning of array
$removedElement = array_shift($yourArray);
array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down.
This question already has answers here:
Move array item with certain key to the first position in an array, PHP
(9 answers)
move array element to the first position but remain order
(4 answers)
Move an array element to a new index in PHP
(9 answers)
Move Value in PHP Array to the Beginning of the Array
(12 answers)
Move array element with a particular value to top of array
(2 answers)
Closed 2 months ago.
How to sort an array at PHP to force selected row as a first ?
My array is
array[]=array(id=>'a', content=>'lemon');
array[]=array(id=>'b', content=>'apple');
array[]=array(id=>'c', content=>'banana');
array[]=array(id=>'d', content=>'cherry');
How to sort the array to force
array[]=array(id=>'b', content=>'apple');
as a first row and doesn't matter the rest (apple is the key).
And in other example turn sort to get
array[]=array(id=>'d', content=>'cherry');
as a first row and doesn't matter the rest (cherry is the key).
Another way to do this is to effectively rotate the array using array_slice, bringing the element you want to the start:
$first = 'apple';
$k = array_search($first, array_column($array, 'content'));
$array = array_merge(array_slice($array, $k), array_slice($array, 0, $k));
print_r($array);
Output:
Array (
[0] => Array ( [id] => b [content] => apple )
[1] => Array ( [id] => c [content] => banana )
[2] => Array ( [id] => d [content] => cherry )
[3] => Array ( [id] => a [content] => lemon )
)
Demo on 3v4l.org
There are 2 ways I can think of doing this. The first is as Ultimater in the comments suggest to extract the matching row, then sort and then add the row back in...
$first = 'apple';
$array = [];
$array[]=array('id'=>'a', 'content'=>'lemon');
$array[]=array('id'=>'b', 'content'=>'apple');
$array[]=array('id'=>'c', 'content'=>'banana');
$array[]=array('id'=>'d', 'content'=>'chery');
$firstElement = array_search($first, array_column($array, "content"));
$row = $array[$firstElement];
unset($array[$firstElement]);
sort($array);
array_unshift($array, $row);
print_r($array);
The second is to use usort and add specific clauses in that if the key matches the row you want first, then it will always force it to the first row...
$first = 'apple';
usort($array, function ($a, $b) use ($first){
if ( $a['content'] == $first) {
return -1;
}
if ( $b['content'] == $first) {
return 1;
}
return $a <=> $b;
});
print_r($array);
(I've used <=> in this which is PHP 7+, there are alternatives if you need to use PHP 5).
If as your comment suggests that there is no need to sort the rest of the data, then the first set of code minus the sort() should do.
One other option - if id:content is one to one, we can index the array by content and merge with an array with a single empty "apple" key (or whichever content value you're looking for).
$array = array_merge(['apple' => []], array_column($array, null, 'content'));
If the resulting string keys are undesirable the array can be reindexed with array_values.
If the array only contains id and content and id:content is in fact one to one, a "dictionary" of key-value pairs will be handier to deal with than a list of rows like this and it would probably be better to set the array up that way to begin with if possible.
If id:content is not one to one, then... never mind. ;-)
This question already has answers here:
PHP - Accessing Multidimensional Array Values
(4 answers)
Closed 8 years ago.
I've got this url:
http://web.com/script.php?identifiers%5Bmc%5D%5Bnick%5D=name1&identifiers%5Bcs%5D%5Bnick%5D=name2&identifiers%5Bcs%5D%5Bpassword%5D=mypass
so i will get array like this:
[identifiers] => Array
(
[mc] => Array
(
[nick] => name1
)
[cs] => Array
(
[nick] => name2
[password] => mypass
)
)
How do I take value name1 and put into variable $mc_name?
That's a simple array containing another array so you can simply specify multiple indexes for included array:
$mc_name = $_GET['identifiers']['mc']['nick'];
To better understand how it works think of it like assigning each array first to a variable like:
$identifiers = $_GET['identifiers'];
$mc_array = $identifiers['mc'];
$mc_name = $mc_array['nick'];
which will essentially do the same thing at once, without the need to specify multiple variables and arrays.
Start with:
identifiers = $_GET['identifiers']
If you know the key names, then simply:
$mc_name = $identifiers['mc']['nick']
If you know it's the first value or the first value, then you can:
$mc_name = array_shift($identifiers); // get the 'mc' array
$mc_name = array_shift($identifiers); // get the 'nick' value
Not that array_shift will actually remove the elements from the original array.
This question already has answers here:
Get the first element of an array
(39 answers)
multidimensional array
(3 answers)
Closed 9 years ago.
I have this:
Array
(
[28] => Array
(
[name] => HTC Touch HD
)
)
There's only one array inside the main array and I only the value of name. Problem is that I don't know the index (28).
You could use array_values just in general to get rid of any weird keys:
$normal = array_values($arr);
$normal[0]['name']
Or in this particular case, end, which is only a little bit hacky:
end($normal)['name']
http://codepad.viper-7.com/cApBjK
(Yep, reset and first and such work too.)
You could also just use
$array = array_pop($array);
And then to get the name element:
$array['name']
You can try something like this:
reset($outerArray);
$innerArray = current($outerArray);
Now you should have access to the value you want.
Pretty self-explanatory :)
<?php
$array = array(
28 => array(
'name' => 'HTC Touch HD'
)
);
$key = current(array_keys($array));
echo '<pre>';
print_r($array[$key]);
echo '</pre>';
?>
If you don't know the structure of an array, you can use foreach construct.
This question already has answers here:
Get first key in a (possibly) associative array?
(25 answers)
Closed 5 years ago.
hi i have a multidimensional l array .
Array
(
[1] => Array
(
[38] => Fashion Retail | Fashion Accessories
)
[10] => Array
(
[194] => Automotive | 4x4
[206] => Automotive | Aftermarket Parts and Kits
[201] => Automotive | ATVs
)
)
i want to get the first sub array's key , in this scenario it is 1 , i can get it using a foreach loop .
foreach($myarry as $key=>$val)
is there any way to achieve this with out looping , please help . thanks in advance
If using >= PHP 5.5...
$first = array_keys($myarry)[0];
If using an older PHP, just assign the keys somewhere, and then subscript the first element as normal.
$arrKeys = array_keys($array);
$key = array_shift($arrKeys); // gives first key
Returns the first key and deletes this from the arrKeys, hence the next key, 10 in this case will be returned on next call. No need to make another array.
yep, I did that , I have used
current(array_keys($my_array))
print_r($myarry[array_keys($myarry)[0]]);
Please try:
reset($myarry);
$first_key = key($myarry);