How to combine two arrays as key and value? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In one array there are they keys and in another array there the values. I want to combine these two arrays in a manner all keys of array1 and all values of array2 as shown below. How can it be done in PHP?
Here are the two arrays
Array
(
[0] => url
[1] => downloadName
[2] => downloadType
[3] => downloadSize
[4] => url
[5] => downloadName
[6] => downloadType
[7] => downloadSize
)
Array
(
[0] => https://www.clearcube.com/support/controller/downloads.php?id=450
[1] => F6151 Media Converter System with 100 base TX to 100 base FX.pdf
[2] => Manual
[3] => 0.39
[4] => https://www.clearcube.com/support/controller/downloads.php?id=582
[5] => G0400147 Rev B.xlsx
[6] => Manual
[7] => 0.37
)
And I need in this format.
Array
(
[url] => Array(
[0] => https://www.clearcube.com/support/controller/downloads.php?id=450
[1] => https://www.clearcube.com/support/controller/downloads.php?id=582
)
[downloadName] => Array(
[0] => F6151 Media Converter System with 100 base TX to 100 base FX.pdf
[1] => G0400147 Rev B.xlsx
)
[downloadType] => Array(
[0] => Manual
[1] => Manual
)
[downloadSize] => Array(
[0] => 0.39
[1] => 0.37
)
)

With the help of array_walk() you can combine it easily.
$keys = ['url','downloadName','downloadType','downloadSize','url','downloadName','downloadType','downloadSize'];
$values = [ 'https://www.clearcube.com/support/controller/downloads.php?id=450', 'F6151 Media Converter System with 100 base TX to 100 base FX.pdf', 'Manual', 0.39, 'https://www.clearcube.com/support/controller/downloads.php?id=582', 'G0400147 Rev B.xlsx', 'Manual', 0.37 ];
$result = [];
array_walk($keys, function ($val, $key) use (&$result, $values) { $result[$val][] = $values[$key]; });
print_r($result);
Working demo.
Output:
Array
(
[url] => Array
(
[0] => https://www.clearcube.com/support/controller/downloads.php?id=450
[1] => https://www.clearcube.com/support/controller/downloads.php?id=582
)
[downloadName] => Array
(
[0] => F6151 Media Converter System with 100 base TX to 100 base FX.pdf
[1] => G0400147 Rev B.xlsx
)
[downloadType] => Array
(
[0] => Manual
[1] => Manual
)
[downloadSize] => Array
(
[0] => 0.39
[1] => 0.37
)
)

a simple way would be
$result = [];
foreach ($array1 as $key1 => $value1) {
$result[$value1][] = $array2[$key1];
}
with verification implemented and independently from the indexes
if (count($array1) == count($array2)) {
$result = [];
$i = 0;
foreach ($array1 as $value1) {
$j=0;
foreach ($array2 as $value2) {
if ($j == $i) {
$result[$value1][] = $value2;
break;
}
$j++;
}
$i++;
}
var_dump($result);
}

Suppose key array is $arr1 and data array is $arr2 then you can run following loop and create new array by following
$new_arr = []
foreach($arr1 as $key=>$arr){
$new_arr[$arr][] = $arr2[$key];
}
//final array is $new_arr

You might use an index based approach, first checking if the length of the arrays are the same.
Loop the keys from array 1 and add the values from array 2 for that key.
$result = [];
if (count($array1) === count($array2)) {
for ($i = 0; $i < count($array1); $i++) {
$result[$array1[$i]][] = $array2[$i];
}
}
print_r($result);
Php demo
Result
Array
(
[url] => Array
(
[0] => https://www.clearcube.com/support/controller/downloads.php?id=450
[1] => https://www.clearcube.com/support/controller/downloads.php?id=582
)
[downloadName] => Array
(
[0] => F6151 Media Converter System with 100 base TX to 100 base FX.pdf
[1] => G0400147 Rev B.xlsx
)
[downloadType] => Array
(
[0] => Manual
[1] => Manual
)
[downloadSize] => Array
(
[0] => 0.39
[1] => 0.37
)
)

Related

How to separating arrays in php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I get array data with this format:
For example:
array = [
"index-category",
"create-category",
"store-category",
"show-category",
"index-products",
"create-products",
"store-products",
"show-products",
"edit-products",
"index-sales",
"create-sales",
]
I need convert to :
array {
0 => {
"index-category",
"create-category",
"store-category",
"show-category",
},
1 => {
"index-products",
"create-products",
"store-products",
"show-products",
"edit-products",
}
2 => {
"index-sales",
"create-sales",
}
}
How to convert to this array?
Is php a method that automatically converts ?
I think you just mess up with proper array notation here. Array notation no uses curly braces { or }. So If it is a proper array then you can try like this way using array_chunk()
N.B : For array notation we can use two different ways e.g $array = [] or $array=array()
<?php
$array = [
'category.index',
'category.create',
'category.store',
'category.edit',
'product.index',
'product.create',
'product.store',
'product.edit',
'city.index',
'city.create',
'city.store',
'city.edit',
];
print '<pre>';
print_r(array_chunk($array,4));
print '</pre>';
?>
Output:
Array
(
[0] => Array
(
[0] => category.index
[1] => category.create
[2] => category.store
[3] => category.edit
)
[1] => Array
(
[0] => product.index
[1] => product.create
[2] => product.store
[3] => product.edit
)
[2] => Array
(
[0] => city.index
[1] => city.create
[2] => city.store
[3] => city.edit
)
)
DEMO: https://eval.in/980164
EDIT:
AS PER YOUR QUESTION UPDATE
Simply try like this with list()
$new = array();
foreach($array as $val) {
list($item, $number) = explode('.', $val);
$new[$item][] = $val;
}
print '<pre>';
print_r(array_values($new));
print '</pre>';
DEMO: https://eval.in/980176
Output:
Array
(
[0] => Array
(
[0] => category.index
[1] => category.create
[2] => category.store
[3] => category.edit
)
[1] => Array
(
[0] => product.index
[1] => product.create
[2] => product.store
)
[2] => Array
(
[0] => city.index
[1] => city.create
[2] => city.store
[3] => city.edit
)
)
EDITED AGAIN
As you've different initial array on your question now so now code should be like this.
$new = array();
foreach($array as $val) {
list($item, $number) = explode('-', $val);
$new[$number][] = $val;
}
print '<pre>';
print_r(array_values($new));
print '</pre>';
DEMO: https://eval.in/980251
This would be a simple algorithm to convert the input data:
<?php
$input = [
'category.index',
'category.create',
'category.store',
'category.edit',
'product.index',
'product.create',
'product.store',
'city.index',
'city.create',
'city.store',
'city.edit',
];
$category = [];
array_walk($input, function ($entry) use (&$category) {
$tokens = explode('.', $entry);
$category[$tokens[0]][] = $entry;
});
$output = array_values($category);
print_r($output);
The output obviously is:
Array
(
[0] => Array
(
[0] => category.index
[1] => category.create
[2] => category.store
[3] => category.edit
)
[1] => Array
(
[0] => product.index
[1] => product.create
[2] => product.store
)
[2] => Array
(
[0] => city.index
[1] => city.create
[2] => city.store
[3] => city.edit
)
)
Here is the same algorithm with a trivial modification to match the changed input data you now suddenly presented by updating the question and commenting to this answer.
As already mentioned before in the comments all this does not really affect the algorithm, tiny adjustments will again deliver the desired output:
<?php
$input = [
"index-category",
"create-category",
"store-category",
"show-category",
"index-products",
"create-products",
"store-products",
"show-products",
"edit-products",
"index-sales",
"create-sales",
];
$category = [];
array_walk($input, function ($entry) use (&$category) {
$tokens = explode('-', $entry);
$category[$tokens[1]][] = $entry;
});
$output = array_values($category);
print_r($output);
This will, as expected, result in that output:
Array
(
[0] => Array
(
[0] => index-category
[1] => create-category
[2] => store-category
[3] => show-category
)
[1] => Array
(
[0] => index-products
[1] => create-products
[2] => store-products
[3] => show-products
[4] => edit-products
)
[2] => Array
(
[0] => index-sales
[1] => create-sales
)
)

Convert Multidimensional array to single array in php [duplicate]

This question already has answers here:
Convert multidimensional array into single array [duplicate]
(24 answers)
Closed 5 years ago.
It's probably beginner question but I'm going through documentation for
longer time already and I can't find any solution and i have an array which
is multidimensional given below format.
/* This is how my array is currently */
Array
(
[0] => Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
)
[1] => Array
(
[0] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[1] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
)
I want to convert this array into this form
/*Now, I want to simply it down to this*/
Array
(
[0] => Array
(
[title] => a
[title_num] =>1
[status] => 1
)
[1] => Array
(
[title] => Mr
[title_num] => 82
[status] => 1
)
[2] => Array
(
[title] => b
[title_num] =>25
[status] => 2
)
[3] => Array
(
[title] => c
[title_num] =>45
[status] => 2
)
)
I have tried array_flatten,array_map PHP built in function
A link or anything to point me in the right direction will be highly
appreciated
Here is another trick to solve your problem,
function custom_filter($array) {
$temp = [];
array_walk($array, function($item,$key) use (&$temp){
foreach($item as $value)
$temp[] = $value;
});
return $temp;
}
array_walk — Apply a user supplied function to every member of an array
Here is working demo.
here you go
$result = [];
foreach($array as $arr)
{
$result = array_merge($result , $arr);
}
var_dump($result);
Like this:
$i=0;
foreach ($array as $n1) {
foreach ($n1 as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
$i++;
}
Or simpler as suggested in the comments
for ($i=0; $i < count($array); $i++) {
foreach ($array[$i] as $n2) {
$newArr[$i]['title']=$n2['title'];
$newArr[$i]['title_num']=$n2['title_num'];
$newArr[$i]['status']=$n2['status'];
}
}

Sorting php array by column in ascending order

Array
(
[content_type] => Array
(
[0] => story
[1] => delhi
[2] => tez
)
[type] => Array
(
[0] => video_id
[1] => subcategory
[2] => story_id
)
[fetch_id] => Array
(
[0] => 32
[1] => 32
[2] => 2
)
[order] => Array
(
[0] => 6
[1] => 4
[2] => 5
)
[label] => Array
(
[0] => dsfs fdsf dsf sdf
[1] => dfsdfs
[2] => sdfsdfsd
)
[link] => Array
(
[0] => fsd fsdf sdf
[1] => fsdfsdfdsf
[2] => fsdfdsfds
)
[record] => Array
(
[0] => 10
[1] => 8
[2] => 12
)
)
Above is the array I have to sort this array in the basis of order field and it should shorted all the fields accordingly like below example.
$arr['order'][0] = 4;
$arr['order'][1] = 5;
$arr['order'][2] = 6;
$arr['type'][0] = 'subcategory';
$arr['type'][1] = 'story_id';
$arr['type'][2] = 'video_id';
and so on.....
You can try this -
$new = array();
// Extract and get the keys as values
$order = array_flip($array['order']);
// sort them according to keys
ksort($order);
// loop through main array
foreach($array as $key => $sub_array) {
// loop through order
foreach ($order as $o) {
// store the new value according to order
$new[$key][] = $sub_array[$o];
}
}
Demo
Some lesser solution:
asort($array['order']);
foreach ($array as $key => $subArray) {
$array[$key] = array_replace($array['order'], $subArray);
}
For reset a key sequence you may just to use array_values().

Sort multidimensional array in PHP using foreach

For example i have an array named $slice like this :
Array
(
[0] => Array
(
[0] => 12
[1] => 4
[2] => 2
[3] => 8
[4] => 20
)
[1] => Array
(
[0] => 9
[1] => 7
[2] => 1
[3] => 10
[4] => 23
)
)
I want to sort array above so the output will be like this :
Array
(
[0] => Array
(
[0] => 2
[1] => 4
[2] => 8
[3] => 12
[4] => 20
)
[1] => Array
(
[0] => 1
[1] => 7
[2] => 9
[3] => 10
[4] => 23
)
)
Then i tried to use foreach and array_multisort, and when i use print_r the result is 1 for each col :
foreach ($slice1 as $col) {
$slicesort[] = array_multisort($col);
}
output :
Array
(
[0] => 1
[1] => 1
)
array_multisort sorts the array in place, it does not return the sorted array. You need to use it like this:
foreach ($slice1 as $col) {
array_multisort($col);
$slicesort[] = $col;
}
Having said this, array_multisort is somewhat overkill here, and I'm not sure that you really need to create a copy of the array. This will do just fine:
foreach ($slice1 as &$col) {
sort($col);
}
This applies sort to each array within $slice1 by reference, and thereby orders $slice1 in place.
PHP array_multisort, as per the documentation, is for sorting multiple or multi-dimensional arrays, in your case you don't really need it.
In your case you just need sort, you can find the documentation here
$slicesort = array();
foreach ($slice1 as $col) {
sort($col);
$slicesort[] = $col;
}
$slice = array(
array(12,4,8,2,10),
array(9,7,1,10,13)
);
foreach ($slice as &$arr) {
sort($arr);
}
print_r($slice);
array_multisort return a boolean value, true for success, false otherwise.
Change your code this way:
foreach ($slice1 as $col) {
if (array_multisort($col)) {
$slicesort[] = $col;
}
}

Intersection of two arrays using common key value for comparison

I want to perform an intersection of two arrays that have different structures, but both have one key common (fid). I want a new (filtered second) array after intersection with first array. below is my code and two arrays :
first array:
Array
(
[0] => Array
(
[fid] => 1
)
[1] => Array
(
[fid] => 3
)
)
Second array:
Array
(
[0] => Array
(
[fid] => 9
[functionality] => testing
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[2] => Array
(
[fid] => 2
[functionality] => view functionality category
[funcat_id] => 1
[name] => functionality
)
[3] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
[4] => Array
(
[fid] => 4
[functionality] => edit functionality
[funcat_id] => 1
[name] => functionality
)
)
I want this Output :
Array
(
[0] => Array
(
[fid] => 1
[functionality] => add functionality
[funcat_id] => 1
[name] => functionality
)
[1] => Array
(
[fid] => 3
[functionality] => view functionality
[funcat_id] => 1
[name] => functionality
)
)
I tried this code but I'm not getting the right answer:
$result=array_intersect($array1,$array2);
//Or this also
$result=recursive_array_intersect_key($array1,$array2);
Please let me know, if any one can do this ?
I do not know if a function does exists to do this outright, but alternatively, you can just loop them instead:
$result = array();
foreach($array2 as $val2) {
foreach ($array1 as $val1) {
if($val2['fid'] == $val1['fid']) {
$result[] = $val2;
}
}
}
echo '<pre>';
print_r($result);
Sample Output
Or if you're using PHP 5.5 or greater:
$val1 = array_column($array1, 'fid');
$result = array_filter($array2, function($val2) use($val1) {
return in_array($val2['fid'], $val1);
});
foreach($array2 as $val)
{
$i=0;
foreach($array1 as $val1)
{
if($val['fid']==$val1['fid'])
{
$i++;
}
}
if($i!=0)
{
$a[]=$val;
}
}
print_r($a);

Categories