How to change key array php - php

How to change key array php from
array(
[0]=>Joni
[1]=>Jono
[2]=>Riki
[3]=>Budi
);
Change index to:
array(
[nominal]=>Joni
[nominal]=>Jono
[nominal]=>Riki
[nominal]=>Budi
);

you can make a multi-dimensional array for this purpose
$arr = array('a','b','c','d');
for($i=0;$i<count($arr);$i++){
$newArr['nominal'][$i] = $arr[$i];
}
print_r($newArr);

The expected outcome you want is not possible at all, because same indexes will be over-written in single-dimensional array.
Check this to understand what i said above:- https://eval.in/954556
Now there are 2 closer possiblities of outcomes, which i am going to mention:-
$possibility1 = [];
$possibility2 =[];
foreach($array as $arr){
$possibility1[] = ['nominal'=>$arr];
$possibility2['nominal'][] = $arr;
}
print_r($possibility1);//first closer possibility
print_r($possibility2);//second closer possibility
Output:- https://eval.in/954559

Related

PHP renumber array but keep sequence

I have an array like this:
array(13,4,7,1,16);
I want to recount the array, but I want to keep the sequence, like this:
array(4,2,3,1,5);
How can I do this?
If you are trying to sort the array, keeping the keys in the same order as the values, the PHP asort() function does that.
If you want to keep the original array but get the keys in sort order, then you can use something like:
$arr = array(13,4,7,1,16);
asort($arr);
$keys = array_keys($arr);
Then $keys has the keys from the original array sorted in the order of the original values, e.g. $keys = array(4,2,3,1,5);
if you want to get the index of the array with reference to the sorted values
Try this
$numbers = array(13,4,7,1,16);
$numberscopy = $numbers;
sort($numberscopy);
$final = array();
//echo array_search(13, $numbers);
for($a=0 ; $a<count($numberscopy );$a++){
$final[] = array_search($numberscopy[$a], $numbers) + 1;
}
var_dump($final);

Storing array data in foreach loop in PHP

I am wondering how can I store all values from a foreach loop, I know that I am re-initialising in the loop but I'm not sure how to store the data. Heres my basic loop:
$array = array("v1", "v2", "v3", "v4");
foreach($array as $row){
$arr = array('val' => $row);
echo $row;
}
print_r($arr);
So when I use the print_r($arr) the only thing outputted would be v4 and I know that the values are there because the echo $row; does return each output individually.
My question would be how can I store each instance of row in my array?
Create a new array, fill it:
$array = array("v1", "v2", "v3", "v4");
$newArray = array();
foreach($array as $row){
// notice the brackets
$newArray[] = array('val' => $row);
}
print_r($newArray);
It looks like you are storing your array wrong.
Try adjusting the $arr = array('val' => $row);
to:
$arr[] = array('val' => $row);
This will set it so you pick up each line as a separate array which you can easily navigate through.
Hope this helps!
If I'm reading correctly, you want to transform your array from simple values to key-value pairs of 'val'->number. array_map is a concise way of doing this sort of transformation.
$array = array("v1", "v2", "v3", "v4");
$arr = array_map(function($v) { return array('val'=>$v); }, $array);
print_r($arr);
While it doesn't matter in this case, array_map also has the handy feature of preserving your keys, in case that is desired.
Note that you can also provide a named function to array_map, instead of providing the implementation inline, which can be nice in the event that your transform method gets more complicated. More on array_map here.

Add tems to array with keys in PHP

How to add array items to an existing array with key => value ? actually i want to create an array of mysql rowset i.e.
$n =0;
while($row = mysql_fetch_array($rowset))
{
$array[$n] = array('name' => $row['name'], 'city' = $row['city']);
$n += 1;
}
Thanks.
Just try with:
$existingArray['newKey'] = 'new value';
Or use array_merge function:
$newArray = array_merge($existingArray, $additionalData);
http://php.net/manual/en/function.array-merge.php
That what you're looking for?
-edit-
Just to note, if conflicting results are found, the last most array entry will be used. If you array merge three arrays with id fields, only the final arrays id will be stored in the result.
You may want to look into this:
http://php.net/array_push
Should be simple enough.
For One:
$array['key'] = $value;
Merge:
$mergedArray = array_merge($array1, $array2);
(http://php.net/manual/en/function.array-merge.php)

Combine Two Arrays with numerical keys without overwriting the old keys

I don't want to use array_merge() as it results in i misunderstood that all values with the same keys would be overwritten. i have two arrays
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
and would like to combine them resulting like this
array(0=>'foo', 1=>'bar',2=>'bar', 3=>'foo');
array_merge() appends the values of the second array to the first. It does not overwrite keys.
Your example, results in:
Array (
[0] => foo
[1] => bar
[2] => bar
[3] => foo )
However, 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.
Unless this was just an example to another problem you were having?
Does this answer your question? I'm not sure exactly what you're trying to accomplish, but from your description it sounds like this will work:
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
foreach ($array2 as $i) {
$array1[] = $i;
}
echo var_dump($array1);
If someone stumbles upon this, this is a way to do it nowadays:
var_dump(array_merge_recursive($array1, $array2));
There are probably much better ways but what about:
$newarray= array();
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
$dataarrays = array($array1, $array2);
foreach($dataarrays as $dataarray) {
foreach($dataarray as $data) {
$newarray[] = $data;
}
}
print_r($newarray);
$result = array_keys(array_merge(array_flip($array1), array_flip($array2)));
var_dump($result);

Referencing for element in a PHP array

Answer is not $array[0];
My array is setup as follows
$array = array();
$array[7] = 37;
$array[19] = 98;
$array[42] = 22;
$array[68] = 14;
I'm sorting the array and trying to get the highest possible match out after the sorting. So in this case $array[19] = 98;
I only need the value 98 back and it will always be in the first position of the array. I can't reference using $array[0] as the 0 key doesn't exist. Speed constraints mean I can't loop through the array to find the highest match.
There also has to be a better solution than
foreach ( $array as $value )
{
echo $value;
break;
}
$keys = array_keys($array);
echo $array[$keys[0]];
Or you could use the current() function:
reset($array);
$value = current($array);
You want the first key in the array, if I understood your question correctly:
$firstValue = reset($array);
$firstKey = key($array);
You can always do ;
$array = array_values($array);
And now $array[0] will be the correct answer.
If you want the first element you can use array_shift, this will not loop anything and return only the value.
In your example however, it is not the first element so there seems to be a discrepancy in your example/question, or an error in my understanding thereof.
If you're sorting it, you can specify your own sorting routine and have it pick out the highest value while you are sorting it.
$array = array_values($array);
echo $array[0];

Categories