Why PHP lose item value of array? - php

I have the array with values:
$array1 = array('Boss', 'Lentin', 'Endless');
print_r ($array);
The result will be:
Array ( [0] => Boss [1] => Lentin [2] => Endless
It's ok.
But, if I add two elements to this array with a keys, the "Boss" element will be lost.
$array2 = array("1"=>'Doctor','Boss', 2=>'Lynx', 'Lentin', 'Endless');
print_r ($array2);
The result will be:
Array ( [1] => Doctor [2] => Lynx [3] => Lentin [4] => Endless )
//Where is "BOSS"???
Why?

When php create the array, set Doctor in index 1 and Boss in index 2, but 2=>'Lynx' cause php overwrite index 2 and set Lynx in it.
You can set it after setted index or use index for it. For example like
$array2 = array("1"=>'Doctor', 2=>'Lynx', 'Boss', 'Lentin', 'Endless');
// or
$array2 = array("1"=>'Doctor', 2=>'Boss', 3=>'Lynx', 'Lentin', 'Endless');

When $array is being created, 'Boss' will first be stored in index 2 (Array([2] =>'Boss') which is later overwritten by 'Lynx'

Your issue is index keys
$array2 = array("1"=>'Doctor','Boss', 2=>'Lynx', 'Lentin', 'Endless');
print_r ($array2);
This is because, on index 1 it is already doctor, boss will be second, which will be replaced by Lynx which have same index of 2 where boss will be replaced.
I hope I am clear.

This is expected behaviour from php (see http://php.net/manual/en/language.types.array.php#example-57). In case you need all the values in the array and don't need to work with keys, I recommend to use array_push($array $value). Otherwise you should add all the values with their keys and remember that for PHP 1 and "1" and true are the same values so they will overwrite each other.

array() is a construct with dynamic arguments representing literal arrays. The assignment of the given values to the array structure is done sequentially i.e. one by one from left to right. In your example:
Doctor is assigned to index 1.
Boss is assigned to index 2.
Lynx overwrites index 2.
Lentin and Endless are assigned to index 3 and 4 respectively.

hey #Weltkind first of all i suggest you to read
"http://php.net/manual/en/language.types.array.php",
now come to your answer In php the array key can be string or integer and if you do not mention the key then
default integer is set and the value of next array key is depending on the previous array integer key means
next array key = previous integer key + 1;
In PHP array the same key value will override by the same key
Now lets understand with your array2:
<?php
$array2 = array("1"=>'Doctor','Boss', 2=>'Lynx', 'Lentin', 'Endless');
1) as you started your array with key "1" so the
so for 1st key value is [1] => 'Doctor'
current array like: array([1] => 'Doctor')
now next key = previous integer key(that is 1) + 1 = 2;
2) for 2nd key value is [2] => 'BOSS'
current array like: array([1] => 'Doctor', [2] => 'BOSS')
3) next key = previous integer key(that is 2) + 1 = 3 it will carry
to next key but as next key is [2] => 'Lynx' as you mentioned so at
key [2] the value will be override by value 'BOSS' to 'Lynx'; current
array like : array([1] => 'Doctor', [2] => 'Lynx')
Now the next key we have is [3]
4) for next value the key is [3] => 'Lentin'
current array like : array([1] => 'Doctor', [2] => 'Lynx', [3] =>
'Lentin');
now next key = previous integer key(that is 3) + 1 = 4;
5) for next value the key is [4] => 'Endless'
current array like : array([1] => 'Doctor', [2] => 'Lynx', [3] =>
'Lentin', [4] => 'Endless');
and that is why the final array is like below :
array(
[1] => 'Doctor',
[2] => 'Lynx',
[3] => 'Lentin',
[4] => 'Endless'
);

Related

how to get the rightmost index value from an array with the same values using php

I'm making a program to take fingerprint values using a winnowing algorithm. which is where I have to take the smallest value from an array. but not only the smallest, there are conditions where if you find the same value, the rightmost value will be taken along with the index value. example array (3,5,5,8,6,7). from this example, I want to take the index of the second number 5, which is index 2, not index 1.
but when I try to get the same value as the rightmost position use the function min () in php, but the one I get is always the leftmost position.
I expect the output of
$a = array(3,5,5,8,6,7,6);
be
[3,0], [5,2], [8,3], [6,6], [7,5]
but the actual output is
[3,0], [5,1], [8,3], [6,4], [7,5]
Just a quick alternative, if you use array_flip() which swaps the value and key over, then this will automatically overwrite any previous key value, so when you flip [1=>5, 2=>5] then the second one will overwrite the first.
So
$a = array(3,5,5,8,6,7,6);
print_r(array_flip($a));
gives...
Array
(
[3] => 0
[5] => 2
[8] => 3
[6] => 6
[7] => 5
)
Use the value from an array as an index to filter out the most right value
$values = array(3,5,5,8,6,7,6);
$result = [];
foreach ($values as $index => $value) {
$result[$value] = [$value, $index];
}
print_r(array_values($result));
Use array_unique to solve this problem
$a = array(3,5,5,8,6,7,6);
$unique = array_unique($a);
print_r($unique);
answer should be
Array
(
[0] => 3
[1] => 5
[3] => 8
[4] => 6
[5] => 7
)

How can I get the numeric value from an array?

I have this following segment in my array.
[genres] => Array
(
[0] => Adventure
[1] => Drama
[2] => Game
[3] => Harem
[4] => Martial Arts
[5] => Seinen
)
I am trying to return each of those elements separately.
foreach($t['genres'] as $tag=>$value) {
// I don't know what to do from here
}
Can someone help me on how I can print each unique value?
genres is an associative array meaning that the key will only give you the index point of that value. Your values are of type String, not numerical values.
$genres = ['Adventure', 'Drama', 'Game', 'Harem', 'Martial Arts', 'Seinen'];
So in this case, at index point 0 (arrays start at 0), we will get Adventure.
[0] => Adventure
To get these values out of the array one by one you can do this:
foreach($genres as $_genre) {
echo $_genre;
}
To get these values and/or keys from the array one by one you can do this:
foreach($genres as $_key => $_genre) {
echo "Index: {$_key} - Value: {$_genre}"
}
Keys are numerical values, they mark the point of the value in that array. For example, if we wanted to get Game from the array:
[2] => Game
We can see that it has an index of 2 and can be called like:
echo $genres[2];

How does PHP set keys for arrays?

I have an array:
$a = array('color' => 'green', 'format' => 'text', 'link_url');
and another:
$b = array('zero', 'one', 'two', 'three', 'test' => 'ok', 'four');
And with array_merge() I have an array like this:
Array
(
[color] => green
[format] => text
[0] => link_url
[1] => zero
[2] => one
[3] => two
[4] => three
[test] => ok
[5] => four
)
Why PHP sets array key as above? Why not like this:
Array
(
[color] => green
[format] => text
[2] => link_url
[3] => zero
[4] => one
[5] => two
[6] => three
[test] => ok
[8] => four
)
That's because numeric IDs are counted separately from seeing indices. The string indices have no number and are not counted.
Quoting from the PHP manual for your original array definitions:
The key is optional. If it is not specified, PHP will use the increment of the largest previously used integer key.
and from the docs on array_merge():
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
So it's all quite explicitly documented
Well, if you look at the original array, should be clear:
array(3) {
["color"]=>
string(5) "green"
["format"]=>
string(4) "text"
[0]=>
string(8) "link_url"
}
You appear to have assumed an ordering or a congruity with non-numeric keys, which does not exist.
The numeric keys have an order and this is represented in their new values; the string keys are not part of that ordering system and thus do not affect those new numeric values.
This is simply the way it is and it makes complete sense.
Please check the doc :
If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.
Ref: http://www.php.net/manual/en/function.array-merge.php

How do I insert a value in between two keys in an array using php?

I have an array with 3 values:
$b = array('A','B','C');
This is what the original array looks like:
Array ( [0] => A [1] => B [2] => C )
I would like to insert a specific value(For example, the letter 'X') at the position between the first and second key, and then shift all the values following it down one. So in effect it would become the 2nd value, the 2nd would become the 3rd, and the 3rd would become the 4th.
This is what the array should look like afterward:
Array ( [0] => A [1] => X [2] => B [3] => C )
How do I insert a value in between two keys in an array using php?
array_splice() is your friend:
$arr = array('A','B','C');
array_splice($arr, 1, 0, array('X'));
// $arr is now array('A','X','B','C')
This function manipulates arrays and is usually used to truncate an array. However, if you "tell it" to delete zero items ($length == 0), you can insert one or more items at the specified index.
Note that the value(s) to be inserted have to be passed in an array.
There is a way without the use of array_splice. It is simpler, however, more dirty.
Here is your code:
$arr = array('A', 'B', 'C');
$arr['1.5'] = 'X'; // '1.5' should be a string
ksort($arr);
The output:
Array
(
[0] => A
[1] => B
[1.5] => X
[2] => C
)

PHP Multidimensional Array First Object

I have an array in PHP that looks like
Array ( [123654] => Array ( [0] => 123456789123456789 [1] => 1 [2] => 06/24/2011 [3] => 06/24/2012 [4] => 12355.44 [5] => 55321.55 ) )
I know in javascript I could access the data I need by doing array[0][0], how would I go about doing this in PHP. It is the 123456789123456789 value that I'm looking at getting.
Try this
array_slice($array, 0, 1);
http://php.net/array_slice
If you don't know the exact keys, you could do something like this:
$a = array_values($my_array);
$b = array_values($a[0]);
echo $b[0];
array_values replaces the keys by simple numbers from 0 to n-1 (where n is the count of values), by that you can access your desired value with the indexes [0][0]. See more here
http://codepad.org/YXu6884R
Here you go. See above for proof. The methodology from #azat is not explicit enough and is prone to risk if the elements of the array or sub array are re-arranged or if the key value for the super array changes.
$my_array = array( 123654 => array( 0 => '123456789123456789', 1 => '1', 2 => '06/24/2011', 3 => '06/24/2012', 4 => '12355.44', 5 => '55321.55' ) );
echo $my_array['123654'][0];
Try
$first = array_shift(array_values($array));
http://php.net/manual/en/function.array-shift.php

Categories