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
)
)
Related
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
)
)
I'm looking to write a function which creates all permutation of a list of arrays (The list is dynamical). Now I found 2 articles, http://dannyherran.com/2011/06/finding-unique-array-combinations-with-php-permutations/ and Finding cartesian product with PHP associative arrays. But I don't want to store them as multiple arrays, I want to add each array to each possibility so I can use them later.
In fact I want to multiply each array with the other.
For example:
$array = array(
array(
1,
2
),
array(
'A',
'B',
'C'),
array(
'I',
'II')
);
In this form:
Array
(
[0] => Array
(
[0] => 1
[1] => Array
(
[0] => Array
(
[0] => A
[1] => Array
(
[0] => I
[1] => II
)
)
[1] => Array
(
[0] => B
[1] => Array
(
[0] => I
[1] => II
)
)
[2] => Array
(
[0] => C
[1] => Array
(
[0] => I
[1] => II
)
)
)
)
[1] => Array
(
[0] => 2
[1] => Array
(
[0] => Array
(
[0] => A
[1] => Array
(
[0] => I
[1] => II
)
)
[1] => Array
(
[0] => B
[1] => Array
(
[0] => I
[1] => II
)
)
[2] => Array
(
[0] => C
[1] => Array
(
[0] => I
[1] => II
)
)
)
)
)
I think this big example made my problem clear. For this type of array I created a function:
foreach ($array[1] as $value) {
$return1[] = array($value, $array[2]);
}
foreach ($array[0] as $value) {
$return[] = array($value, $return1);
}
print_r($return);
Now I want to create this function inside a recursive function (so it becomes dynamical) but I got stuck. I wanted to pass the amount of arrays to the function and then iterate.
function createTree($array, $loops=3){
$b = $array[$loops-2];
foreach ($b as $v) {
$return[] = array($v, createTree($return, $loops-1));
}
print_r($return);
}
Maybe there is a total other solution to multiply the arrays? But the function which isn't recursive is easy for me, but making it recursive...
Thanks for your help
function createTree($array){
switch(count($array)) {
case 0:
die('Illegal argument.');
case 1:
return $array[0];
default:
$lastArray = array_pop($array);
$subArray = createTree($array);
foreach ($lastArray as $item) {
$return[] = array($item, $subArray);
}
return $return;
}
}
var_dump(createTree(array_reverse($array)));
This question already has answers here:
Merge row data from multiple arrays
(6 answers)
Closed 5 months ago.
here: Transforming array values in elements of a subarray using PHP I asked quite the same, but the arrays were flatter. I tried to adapt the code, but unfortunately without success.
How could I merge following arrays so the second array won't be added after the end of the first array, but each subarray of the first array will receive the correspondent values of subarrays of the second. In other words, I want to merge the subarrays. Here my example:
Array 01:
Array01 (
[0] => Array
(
[0] => 40292633
[1] => 412
)
[1] => Array
(
[0] => 41785603
[1] => 382
)
[2] => Array
(
[0] => 48792980
[1] => 373
)
[3] => Array
(
[0] => 44741143
[1] => 329
))
Array 02:
Array02(
[0] => Array
(
[0] => 3460581
[1] => 1407424B1
[2] => 951753
)
[1] => Array
(
[0] => 3484251
[1] => 1028325B1
[2] => 159357
)
[2] => Array
(
[0] => 3519102
[1] => 0586365A1
[2] => 456654
)
[3] => Array
(
[0] => 3529714
[1] => 1059876A1
[2] => 852258
))
Final array:
finalArray(
[0] => Array
(
[0] => 40292633
[1] => 412
[2] => 3460581
[3] => 1407424B1
[4] => 951753
)
[1] => Array
(
[0] => 41785603
[1] => 382
[2] => 3484251
[3] => 1028325B1
[4] => 159357
)
[2] => Array
(
[0] => 48792980
[1] => 373
[2] => 3519102
[3] => 0586365A1
[4] => 456654
)
[3] => Array
(
[0] => 44741143
[1] => 329
[2] => 3529714
[3] => 1059876A1
[4] => 852258
))
Many thanks in advance!
try this code
function merge_arrays($a1, $a2) {
return array($a1, $a2);
}
$result = array_map("merge_arrays", $a1, $a2);
foreach($result as $nr)
{
$nres[] = array_merge ($nr[0], $nr[1]);
}
Try this:
$result = array();
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
foreach($keys as $key) {
if( !array_key_exists($key, $arr1) ) {
$result[$key] = $arr2;
} else if( !array_key_exists($key, $arr2) ) {
$result[$key] = $arr1;
} else {
$result[$key] = array_merge($arr1[$key], $arr2[$key]);
}
}
It does not assume that both arrays have the same keys.
If you'd like to use array_map, this is an equivalent solution:
$keys = array_unique( array_merge(array_keys($arr1), array_keys($arr2)) );
$result = array_map(function($key) use ($arr1,$arr2) {
if( !array_key_exists($key, $arr1) ) {
return $arr2;
} else if( !array_key_exists($key, $arr2) ) {
return $arr1;
}
return array_merge($arr1[$key], $arr2[$key]);
},
$keys);
If you're sure both arrays have the same keys, you can try this:
$result = array();
foreach($arr1 as $key => $arr1val) {
$result[$key] = array_merge($arr1val, $arr2[$key]);
}
I have this array:
SimpleXMLElement Object
(
[id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
I need create some php function that help me group the values like this:
[0] => Array
(
[0] => Koala.jpg
[1] => koaladesc
[2] => 1
)
[1] => Array
(
[0] => Jellyfish
[1] => jelly desc
[2] => 5
)
Could anyone help me?
Something like this should do the trick, but it's localized to what you're asking based on the vagueness of your question:
$new_array = array();
foreach($simple_xml_object as $obj) {
if(is_array($obj)) {
for($i = 0; $i < count($obj); $i++) {
$new_array[$i][] = $obj[$i];
}
}
}
I would suggest looking at the documentation on the foreach() construct, as well as looking over the SimpleXML manual.
So, you want to tranpose an array. Here's a magical way of transposing rectangular arrays:
array_unshift($array, null);
$array = call_user_func_array('array_map', $array);
let's suppose your array is saved in variable $arrayValues
$arrayValues = [id] => Array
(
[0] => Koala.jpg
[1] => Jellyfish.jpg
)
[desc] => Array
(
[0] => koaladesc
[1] => jelly desc
)
[qtidade] => Array
(
[0] => 1
[1] => 5
)
now you need to create the following code:
foreach($arrayValues as $array)
{
echo $array->id;
echo $array->desc;
echo $array->qtidade;
}
this may work well for you.
username(
[0] => 'andrew';
[1] => 'teddy';
[2] => 'bear';
)
email(
[0] => 'andrew#andrew.com';
[1] => 'teddy#teddy.com';
[2] => 'bear#bear.com';
)
I got 2 Array coming in from post. I am processing this with PHP.
I would like to combine the array so it looks like this.
So I can use a loop on the array to insert a query on a database.
[1] => Array (
[0] => 'andrew';
[1] => 'andrew#andrew.com';
)
[2] => Array (
[0] => 'teddy';
[1] => 'teddy#teddy.com';
)
[3] => Array (
[0] => 'bear';
[1] => 'bear#bear.com';
)
Take a look at array_combine()
If that doesn't solve your problem, you can always just go with a simple loop:
foreach($usernameArray as $k=>$val)
{
if(array_key_exists($k, $emailArray))
{
$combinedArray[$k] = array($val, $emailArray[$k]);
}
}
You need something like:
$res = array ();
for($i=0;$i<count($username);$i++) {
$res[$i][0] = $username[$i];
$res[$i][1] = $email[$i];
}