I've an array like below :
$array = array('string'=>'hello','somethingeElse'=>'how r u', ....);
I wanna change the keys of array to numeric values (consecutive):
$array = array('1'=>'hello','2'=>'how r u','3'=>....);
any help would be appreciated ;)
You could use the array_values() function, which will basically do what you say.
array_values() returns all the values from the input array and indexes
numerically the array.
Example:
$array = array_values($array);
If you want to avoid PHP loops, you may try something like :
$newArray = array_combine(range(1, count($array)), array_values($array));
Related
I have an array in the array, and I want to make it just one array, and I can easily retrieve that data
i have some like
this
but the coding only combines the last value in the first array, not all values
is it possible to make it like that?
so that I can take arrays easily
I would make use of the unpacking operator ..., combined with array_merge:
$array['test2'] = array_merge(...array_merge(...$array['test2']));
In your case you need to flatten exactly twice, if you try to do it one time too much it will fail due to the items being actual arrays themselves (from PHP's perspective).
Demo: https://3v4l.org/npnTi
Use array_merge (doc) and ... (which break array to separate arrays):
function flatten($arr) {
return array_merge(...$arr);
}
$arr = [[["AAA", "BBB"]], [["CCC"]]];
$arr = flatten(flatten($arr)); // using twice as you have double depth
In your case, $arr is $obj["test2"]. If your object is json cast it to array first and if it is a string use json_decode
Live example: 3v4l
if you have a array then you can use the below code
if(!empty($array['test2'])){
$newarray = array();
foreach ($array['test2'] as $arrayRow) {
$newarray = array_merge($newarray,$arrayRow);
}
$array['test2'] = $newarray;
}
I have problem wiht array_merge():
First array:
$array1=array(
[0]=>array(["key1"]=>"value1",["key2"]=>"value2",["key3"]=>"value3")
);
Second array:
$array2=array(["key4"]=>"value4",["key5"]=>"value5",["key6"]=>"value6");
And I need merge this arrays to one like this:
$array1=array(
[0]=>array(["key1"]=>"value1",["key2"]=>"value2",
["key3"]=>"value3",["key4"]=>"value4",["key5"]=>"value5",["key6"]=>"value6"));
But when use:
$array3=array_merge($array1,$array2);
var_dump($array3);
var_dump return this:
array(
[0]=>array(["key1"]=>"value1",["key2"]=>"value2",
["key3"]=>"value3") ["key4"]=>"value4",["key5"]=>"value5",["key6"]=>"value6");
And don't know why.
Thanks
$array3=array(array_merge($array1[0],$array2));
you have to merge the inner array, not the outer one.
https://3v4l.org/dCm2F
Merging the first element from first array with the second one, may help:
$array3 = array();
$array3[0] = array_merge($array1[0], $array2);
If I have two arrays and I want to "combine" them into one array whilst maintaining each individual array and leaving the keys as they are as they are always duplicated each iteration, can I do this?:
$array1 = array('0'=>'Bob', '1'=>'Tom', '2'=>'John');
$array2 = array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan');
If I use array_merge:
$new_array = array_merge($array1, $array2);
I get:
array('0'=>'Bob','1'=>'Tom','2'=>'John','3'=>'Michelle','4'=>'Joan','5'=>'Susan')
whereas I want to get something like:
array(array('0'=>'Bob', '1'=>'Tom', '2'=>'John'),array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan'))
Create a new array and add the other arrays to that one like this:
$arr = array($array1, $array2);
$ new_array = array ($ array, $ array2);
I have a big associative array with over 1 000 items and I want to rename one key but the order must be preserved.
I don't wont to iterate over the complete array and copy it into a new.
have a look at the array_splice function: http://php.net/manual/en/function.array-splice.php
this will do the job
$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
$array_keys = array_keys($array);
$array_keys[array_search('old', $array_keys)] = 'new';
$array = array_combine($array_keys, $array);
$array[$newkeyname] = $array[$oldkeyname];
unset($array[$oldkeyname]);
I have 2 arrays , $array1 & $array2. I want to create new array such that its key value is values of array1 and values are values of array2. Is it possible..
I used following approach to create this new array named $inputs. Is this correct?
$inputs=array();
$array1=array("3","4","6");
$array2=array("a","b","c");
$inputs=array_fill("$array1",count($array1),$array2);
print_r($inputs);
If I read your question correctly, you can use array_combine() PHP Manual
$array1 = array("3","4","6");
$array2 = array("a","b","c");
$inputs = array_combine($array2, $array1); # keys, values
Use array_combine.
http://www.php.net/manual/de/function.array-combine.php
$inputs = array_combine($array1, $array2);
<?php
$inputs=array();
$array1=array("3","4","6");
$array2=array("a","b","c");
$inputs=array_combine($array1,$array2);
print_r($inputs);
?>
http://codepad.org/6fTXCZa5
use php array_combine function
http://php.net/manual/en/function.array-combine.php