mapping value to one key in php array - php

I have a array with many key and value pairs. I want to get all value with one key in php array.
I Have result like
$seat=[
(int) 0 => [
(int) 0 => 'A_1',
(int) 1 => 'A_2',
(int) 2 => 'A_3',
(int) 3 => 'A_4'
],
(int) 1 => [
(int) 0 => 'B_1',
(int) 1 => 'B_2',
(int) 2 => 'B_3'
]
]
i want to change like following syntax.
$seat=[
(int) 0 => [
(int) 0 => 'A_1',
(int) 1 => 'A_2',
(int) 2 => 'A_3',
(int) 3 => 'A_4'
(int) 4 => 'B_1',
(int) 5 => 'B_2',
(int) 6 => 'B_3'
]
]
thank all:

try this,
<?php
$result = array();
$seat=array(
"0" => array("0" => 'A_1',"1" => 'A_2',"2" => 'A_3',"3" => 'A_4'),
"1" => array("0" => 'B_1',"1" => 'B_2',"2" => 'B_3'),
"2" => array("0" => 'C_1',"1" => 'C_2',"2" => 'C_3'));
for($i=0;$i<count($seat);$i++){
$result = array_merge($result,$seat[$i]);
}
echo "<pre>";print_r($result);
?>
OUTPUT
Array
(
[0] => A_1
[1] => A_2
[2] => A_3
[3] => A_4
[4] => B_1
[5] => B_2
[6] => B_3
[7] => C_1
[8] => C_2
[9] => C_3
)

Simply using array_merge function.
$result = call_user_func_array('array_merge', $seat);
WORKING DEMO: http://phpio.net/s/1tx5

You can simply do it like this:
function flattenArray($arr) {
$toflat = array($arr);
$res = array();
while (($r = array_shift($toflat)) !== NULL) {
foreach ($r as $v) {
if (is_array($v)) {
$toflat[] = $v;
} else {
$res[] = $v;
}
}
}
return $res;
}
$newArray = array(flattenArray($array1)); // This will be your desired result
OUTPUT:
// So for example, if you pass array to flatternArray($array) method:
$array = array:2 [
0 => array:3 [
0 => 1
1 => 2
2 => 3
]
1 => array:3 [
0 => 4
1 => 5
2 => 6
]
]
// then $newArray will results in:
array:1 [
0 => array:6 [
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 6
]
]
Hope this helps!

Since your input is an indexed array, you can just flatten the structure (with a classic splat&merge technique) and push the merged structure into an empty array to achieve the desired result (no loop required).
Code: (Demo)
$seat = [
['A_1', 'A_2', 'A_3', 'A_4'],
['B_1', 'B_2', 'B_3'],
['C_1']
];
var_export(
[array_merge(...$seat)]
);
Output:
array (
0 =>
array (
0 => 'A_1',
1 => 'A_2',
2 => 'A_3',
3 => 'A_4',
4 => 'B_1',
5 => 'B_2',
6 => 'B_3',
7 => 'C_1',
),
)

Related

Merge with count for key value pair in associative array

I have this array and I want to combine the y_axis if the x_axis is the same, please see both examples so you will have an idea of what I need.
array:4 [
0 => array:2 [
"x_axis" => 8
"y_axis" => 1
]
1 => array:2 [
"x_axis" => 9
"y_axis" => 1
]
2 => array:2 [
"x_axis" => 11
"y_axis" => 2
]
3 => array:2 [
"x_axis" => 11
"y_axis" => 3
]
]
Like
array:3 [
0 => array:2 [
"x_axis" => 8
"y_axis" => 1
]
1 => array:2 [
"x_axis" => 9
"y_axis" => 1
]
2 => array:2 [
"x_axis" => 11
"y_axis" => 5
]
]
I wrote a function. Where i include two two loops. First loop create a new array where you calculate the values. The second create the final array where assign the values in the pattern where you need.
<?php
$arr = [
['x_axis' => 8, 'y_axis' => 1],
['x_axis' => 9, 'y_axis' => 1],
['x_axis' => 11, 'y_axis' => 2],
['x_axis' => 11, 'y_axis' => 3],
];
function make($arr) {
$xy = [];
foreach($arr as $k => $v) {
$xy[$v['x_axis']] = isset($xy[$v['x_axis']]) ? $xy[$v['x_axis']] + $v['y_axis'] : $v['y_axis'];
}
$arrNew = [];
foreach($xy as $k => $v) {
$arrNew[] = ['x_axis' => $k, 'y_axis' => $v];
}
return $arrNew;
}
print_r( make($arr) );
output
Array
(
[0] => Array
(
[x_axis] => 8
[y_axis] => 1
)
[1] => Array
(
[x_axis] => 9
[y_axis] => 1
)
[2] => Array
(
[x_axis] => 11
[y_axis] => 5
)
)

how to get the sum of 2 array with the same id in a collection

i have an array of collections with some same occurrences like below :
array:6 [▼
0 => Collection {#926 ▶}
1 => Collection {#926 ▶}
2 => Collection {#1045 ▶}
3 => Collection {#1045 ▶}
4 => Collection {#1156 ▶}
5 => Collection {#1156 ▶}
]
now what i want to do is to is to get the sum of those who repeated like 926 so i would have 2 prices to sum because 926 occurred 2 times . how can i achieve that thanks !
Use this approach:
// sum same keys in an array
$sum = array_sum(array_column($yourarray, 'your key/index'));
You Can do like this.
$array = [
'0' => array(
'id' => 926,
'total' => 90,
),
'1' => array(
'id' => 926,
'total' => 10,
),
'2' => array(
'id' => 927,
'total' => 101,
),
'3' => array(
'id' => 928,
'total' => 101,
),
];
$result = array();
foreach($array as $k => $v) {
$id = $v['id'];
$result[$id][] = $v['total'];
}
$new = array();
foreach($result as $key => $value) {
$new[] = array('id' => $key, 'total' => array_sum($value));
}
echo '<pre>';
print_r($new);
OUTPUT
Array
(
[0] => Array
(
[id] => 926
[total] => 100
)
[1] => Array
(
[id] => 927
[total] => 101
)
[2] => Array
(
[id] => 928
[total] => 101
)
)

Find the difference from a multi-dimencional array to a normal array in php

I have 2 arrays one contains just a list of email address and the other contains emails and names.
The list of emails in array1 are stored in a database and the other array is a list of new and current users.
I have tried using array_diff however this only seems to work if I specify in my loop that I just want emails.
$array1 = array(
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
);
$array2 = array(
0 => array(
0 => 'tom',
1 => 'tom#tomshouse.com'
),
1 => array(
0 => 'james',
1 => 'james#jameshouse.com'
),
2 => array(
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
),
3 => array(
0 => 'jane',
1 => 'jane#janeshouse.com'
),
);
$array2emails = array();
foreach ($array2 as $item) {
$array2emails[] = $item[1];
}
$result = array_diff($array2emails, $array1);
$results gives me
Array
(
[1] => james#jameshouse.com
[2] => marvin#marvinshouse.com
[3] => jane#janeshouse.com
)
however I need to get this:
Array
(
[1] => array(
[0] => james
[1] => james#jameshouse.com
)
[2] => array(
[0] => marvin
[1] => marvin#marvinshouse.com
)
[3] => array(
[0] => jane
[1] => jane#janeshouse.com
)
)
Any help would be much appreciated. Thanks
Since the array returned by array_diff has the same keys as the original array, you can use that to get the elements of the original array back, with array_intersect_key.
$array1 = array(
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
);
$array2 = array(
0 => array(
0 => 'tom',
1 => 'tom#tomshouse.com'
),
1 => array(
0 => 'james',
1 => 'james#jameshouse.com'
),
2 => array(
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
),
3 => array(
0 => 'jane',
1 => 'jane#janeshouse.com'
),
);
$array2emails = array_column($array2, 1);
$keys = array_diff($array2emails, $array1);
$result = array_intersect_key($array2, $keys);
print_r($result);
Result:
Array
(
[1] => Array
(
[0] => james
[1] => james#jameshouse.com
)
[2] => Array
(
[0] => marvin
[1] => marvin#marvinshouse.com
)
[3] => Array
(
[0] => jane
[1] => jane#janeshouse.com
)
)
$array1 = array(
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
);
$array2 = array(
0 => array(
0 => 'tom',
1 => 'tom#tomshouse.com'
),
1 => array(
0 => 'james',
1 => 'james#jameshouse.com'
),
2 => array(
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
),
3 => array(
0 => 'jane',
1 => 'jane#janeshouse.com'
),
);
$diffs = array();
foreach ($array2 as $item) {
if (!in_array($item[1], $array1)) {
$diffs[] = $item;
}
}
Since you are using a foreach loop why not create the array there?
As long as the arrays are not to long this will word nicely. I even took the original key into the new array. So you will be able to do other comparison methods to it.
$array1 = [
0 => 'dave#daveshouse.com',
1 => 'sam#samshouse.com',
2 => 'jim#jimshouse.com',
3 => 'olly#ollyshouse.com',
4 => 'tom#tomshouse.com'
];
$array2 = [
0 => [
0 => 'tom',
1 => 'tom#tomshouse.com'
],
1 => [
0 => 'james',
1 => 'james#jameshouse.com'
],
2 => [
0 => 'marvin',
1 => 'marvin#marvinshouse.com'
],
3 => [
0 => 'jane',
1 => 'jane#janeshouse.com'
],
];
$arrayExits = [];
foreach ($array2 as $key => $item) {
if(in_array($item[1], $array1)) {
$arrayExits[$key] = $item;
}
}
print_r($arrayExits);
Use array_filter function.
function filterEmail($value, $key){
global $array1;
return !in_array($value[1], $array1);
}
$emailDiff = array_filter($array2, 'filterEmail' , ARRAY_FILTER_USE_BOTH );
print_r($emailDiff );

how to convert associative array in to one array

I am stuck on this i have multiple associative array and i want to convert in to one:-
Here is the array:-
Array
(
[0] => Array
(
[0] => Women
)
[1] => Array
(
[0] => children
[1] => smile
)
[2] => Array
(
[0] => Abstract
)
[3] => Array
(
[0] => Lion
[1] => Cheetah
)
)
I want output something like this:-
Array
(
[0] => Women
[1] => children
[2] => smile
[3] => Abstract
[4] => Lion
[5] => Cheetah
)
Here i have tried so far:-
$getKeywords = DB::table('contributor_images')->select('keywords')->get();
$getKeywords = json_decode(json_encode($getKeywords),true);
foreach($getKeywords as $keyword){
$AllKeywords[] = $keyword['keywords'];
}
foreach ($AllKeywords as $key => $ExplodeKeywords) {
$searchkeywords[] = explode(',',$ExplodeKeywords);
}
echo "<pre>"; print_r($searchkeywords); die;
I am using laravel framework of php. THANKS IN ADVANCE :)
You can use Laravel helper function array_flatten for this:
$array = [
0 => [
0 => 'Women',
],
1 => [
0 => 'children',
1 => 'smile',
],
2 => [
0 => 'Abstract',
],
3 => [
0 => 'Lion',
1 => 'Cheetah',
],
];
$result = array_flatten($array);
var_dump($result);
Output:
array (size=6)
0 => string 'Women' (length=5)
1 => string 'children' (length=8)
2 => string 'smile' (length=5)
3 => string 'Abstract' (length=8)
4 => string 'Lion' (length=4)
5 => string 'Cheetah' (length=7)
Try this:
foreach ($old as $data) {
foreach ($data as $value) {
$new[] = $value;
}
}
print_r($new);
}
In first foreach you are getting array inside array and in second foreach you will get the value. Insert these values in new array to get desired result. Use print_r to see the result
Simply you can use : call_user_func_array
<?php
$array = array (
0 =>
array (
0 => 'Women',
),
1 =>
array (
0 => 'children',
1 => 'smile',
),
2 =>
array (
0 => 'Abstract',
),
3 =>
array (
0 => 'Lion',
1 => 'Cheetah',
),
);
$result = call_user_func_array('array_merge', $array);
print_r($result);
?>
Output :
Array
(
[0] => Women
[1] => children
[2] => smile
[3] => Abstract
[4] => Lion
[5] => Cheetah
)
Check here : https://eval.in/829111
Ref : http://php.net/manual/en/function.call-user-func-array.php
Since Laravel 5.7 the syntax is Arr::flatten($array) so that now it looks like
use Illuminate\Support\Arr;
$array = [
0 => [
0 => "Women"
],
1 => [
0 => 'children',
1 => 'smile'
],
2 => [
0 => 'Abstract'
],
3 => [
0 => 'Lion',
1 => 'Cheetah'
]
];
Arr::flatten($array);
Output :
array:6 [
0 => "Women"
1 => "children"
2 => "smile"
3 => "Abstract"
4 => "Lion"
5 => "Cheetah"
]
Docs be here

how to sort array data in alphabetic key order in php

my collection of data in array which is shown below the index key is A,B,C but i want to store these key in "key" and its key letter's words in "dishes" key
array:3 [
"A" => array:4 [
0 => 37
1 => "Algerian"
2 => 6
3 => "American"
]
"B" => array:6 [
0 => 27
1 => "Belgian"
2 => 20
3 => "Brazilian"
]
and so on..
i wanna sort this array like aplhabetic order as shown below
array:10 [
0 => array:2 [
"key" => "A"
"dishes" => array:2 [
0=>array:2[
"id" => 37
"type" => "Algerian"
],
1=>array:2[
"id" => 6
"type" => "American"
]
]
]
1 => array:2 [
"key" => "B"
"dishes" => array:2 [
0=>array:2[
"id" => 27
"type" => "Belgian"
],
1=>array:2[
"id" => 20
"type" => "Brazilian"
]
]
]
and so on...
This would be a possible solution:
<?php
$input = [
'A' => [
0 => 37,
1 => "Algerian",
2 => 6,
3 => "American"
],
'B' => [
0 => 27,
1 => "Belgian",
2 => 20,
3 => "Brazilian"
]
];
$output = [];
array_walk($input, function($values, $key) use (&$output) {
$entry = [
'key' => $key,
'dishes' => []
];
foreach(array_chunk($values, 2) as $chunk) {
$entry['dishes'][] = [
'id' => $chunk[0],
'type' => $chunk[1]
];
}
$output[] = $entry;
});
print_r($output);
The output of above code obviously is:
Array
(
[0] => Array
(
[key] => A
[dishes] => Array
(
[0] => Array
(
[id] => 37
[type] => Algerian
)
[1] => Array
(
[id] => 6
[type] => American
)
)
)
[1] => Array
(
[key] => B
[dishes] => Array
(
[0] => Array
(
[id] => 27
[type] => Belgian
)
[1] => Array
(
[id] => 20
[type] => Brazilian
)
)
)
)
You have to loop through the original array to create your new structure. Then you can use the ksort function to sort them.
$newArr = new array();
for ($arr as $elem) {
$dishArr = new array();
for($elem['dishes'] as $dish) {
$dishArr[] = $dish['id'];
$dishArr[] = $dish['type'];
}
$newArr[$elem['key']] = $dishArr;
}
ksort($newArr);

Categories