I have an array like below.
$arr=array('0_1','0_3','1_2',1_1','4_1');
can i divide it into
$arr[0][1]='0_1';
$arr[0][3]='0_3';
...
Thanks.
$newArray = [];
foreach ($arr as $item) {
$items = explode('_', $item);
$newArray[$items[0]][$items[1]] = $item;
}
var_dump($newArray);
exit();
This should do the trick. You should have a look at the explode function
Another way to do this without foreach loop. :->
$arr = array('0_1','0_3','1_2','1_1','4_1');
$result = [];
array_walk($arr,function($v,$k)use (&$result){
$data = explode("_",$v);
$result[$data[0]][$data[1]] = $v;
});
print_r($result);
$arrv = array('0_1','0_3','1_2','1_1','4_1');
$newArray = [];
array_walk($arrv,function($value,$key){
global $newArray;
$indexArray = explode("_",$value);
$newArray[$indexArray[0]][$indexArray[1]] = $value;
});
var_dump($newArray);
Related
I've got 2 arrays.
I would like to add each element in array2 after each element in array1.
Could you please help me how to figure out this in PHP.
Array1[] = ("Test1","Test2","Test3","Test4");
Array2[] = ("ADD1","ADD2","ADD3","ADD4");
FinalArray[] =("Test1","ADD1","Test2","ADD2","Test3","ADD3","Test4","ADD4");
Thanks.
You can do it using foreach
$Array1 = array("Test1","Test2","Test3","Test4");
$Array2 = array("ADD1","ADD2","ADD3","ADD4");
foreach ($Array1 as $key => $value) {
$FinalArray[] = $value;
if(isset($Array2[$key]))
$FinalArray[] = $Array2[$key];
}
print_r($FinalArray);
See the result
try this
<?php
$Array1 = array("Test1","Test2","Test3","Test4");
$Array2 = array("ADD1","ADD2","ADD3","ADD4");
for($i=0 ;$i<count($Array1);$i++ ){
$a[] = $Array1[$i];
$a[] = $Array2[$i];
}
print_r($a);
?>
Try it:
$k=0;
$j=0;
$final_arr = array();
for($i=1;$i<=count($arr1);$i++)
{
if($i%2!=0)
{
array_push($final_arr,$arr1[$k]);
$k++;
}
else
{
array_push($final_arr,$arr2[$j]);
$j++;
}
}
Try Using This Code:
<?php
$arr1 = array("Test1","Test2","Test3","Test4");
$arr2 = array("ADD1","ADD2","ADD3","ADD4");
$final_arr = array();
$index1=0;
$index2=0;
for($i=1;$i<=count($arr1)+count($arr2);$i++)
{
if($i%2==0)
{
array_push($final_arr,$arr2[$index2]);
$index2++;
}
else
{
array_push($final_arr,$arr1[$index1]);
$index1++;
}
}
I have two variable:
$keystr = 'plant,fruit,exotic';
$value='kiwi';
how can i create the associative array?
$arr = ('plant'=>array('fruit'=>array('exotic'=>'kivi')));
$keystr = 'plant,fruit,exotic';
$value='kiwi';
$arr = array();
$current = &$arr;
$keys = explode(',', $keystr);
foreach($keys as $key) {
$current[$key] = array();
$current = &$current[$key];
}
$current = $value;
unset($current);
var_dump($arr);
See http://ideone.com/YiMIRb for a demonstration
I have 2D array and want to get all values which are at same index say at index '1'. what is the best way to get that as a new array.
Example: we have array(array(1,2,3), array(5,6,7)), the result must be array(2, 6).
Thanks
A simple function would do the trick:
function foobar($array, $index) {
$result = array();
foreach($array as $subarray) {
if(isset($subarray[$index])) {
$result[] = $subarray[$index];
}
}
return $result;
}
Or you can just use array_map (requires PHP 5.3):
array_map(function($array) { return $array[1]; }, $input);
$sample = array(array(1,2,3),
array(4,5,6),
array(7,8,9)
);
$index = 1;
$result = array_map(function($value) use($index) { return $value[$index]; }, $sample);
var_dump($result);
$input = array(
array(1,2,3),
array(5,6,7)
);
$output = array();
foreach ( $input as $data ) {
$output[] = $data[1];
}
$myarray=array(array(1,2,3), array(5,6,7));
$index=1;
$result=array();
foreach($myarray as $a) $result[]=$a[$index];
print_r($result);
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
From the above array i need the value Dog alone. how can i get the unique value from an array?. is there any functions in php?...
Thanks
Ravi
Have a look at:
http://php.net/function.array-unique
and maybe:
http://php.net/function.array-count-values
$a = array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
$counted = array_count_values($a);
$result = array();
foreach($counted as $key => $value) {
if($value === 1) {
$result[] = $key;
}
}
//$result is now an array of only the unique values of $a
print_r($result);
function getArrayItemByValue($search, $array) {
// without any validation and cheking, plain and simple
foreach($array as $key => $value) {
if($search === $value) {
return $key;
}
}
return false;
}
then try using it:
echo $a[getArrayitembyValue('Dog', $a)];
Try with:
$a = array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
$aFlip = array_flip($a);
$unique = array();
foreach ( array_count_values( $a ) as $key => $count ) {
if ( $count > 1 ) continue;
// $unique[ array_search($key) ] = $key;
$unique[ $aFlip[$key] ] = $key;
}
Use following function seems to be working & handy.
<?php
$array1 = array('foo', 'bar', 'xyzzy', 'xyzzy', 'xyzzy');
$dup = array_unique(array_diff_assoc($array1, array_unique($array1)));
$result = array_diff($array1, $dup);
print_r($result);
?>
You can see its working here - http://codepad.org/Uu21y6jf
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
$result = array_unique(a);
print_r($result);
try this one...
From:
$arr = array(array('key1'=>'A',...),array('key1'=>'B',...));
to:
array('A','B',..);
$output = array();
foreach ($arr as $array_piece) {
$output = array_merge($output, $array_piece);
}
return array_values($output);
On the other hand, if you want the first value from each array, what you want is...
$output = array();
foreach ($arr as $array_piece) {
$output[] = array_unshift($array_piece);
}
But I'm thinking you want the first one.
Relatively simple conversion by looping:
$newArray = array()
foreach ($arr as $a) {
foreach ($a as $key => $value) {
$newArray[] = $value;
}
}
Or, perhaps more elegantly:
function flatten($concatenation, $subArray) {
return array_merge($concatenation, array_values($subArray));
}
$newArray = array_reduce($arr, "flatten", array());
John's solution is also nice.
Something like this should work
<?
$arr = array(array('key1'=>'A','key2'=>'B'),array('key1'=>'C','key2'=>'D'));
$new_array = array();
foreach ($arr as $key => $value) {
$new_array = array_merge($new_array, array_values($value));
}
var_export($new_array);
?>
If you want all the values in each array inside your main array.
function collapse($input) {
$buf = array();
if(is_array($input)) {
foreach($input as $i) $buf = array_merge($buf, collapse($i));
}
else $buf[] = $input;
return $buf;
}
Above is a modified unsplat function, which could also be used:
function unsplat($input, $delim="\t") {
$buf = array();
if(is_array($input)) {
foreach($input as $i) $buf[] = unsplat($i, $delim);
}
else $buf[] = $input;
return implode($delim, $buf);
}
$newarray = explode("\0", unsplat($oldarray, "\0"));