I have a PHP array, which looks like follows:
array(8) {
[0]=>
string(3) "639"
[1]=>
string(2) "33"
[2]=>
string(2) "68"
[3]=>
string(3) "196"
[4]=>
string(3) "275"
[5]=>
string(3) "309"
[6]=>
string(3) "331"
[7]=>
string(3) "378"
}
I would like to change all the keys to these values to incrementing letters (a, b, c, etc.) - how would I do this?
I realise I can increment letters like so:
$x = "a";
$x++;
echo $x;
"b"
But how can I perform this within a loop?
The desired result would be something like this:
"a" => "639"
"b" => "33"
"c" => "68"
etc.
I think the following can help
$newArray = array();
$index = "a";
foreach($oldArray as $value)
{
$newArray[$index] = $value;
$index++;
}
You have provided the answer pretty much yourself already
$array = array('639', '33', '68', '196', '275', '309', '331', '378');
$index = 'a';
$newArray = array();
foreach ($array as $value) {
$newArray[$index++] = $value;
}
Following code will surely help you:
$result = [];
array_walk($data,function($v,$k)use (&$result){
$result[chr(65 + $k)] = $v;
});
print_r($result);
Demo
Related
Struggling to find the logic to merge 2 arrays in a specific way for example I have
array('lemons', 'oranges', 'grapes', 'pineapples');
and
array('carrots', 'onions', 'cabbage');
and I want to end up with
array('lemons', 'carrots', 'oranges', 'onions', 'grapes', 'cabbage' 'pineapples');
I.e to merge them in a one from each way rather than just together at the end and start.
$x = 0;
while ($x < max(count($fruit), count($veg))) {
if (isset($fruit[$x])) {
$merged[] = $fruit[$x];
}
if (isset($veg[$x])) {
$merged[] = $veg[$x];
}
$x++;
}
something like that?
Not the most sophisticated answer, but this is what I have come up with.
$array1 = array('lemons', 'oranges', 'grapes', 'pineapples');
$array2 = array('carrots', 'onions', 'cabbage');
$iterator = 0;
// Continue until the end of both arrays
while (!empty($array1) || !empty($array2)) {
// If iterator is odd then we want the second array, or if array one is empty
if ($iterator++ % 2 == 1 && !empty($array2) || empty($array1)) {
$array3[] = array_shift($array2);
} else {
$array3[] = array_shift($array1);
}
}
var_dump($array3);
Output:
array('lemons', 'carrots', 'oranges', 'onions', 'grapes', 'cabbage' 'pineapples');
Loop over the indexes (from 0 to max(count($array1), count($array2))), then in the loop statement, append the indexed element of each array to a new one, if they are set.
Try something like this:
$ar1 = array('lemons', 'oranges', 'grapes', 'pineapples');
$ar2 = array('carrots', 'onions', 'cabbage');
function mergeCustom($ar1, $ar2) {
$newAr = array();
foreach ($ar1 as $key => $item) {
array_push($newAr, $item);
if (!empty($ar2[$key])) {
array_push($newAr, $ar2[$key]);
}
}
// if length of second array is greater then first one
while(!empty($ar2[$key])) {
array_push($newAr, $ar2[$key]);
$key++;
}
return $newAr;
}
var_dump(mergeCustom($ar1, $ar2), mergeCustom($ar2, $ar1));
Output will be:
array(7) {
[0]=>
string(6) "lemons"
[1]=>
string(7) "carrots"
[2]=>
string(7) "oranges"
[3]=>
string(6) "onions"
[4]=>
string(6) "grapes"
[5]=>
string(7) "cabbage"
[6]=>
string(10) "pineapples"
}
array(8) {
[0]=>
string(7) "carrots"
[1]=>
string(6) "lemons"
[2]=>
string(6) "onions"
[3]=>
string(7) "oranges"
[4]=>
string(7) "cabbage"
[5]=>
string(6) "grapes"
[6]=>
string(6) "grapes"
[7]=>
string(10) "pineapples"
}
$input = "hello|world|look|at|this";
$explode = explode("|", $input);
$array = array("Title" => "Hello!", "content" => $explode);
This will output:
array(2) {
["Title"]=>
string(6) "Hello!"
["content"]=>
array(5) {
[0]=>
string(5) "hello"
[1]=>
string(5) "world"
[2]=>
string(4) "look"
[3]=>
string(2) "at"
[4]=>
string(4) "this"
}
}
But I want them to be keys with a NULL as value as I add values in a later step.
Any idea how to get the explode() function to return as keys? Is there a function from php available?
array_fill_keys can populate keys based on an array:
array_fill_keys ($explode, null);
Use a foreach loop on the explode to add them:
foreach($explode as $key) {
$array["content"][$key] = "NULL";
}
how about array_flip($explode)? That should give you this
array(2) {
["Title"]=>
string(6) "Hello!"
["content"]=>
array(5) {
[hello]=> 1
No nullvalues but atleast you got the keys right
$input = "hello|world|look|at|this";
$explode = explode('|', $input);
$nulls = array();
foreach($explode as $x){ $nulls[] = null; };
$array = array("Title" => "Hello!", "content" => array_combine($explode, $nulls));
Played for hours, but couldn't do this. Task looks very simple, though..I need recursively combine 2 arrays into one. Using first array's values as keys, and second array's leave values as they are. This is what I have:
array(2) {
[0]=>
array(4) {
[0]=>
string(9) "First"
[1]=>
string(6) "Something"
}
[1]=>
array(4) {
[0]=>
string(3) "More"
[1]=>
string(6) "Nomore"
}
}
Second array
array(2) {
[0]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
[1]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
}
What I'm trying to achive:
array(2) {
[0]=>
array(4) {
["First"]=>
string(1) "1"
["Something"]=>
string(1) "2"
}
[1]=>
array(4) {
["More"]=>
string(1) "1"
["Nomore"]=>
string(1) "2"
}
}
Another solution using array_combine
$first_array = array(
array('first', 'second', 'third'),
array('more1', 'more2', 'more3'),
);
$second_array = array(
array('val1', 'val2', 'val3'),
array('2val1', '2val2', '2val3')
);
$new_array = array();
foreach($first_array AS $k => $v) {
$new_array[$k] = array_combine($v,$second_array[$k]);
}
$firstArray = array(
array('first', 'second', 'third'),
array('more1', 'more2', 'more3'),
);
$secondArray = array(
array('val1', 'val2', 'val3'),
array('2val1', '2val2', '2val3')
);
$newArray = array();
for ($i=0; $i<count($firstArray); ++$i) {
$subArray1 = $firstArray[$i];
$subArray2 = $secondArray[$i];
$newArray[$i] = array();
for ($j=0; $j<count($subArray1); ++$j) {
$key = $subArray1[$j];
$value = $subArray2[$j];
$newArray[$i][$key] = $value;
}
}
var_dump($newArray);
Wouldn't be more elegant to do something like this ?
$newArray = array();
foreach ($firstArray as $key => $firstVal)
foreach ($secondArray as $key => $secondVal)
array_push($newArray, array_combine($firstVal, $secondVal));
This way you'll have the same result you wanted inside $newArray
with a bit simpler code.
I haven't tested that though, let me know if it works or breaks :)
Here is an example...
I have the following code:
$a=array("a","b","c");
$b=array("1","2","3");
$c = array_merge($a,$b);
echo "<pre>";
var_dump($c);
echo "</pre>";
Gives me an output:
array(6) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "1"
[4]=>
string(1) "2"
[5]=>
string(1) "3"
}
How could I change the code so that it gives me this output instead:
array(3) {
[0]=>
string(5) "a','1"
[1]=>
string(5) "b','2"
[2]=>
string(5) "c','3"
Any ideas?
$c = array_map(function ($a, $b) { return "$a','$b"; }, $a, $b);
For whatever that's good for...
Using SPL's MultipleIterator:
$a = array("a","b","c");
$b = array("1","2","3");
$mi = new MultipleIterator();
$mi->attachIterator(new ArrayIterator($a));
$mi->attachIterator(new ArrayIterator($b));
$c = array();
foreach($mi as $row) {
$c[] = $row[0] . "','" . $row[1];
}
var_dump($c);
If the keys to both arrays are always in parity, you could do something like
foreach ($a as $key => $value) {
$newArray[] = "$value','{$b[$key]}";
}
var_dump($newArray);
// would output the below
array(3) {
[0]=>
string(5) "a','1"
[1]=>
string(5) "b','2"
[2]=>
string(5) "c','3"
However, the result looks a little weird, are you sure this is what you are trying to achieve?
$a=array("a","b","c");
$b=array("1","2","3");
if(count($a) > count($b)){
foreach($a as $key => $value)
$c[$key] = isset($b[$key])? $value.",".$b[$key] : $value;
} else {
foreach($b as $key => $value)
$c[$key] = isset($a[$key])? $a[$key].",".$value : $value;
}
Use above code it will for your array.
Is there a way to set the value of members of an array with foreach?
<?
$arr = array(0=>'a',1=>'b',2=>'c',3=>'d');
foreach($arr as $key => $value){
$value = 'a';
}
var_dump($arr);
?>
returns:
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
}
Where what I am trying to get it to return is:
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "a"
[2]=>
string(1) "a"
[3]=>
string(1) "a"
}
Here is a link to the codepad I was using.
http://codepad.org/FQpPYFtz
$arr = array(0=>'a',1=>'b',2=>'c',3=>'d');
foreach($arr as $key => &$value) { // <-- use reference to $value
$value = 'a';
}
var_dump($arr);
It is quite simple:
foreach ($data as $key => $value) {
$data[$key] = 'new value';
}