I have got this Array
Array ( [0] => 9314802498 [1] => 9314801890 [2] => MOSWAPELE ESTHER [3] => BENNETH )
I Want to retrieve like that Array
Array ( [0] => 9314802498 [1] => MOSWAPELE ESTHER [2] => 9314801890 [3] => BENNETH )
This can help -
$array = array ( 0 => 9314802498, 1 => 9314801890, 2 => 'MOSWAPELE ESTHER', 3 => 'BENNETH' );
$temp= array_chunk($array, count($array)/2);
$final = array();
foreach($temp[0] as $key => $val) {
$final[]= $val;
$final[]= !empty($temp[1][$key]) ? $temp[1][$key] : null;
}
var_dump($final);
Output
array(4) {
[0]=>
float(9314802498)
[1]=>
string(16) "MOSWAPELE ESTHER"
[2]=>
float(9314801890)
[3]=>
string(7) "BENNETH"
}
Assuming the count of numbers and names would be equal in that array. If not -
$temp= array_chunk($array, ceil(count($array)/2));
change the key
$arr = Array ( [0] => 9314802498 [1] => 9314801890 [2] => MOSWAPELE ESTHER [3] => BENNETH );
$arr[1] = 'MOSWAPELE ESTHER';
$arr[2] = '9314801890';
It will give you desired answer
Try replacing the key values like this:
$arr1 = array('9314802498', '9314801890', 'MOSWAPELE ESTHER', 'BENNETH');
$temp = $arr1[1];
$arr1[1] = $arr1[2];
$arr1[2] = $temp;
Try this code.
$arr = array(
0 => 9314802498,
1 => 9314801890,
2 => 'MOSWAPELE ESTHER',
3 => 'BENNETH'
);
$temp_val = $arr[1];
$arr[1] = $arr[2];
$arr[2] = $temp_val;
print_r($arr);
Output
Array ( [0] => 9314802498 [1] => MOSWAPELE ESTHER [2] => 9314801890 [3] => BENNETH )
Related
I got string like 1_2_3_4_5 and I want an array like this:
array(
[0] = 1
[1] = 1_2
[2] = 1_2_3
[3] = 1_2_3_4
[4] = 1_2_3_4_5
)
Any idea for a oneliner?
just for fun using substr
$st = '1_2_3_4_5';
$num[]=$st;
while($pos=strrpos($st,'_')){
$num[]=$st=substr($st,0,$pos);
}sort($num);
print_r($num);
Output
(
[0] => 1
[1] => 1_2
[2] => 1_2_3
[3] => 1_2_3_4
[4] => 1_2_3_4_5
)
<?php
$input = '1_2_3_4_5';
// Get an array with 1, 2, 3, 4 and 5
$parts = explode('_', $input);
$output = [];
$i = 1;
foreach ($parts as $part) {
// array_slice() will take 1 to n elements from the array of numbers
$values = array_slice($parts, 0, $i++);
// Join the values
$output[] = implode('_', $values);
}
print_r($output);
It will show this:
Array
(
[0] => 1
[1] => 1_2
[2] => 1_2_3
[3] => 1_2_3_4
[4] => 1_2_3_4_5
)
Try it at 3V4L.
I have 2 array's, first array have for example ItemID of my item, second array have description about my item. I want to match data into 1 array.
It looks like:
[rgInventory] => Array
(
[1234567890] => Array
(
[id] => 1234567890
[classid] => 123456789
[instanceid] => 987654321
[amount] => 1
[pos] => 1
)
)
[rgDescriptions] => Array
(
[192837465_918273645] => Array
(
[appid] => 730
[name] => Something
)
)
Items in arrays don't have the same value like ID, but they are in the same order so:
Description for the first item in rgInventory is in the first array inside rgDescriptions.
What should I do to match for example id from rgInventory with name from rgDescriptions in the same array for example $backpack = array();?
Regards for you.
Try this:
<?php
$array1 = array('rgInventory' =>
array(
'1234567890' => array(
'id' => 1234567890,
'classid' => 123456789,
'instanceid' => 987654321,
'amount' => 1,
'pos' => 1
)
)
);
$array2 = array(
'rgDescriptions' => array(
'192837465_918273645' => array(
'appid' => 730, 'name' => 'Something')
)
);
Create new function to combine the two arrays into one array:
function array_sum_recursive($data1, $data2) {
if (!is_array($data1) && !is_array($data2)) {
return $data1 + $data2;
}
// deepest array gets precedence
if (!is_array($data2)) {
return $data1;
}
if (!is_array($data1)) {
return $data2;
}
//merge and remove duplicates
$keys = array_unique(array_merge(array_keys($data1), array_keys($data2)));
foreach ($keys as $key) {
if (isset($data1[$key]) && isset($data2[$key])) {
$result[$key] = array_sum_recursive($data1[$key], $data2[$key]);
} else if (isset($data1[$key])) {
$result[$key] = $data1[$key];
} else {
$result[$key] = $data2[$key];
}
}
if(empty($result)){
echo "no result";
die();
}else{
return $result;
}
}
Put the two array in one array $newarray:
$newonearray = array_sum_recursive($array1, $array2);
echo '<pre>';
print_r($newonearray);
?>
And you will get this:
Array
(
[rgInventory] => Array
(
[1234567890] => Array
(
[id] => 1234567890
[classid] => 123456789
[instanceid] => 987654321
[amount] => 1
[pos] => 1
)
)
[rgDescriptions] => Array
(
[192837465_918273645] => Array
(
[appid] => 730
[name] => Something
)
)
)
Hope this may help.
You can use function each to get each element of both arrays, then merge its with array_merge and save this new item to backup array.
Try something like this
<?php
$rgInventory = ['firstInv' => ['invId' => 1], 'secondInv' => ['invId' => 2]];
$rgDescriptions = ['firstDesc' => ['descId' => 1], 'secondDesc' => ['descId' => 2]];
if (count($rgInventory) && count($rgInventory) == count($rgDescriptions)) {
$backpack = [];
while($inventory = each($rgInventory)) {
$description = each($rgDescriptions);
$item = array_merge($inventory['value'], $description['value']);
$backpack[] = $item;
}
var_dump($backpack);
}
Output will be:
array(2) {
[0]=>
array(2) {
["invId"]=>
int(1)
["descId"]=>
int(1)
}
[1]=>
array(2) {
["invId"]=>
int(2)
["descId"]=>
int(2)
}
}
I have one problem with randomness.
I have example array.
$example=array(
'F1' => array('test','test1','test2','test5'),
'F2' => array('test3', 'test4'),
'F3' => array('one','twoo','threee','x'),
'F5' => array('wow')
)
I want to choose random keys from array to other array with specified size. In this second array I want values from other groups.
For example i got
$amounts = array(4,3,1,2,1);
I want to choose randomly specified ammount of variables ($amount) from $example, but of course - always from other groups.
Example result:
$result=(
array(4) => ('test','test4','x','wow'),
array(3) => ('test2','test3','three'),
array(1) => ('test1')
array(2) => ('test5','one')
array(1) => ('twoo')
What I tried so far?
foreach($amounts as $key=>$amount){
$random_key[$key]=array_rand($example,$amount);
foreach($result[$key] as $key2=>$end){
$todelete=array_rand($example[$end]);
$result[$key][$key2]=$example[$amount][$todelete]
}
}
I don't know now, what to fix or to do next.
Thanks for help!
$example = array(
'F1' => array('test', 'test1', 'test2', 'test5'),
'F2' => array('test3', 'test4'),
'F3' => array('one', 'twoo', 'threee', 'x'),
'F5' => array('wow')
);
$amounts = array(4, 3, 1, 2, 1);
$result = array();
$example = array_values($example);
//randomize the array
shuffle($example);
foreach ($example as $group) {
shuffle($group);
}
//sort the example array by child length
usort($example, function ($a, $b) {
return count($b) - count($a);
});
foreach ($amounts as $amount) {
$tmpResult = array();
for ($i = 0; $i < $amount; $i++) {
if(empty($example[$i])){
throw new \InvalidArgumentException('The total number of values in the array exceed the amount inputed');
}
$tmpResult[] = array_pop($example[$i]);
}
$result[] = $tmpResult;
//sort the example array again by child length
usort($example, function ($a, $b) {
return count($b) - count($a);
});
}
print_r($result);
test result:
Array
(
[0] => Array
(
[0] => test5
[1] => x
[2] => test4
[3] => wow
)
[1] => Array
(
[0] => test2
[1] => threee
[2] => test3
)
[2] => Array
(
[0] => test1
)
[3] => Array
(
[0] => twoo
[1] => test
)
[4] => Array
(
[0] => one
)
)
This is my array :
Array
(
[0] => Array
(
[0] => S No.
[1] => Contact Message
[2] => Name
[3] => Contact Number
[4] => Email ID
)
[1] => Array
(
[0] => 1
[1] => I am interested in your property. Please get in touch with me.
[2] => lopa <br/>(Individual)
[3] => 1234567890
[4] => loperea.ray#Gmail.com
)
[2] => Array
(
[0] => 2
[1] => This user is looking for 3 BHK Multistorey Apartment for Sale in Sohna, Gurgaon and has viewed your contact details.
[2] => shiva <br/>(Individual)
[3] => 2135467890
[4] => sauron82#yahoo.co.in
)
)
How can I retrieve all data element wise?
You can get information about arrays in PHP on the official PHP doc page
You can access arrays using square braces surrounding the key you like to select [key].
So $array[1] will give yoo:
Array
(
[0] => 1
[1] => I am interested in your property. Please get in touch with me.
[2] => lopa <br/>(Individual)
[3] => 1234567890
[4] => loperea.ray#Gmail.com
)
And $array[1][2] will give you:
lopa <br/>(Individual)
Or you can walkt through the elements of an array using loops like the foreach or the for loop.
// perfect for assoc arrays
foreach($array as $key => $element) {
var_dump($key, $element);
}
// alternative for arrays with seamless numeric keys
$elementsCount = count($array);
for($i = 0; $i < $elementsCount; ++$i) {
var_dump($array[$i]);
}
You have integer indexed elements in multidimensional array. To access single element from array, use array name and it's index $myArray[1]. To get inner element of that previous selected array, use second set of [index] - $myArray[1][5] and so on.
To dynamically get all elements from array, use nested foreach loop:
foreach ($myArray as $key => $values) {
foreach ($values as $innerKey => $value) {
echo $value;
// OR
echo $myArray[$key][$innerKey];
}
}
The solution is to use array_reduce:
$header = array_map(
function() { return []; },
array_flip( array_shift( $array ) )
); // headers
array_reduce( $array , function ($carry, $item) {
$i = 0;
foreach( $carry as $k => $v ) {
$carry[$k][] = $item[$i++];
}
return $carry;
}, $header );
First of all we get the header from the very first element of input array. Then we map-reduce the input.
That gives:
$array = [['A', 'B', 'C'], ['a1', 'b1', 'c1'], ['a2', 'b2', 'c2'], ['a3', 'b3', 'c3']];
/*
array(3) {
'A' =>
array(3) {
[0] =>
string(2) "a1"
[1] =>
string(2) "a2"
[2] =>
string(2) "a3"
}
'B' =>
array(3) {
[0] =>
string(2) "b1"
[1] =>
string(2) "b2"
[2] =>
string(2) "b3"
}
'C' =>
array(3) {
[0] =>
string(2) "c1"
[1] =>
string(2) "c2"
[2] =>
string(2) "c3"
}
}
*/
I think this is what you are looking for
$array = Array
(
0=> Array
(
0 => 'S No.',
1 => 'Contact Message',
2 => 'Name',
3 => 'Contact Number',
4 => 'Email ID'
),
1 => Array
(
0 => 1,
1 => 'I am interested in your property. Please get in touch with me.',
2 => 'lopa <br/>(Individual)',
3 => '1234567890',
4 => 'loperea.ray#Gmail.com',
),
2 => Array
(
0 => 2,
1 => 'This user is looking for 3 BHK Multistorey Apartment for Sale in Sohna, Gurgaon and has viewed your contact details.',
2 => 'shiva <br/>(Individual)',
3 => '2135467890',
4 => 'sauron82#yahoo.co.in',
)
);
$result_array = array();
array_shift($array);
reset($array);
foreach($array as $x=>$array2){
foreach($array2 as $i => $arr){
if($i == 1){
$result_array[$x]['Contact Message'] = $arr;
}elseif($i == 2){
$result_array[$x]['Name'] = $arr;
}elseif($i == 3){
$result_array[$x]['Contact Number'] =$arr;
}elseif($i == 4){
$result_array[$x]['Email ID'] = $arr;
}
}
}
print_r($result_array);
i have 2 arrays and i want 2 create 2D array for create mysql record
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => 1
[1] => 2
[2] => 3
)
i want 2 merge them into 2 dimensional array like this
Array
(
[0] => Array
(
[designation_id] => 1
[judge_name] => a
)
[1] => Array
(
[designation_id] => 2
[judge_name] => b
)
[2] => Array
(
[designation_id] => 3
[judge_name] => c
)
)
i use array_merge_recursive and it generates result like this
Array
(
[0] => a
[1] => b
[2] => c
[3] => 1
[4] => 2
[5] => 3
)
Assuming there will always be the same amount of values in $array1 as there are in $array2..
$array1 = Array("a","b","c");
$array2 = Array(1,2,3);
$newArray = Array();
foreach($array1 as $key => $arr1Val){
$newArray[$key]['designation_id'] = $array2[$key];
$newArray[$key]['judge_name'] = $array1[$key];
}
Of course, you will have to alter $array1 and $array2 to your needs, but you understand the basic idea. Check it here.
Assuming $array1 is the judge_name and $array2 is the designation_id
$newArray = array();
for($i=0; $i<count($array1); $i++)
{
$newArray[] = array(
'designation_id' => $array2[$i],
'judge_name' => $array1[$i]
);
}
Codepad Demo
Outputs
Array
(
[0] => Array
(
[designation_id] => 1
[judge_name] => a
)
[1] => Array
(
[designation_id] => 2
[judge_name] => b
)
[2] => Array
(
[designation_id] => 3
[judge_name] => c
)
)
simple as hell
$array1 = array('a', 'b', 'c');
$array2 = array(1,2,3);
$merged = array();
foreach($array1 as $key => $value)
{
$merged[$key] = array(
'judge_name' => $value,
'designation_id' => array_key_exists($key, $array2) ? $array2[$key] : null
);
}
Assuming that both arrays are of same size
$length = count($array1);
$finalArray = array();
for ($i = 0; $i < $length; $i++) {
$temp = array();
$temp['designation_id'] = $array1[$i];
$temp['judge_name'] = $array2[$i];
$finalArray[$i] = $temp;
}
$a = array('a', 'b', 'c');
$b = array(1,2,3);
$output = array_map(function($i, $j){
return array(
'judge_name' => $j,
'designation_id' => $i
);
}, $a, $b);
var_dump($output);
Outputs
array(3) {
[0]=>
array(2) {
["judge_name"]=>
int(1)
["designation_id"]=>
string(1) "a"
}
[1]=>
array(2) {
["judge_name"]=>
int(2)
["designation_id"]=>
string(1) "b"
}
[2]=>
array(2) {
["judge_name"]=>
int(3)
["designation_id"]=>
string(1) "c"
}
}
If you have PHP >= 5.3 you could use MultipleIterator for that purpose:
$designations = array(1, 2, 3);
$judges = array('a', 'b', 'c');
$mt = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
$mt->attachIterator(new ArrayIterator($designations), "designation_id");
$mt->attachIterator(new ArrayIterator($judges), "judge_name");
$final = iterator_to_array($mt, false);
print_r($final);
Demo
It iterates over multiple arrays, taking a value from each array at every iterator; you can assign a key for each array that will be used to form a single array item.
Afterwards you convert the results into an array using iterator_to_array().
$new1=array("a","b","c");
$new2=array("1","2","3");
$req=array();
$d=0;
foreach($new1 as $value1)
{
foreach($new2 as $value2)
{
$req[$d]["designation_id"]=$value1;
$req[$d]["judge_name"]=$value2;
$d++;
}
}
echo "<pre>";
print_r($req);