Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
how to merge data array of to array in php?
i have 2 arrays like:
$a=[1,2]; $b =[3,4];
and i want to merge data like:
$data = [[1,3]],[2,4]];
how to code in php like array_merge or something in php or laravel?
i have try using array_merge but not my expectation data like this:
$data = array_merge($a, $b);
but data always return
[1,2,3,4]
i have clue about this, can someone help this problem?
Since we concluded in the comments that you actually want a multidimensional array, we need to create new arrays so we can swap the values around between the original arrays.
Here's one possible solution (the comments explain the different parts)
$a = [1,2];
$b = [3,4];
// Initiate a new array
$newArray = [];
// Iterate through one of the arrays
foreach ($a as $index => $value) {
// On each iteration, take the values from both arrays for that specific array index
// and add them as a new sub array.
$newArray[] = [
$a[$index],
$b[$index]
];
}
$newArray will now contain a multidimensional array:
[[1,3],[2,4]]
Here's a demo: https://3v4l.org/okHhQ
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
I have a question about the differents between the normal array code and the shortcode.
The output from the shortcode version has all the results.
But the normal code has only the last record of the data from the database.
Please is there somebody who can explain this?
Below the shortcode
<?php
while ($data = mysqli_fetch_assoc($result)) {
$output[$data['category']][] = $data['type'];
}
?>
Below the normal code
<?php
while ($data = mysqli_fetch_assoc($result)) {
//alleen laatste database result
$output = array($data['category'] => array($data['type']));
}
?>
When you assign
$output = <anything>;
you discard all the previous contents of $output, and replace it with the value of <anything>. So each time through the loop in the second version, you replace the variable with the data from the current row. When the loop is done, it just has the data from the last row.
You don't want to replace all of $output. It should be a multi-dimensional array, where the first dimension is associative with categories as keys, and the second dimension is an array of all type values in that category. Assigning to
$output[$data['category']][]
does two things:
$output[$data['category']] creates the category key in the first dimension if it doesn't already exist.
Assigning to [] pushes a new element onto the array in the second dimension.
It's equivalent to the following long code
while ($data = mysqli_fetch_assoc($result)) {
$cat = $data['category'];
if (!isset($output[$cat])) {
$output[$cat] = array();
}
array_push($output[$cat], $data['type']);
}
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 5 years ago.
Improve this question
I have two associative arrays:
$array1=[key1=>$value1,key2=>$value2,key3=>$value3];
$array2=[key1=>$value1,key2=>$value2,key4=>$value4];
How to create the following array:
$arraytotal=[key1=>$value1,key2=>$value2,key3=>$value3,key1=>$value1,key2=>$value2,key4=>$value4];
array_merge is not an option because i have to keep the same values in the final array.
Tnx.
You can't have multiple keys set to the same value (you have key1 in each array), and as you've seen, the second key1 will override the first when you use array_merge.
Your options depend on if each original array functions as a separate group of key/value pairs, or if you want similar key/value pairs grouped together.
So, the first one is easy:
$array1=[key1=>$value1,key2=>$value2,key3=>$value3];
$array2=[key1=>$value1,key2=>$value2,key4=>$value4];
$super = [$array1, $array2];
The second needs a bit more looping:
$array1=[key1=>$value1,key2=>$value2,key3=>$value3];
$array2=[key1=>$value1,key2=>$value2,key4=>$value4];
$super = [];
foreach ($array1 as $key => $value) {
$super[$key] = [$value];
}
foreach ($array2 as $key => $value) {
if (!isset($super[$key])) {
$super[$key] = [];
}
$super[$key][] = $value;
}
If you know that you'll always have the same keys in each array, then you can skip the isset
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 5 years ago.
Improve this question
How i can combine ad array of values into an array with double combination string without duplications ?
For example, if i have something like:
array('one','two','tre','four','five');
I want to obtain an array of combinations like this ('one / two') just one time and not also ('two / one').
In this way i wanto to get something similar to:
array('one/two', 'one/tre', 'one/four', 'one/five', 'two/tree','two/four' .......
Suggestions ?
You can do it with this code. It won't show two/one, three/two, etc (that's how I understood it):
<?php
$array = array('one','two','tre','four','five');
$newArray = [];
foreach ($array as $el) {
foreach ($array as $el2) {
if ($el === $el2) continue;
$newArray[] = $el."/".$el2;
}
array_shift($array); // remove the element we just went through
}
print_r($newArray);
Demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
From a mySQL row I get the below from column coop as appeared (with commas)
1,2,6,27
My question is how can I have something like
for
as numbers as the column
do the loop {
{
Assuming you have the values stored in a string, let's call it $dbValue, you can split that string into an array:
$values = explode(",", $dbValue);
Then just loop over that array:
foreach ($values as $value) {
// do something with each value
}
As an aside... Storing delimited values in a single database field is very often a bad idea. Each database field should contain one discrete value. If multiple values are needed, you'd create a separate table with a many-to-one relationship.
seems foreach
$tmp = explode(',', $yourvalue) // $yourvalue = '1,2,6,27'
foreach ( $tmp as $key => $value ) {
echo $value;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
how would I convert my array to an array of objects with PHP ?
input
[1, 2, 3]
output
[ {id:1}, {id:2}, {id:3} ]
When you have to reformat array, e.g. transorm values of the array to another format (number to object with id->number) and they map 1:1 (the new array have the same number of elements like the original), array_map is the solution
$newArray = array_map(function($item) {
$object = new \StdClass;
$object->id = $item;
return $object;
}, $array);
If you defined your object before, I guess I'd do a loop
class previouslyDefinedObject{
public $id;
}
$myArray = array(1,2,3);
$newArray = array();
foreach($myArray as $id){
$obj = new previouslyDefinedObject();
$obj->id = $id;
array_push($newArray, $obj);
}
print_r($newArray);
That way, your $newArray will contains every object in an array