I have an array from numbers like this :
$array = array (1,2,3,4,5,6,7,8,9,10,11,12);
I tried to convert it to 2D using two for loops but i failed. Is it better to try with foreach loop , or it can be done with for-s ?
I wanna make something like this:
$array = array (array (1,2,3,4), array(5,6,7,8), array(9,10,11,12));
The reason I'm posting an answer is that a question like this and the availability of array_chunk() may not be very intuitive:
$array = array_chunk($array, 4);
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;
}
This question already has answers here:
Merge two flat indexed arrays of equal size so that values are pushed into the result in an alternating fashion
(2 answers)
Closed last month.
I have two arrays:
$array1 = array("A","One","C","Z");
$array2 = array("B","K","2","5");
Is there some built-in PHP way to get a final array with (I don't know how to say it, maybe 1-1 correspondence addition) alternate keys appended to each other like this
$final_array = array("A","B","One","K","C","2","Z","5");
If I use array_merge, I get:
$final_array = array("A","One","C","Z","B","K","2","5")
But that's exactly what an array_merge does. Is there any workaround other than looping?
Try this:
$array1 = array(1,3,5,7);
$array2 = array(2,4,6,8);
$final_array = array_merge($array1,$array2);
sort($final_array);
print_r($final_array);
Hope this helps. Sorted the array using the sort function.
I am trying to compare two non-associative arrays to create a new array with the matches.
This is what I have though:
//This array has several entries
$a_firstarray = Array();
//This array has less entries than the first array
$a_secondarray = Array();
//This array should contain the matches of the first array and the second array in no particular order
$a_mergedarray
for($i=0;$i <=count($a_firstarray);$i++){
for($a=0;$a <=count ($a_secondarray);$a++){
if($a_firstarray[$i] == $a_secondarray[$a]){
$a_mergedarray[] = $a_activecategory[$i];
}
}
}
It doesn't work for some reason. I am also sure that PHP has some sort of function that does this. Any ideas? thanks in advance.
This is known as the "intersection" of two arrays. PHP provides array_intersect.
use array_intersect.
$result = array_intersect($array1, $array2);
Are you looking for array_intersect()?
http://php.net/manual/en/function.array-intersect.php
I am using str_split() to split a long strings into an array of length 16 each. And I'm assigning the returned array to one in my function. Like this:
$myarray = str_split($string, 16);
The problem is that I want the indexing of $myarray to start from a number other than 0, say 50. Currently I'm doing this:
foreach($myarray as $id => $value)
{
$myarray[$id + 50] = $value;
unset($myarray[$id]);
}
Is there a better solution? Because the arrays and strings I'm dealing with are very long. Thanks
You can use array_pad().
$myarray = str_split($string, 16);
$myarray = array_pad($myarray, -(size($myarray)+50), null);
It will fill the first 50 elements with nulls and push the rest of the array forward by 50 elements.
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));