I see some questions but no work to me.
In a for loop i receive an array like that:
array(1) { [0]=> array(1) { [0]=> string(1) "4" } }
array(1) { [0]=> array(2) { [0]=> string(1) "3" [1]=> string(1) "4" } }
array(1) { [0]=> array(7) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(2) "30" [3]=> string(2) "43" [4]=> string(2) "65" [5]=> string(2) "53" [6]=> string(3) "634" } }
I need implode that values with "-", my desired output isa string:
4
3-4
2-30-43-65-53-634
I try some ways, but no work, some ideia for do it simple?
If it is a two dimensional array and would like to output all elements, you could use a foreach loop and output the implode of each like so:
$mainArray = [
[4],
[3, 4],
[2, 30, 43, 65, 53, 634]];
foreach($mainArray as $key => $secArray){
echo implode('-', $secArray) . '<br/>';
}
PHP Implode
Notice the return type of implode is a string.
Related
i want the last index value of each array for example 8 and 10 will be swap
array(2) {
[0]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "5"
[3]=>
string(1) "8"
}
[1]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "6"
[3]=>
string(2) "10"
}
}
so the result will be
array(2) {
[0]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "5"
[3]=>
string(2) "10"
}
[1]=>
array(5) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "6"
[3]=>
string(1) "8"
}
}
if there are two value or more (even, not odd) will be swap its okay, as long as not all of the value will be swap. the size of array will be dynamic, thats just for example.
it swaps the last value:
$arr1 = array(1, 3, 5, 8);
$arr2 = array(1, 3, 6, 10);
$val1 = end($arr1);
$val2 = end($arr2);
array_pop($arr1);
array_pop($arr2);
array_push($arr1, $val2);
array_push($arr2, $val1);
print_r($arr1);
print_r($arr2);
I have two arrays:
array(3)
{
[0]=>
string(1) "1"
[1]=>
string(1) "1"
[2]=>
string(1) "2"
}
array(3)
{
[0]=>
string(1) "abc"
[1]=>
string(1) "def"
[2]=>
string(1) "ghi"
}
Is there any way I could put the first array's elements into the second array's index producing the following result :
array(3)
{
[1]=>
string(1) "abc"
[1]=>
string(1) "def"
[2]=>
string(1) "ghi"
}
How can I do this in PHP? Thanks in advance.
Since the expected output is impossible the next best thing it to make a multidimensional array where the question id is a subarray for answers.
$arr1 = [1,1,2];
$arr2 = ["abc","def","ghi"];
Foreach($arr1 as $key => $id){
$threads[$id][] = $arr2[$key];
}
Var_dump($threads)
Outputs:
array(2) {
[1]=>
array(2) {
[0]=> "abc"
[1]=> "def"
}
[2]=>
array(1) {
[0]=> "ghi"
}
}
https://3v4l.org/qpJDA
You can use "array_combine" method for this purpose.
http://php.net/array-combine
However, you cannot have same multiple indexes in an array
array(3)
{
[1]=> "abc"
[1]=> "def"
[2]=> "ghi"
}
You have index "1" twice in array. So "def" will replace "abc" in your array
How can I fetch the value "3" from this set of arrays:
array(1) { [0]=> string(1) "1" }
array(1) { [0]=> string(1) "3" }
array(1) { [0]=> string(1) "0" }
The arrays are output from a foreach statement of parenting array, which is:
array(3) { [0]=> string(8) "St" [1]=> string(1) "1" [2]=> string(1) "0" }
array(3) { [0]=> string(16) "Fu" [1]=> string(1) "3" [2]=> string(1) "0" }
array(3) { [0]=> string(13) "Pa" [1]=> string(1) "0" [2]=> string(1) "0" }
Where I am going for the second line value: "Fu" [1]=> string(1) "3"
Maybe I am doing it wrong from the first array?
You're not giving us much to go on. Are the 3 arrays already in a parent array, in an object, etc.? Below is how to get the # 3 from the 3 arrays...but I'm guessing this is not actually what you are asking, we likely need much more detail...the real problem you are trying to solve.
function getThree($arr1, $arr2, $arr3) {
$array = array();
$array[] = $arr1;
$array[] = $arr2;
$array[] = $arr3;
foreach( $array AS $subArray ) {
// whichever condition works for you
if( $subArray[0] == 'Fu' || $subArray[1] == 3 ) {
return $subArray;
}
}
}
This question already has answers here:
How to insert element into arrays at specific position?
(25 answers)
Insert new item in array on any position in PHP
(23 answers)
Closed 9 years ago.
I have an array
array(1) {
[0]=>
array(4) {
[0]=>
string(1) "1"
[1]=>
string(2) "10"
[2]=>
string(3) "100"
[3]=>
string(3) "200"
}
}
I want to insert two element into the array which must be the 3rd and last element.
Output:
array(6) {
[0]=>
array(6) {
[0]=>
string(1) "1"
[1]=>
string(2) "10"
[2]=>
string(1) ""
[3]=>
string(3) "100"
[4]=>
string(3) "200"
[5]=>
string(1) ""
}
}
how can i do this?
What I have tried
array_splice($input,3 ,0,"");
Then result become like this, the array not added in the middle
array(6) {
[0]=>
array(6) {
[0]=>
string(1) "1"
[1]=>
string(2) "10"
[2]=>
string(1) ""
[3]=>
string(3) "100"
[4]=>
string(3) "200"
[5]=>
string(1) ""
}
[1]=>
array(1) {
[0]=>
string(1) ""
}
}
To insert in the middle of array, you can use array_splice with a length of 0.
array_splice($input, 3, 0, "");
To add to the array, you can use either array_push or [] operator
By using array_splice you can insert element inside the array
$array = [0 => 'Data', 1 => 'data2', 2=> 'data3'];
array_splice($array, 1, 0, 'data append');
var_dump($array);
i have the following array:
["addToCart"]=>
array(3) {
[1]=>
array(5) {
["aantal"]=>
int(1)
["film_id"]=>
string(1) "1"
["zaal_id"]=>
string(1) "1"
["dag"]=>
string(7) "maandag"
["seats"]=>
array(4) {
[0]=>
string(2) "67"
[1]=>
string(2) "68"
[2]=>
string(2) "69"
[3]=>
string(2) "70"
}
}
You can see that i have an array called "seats" inside the "addToCart" array.There are 4 items in the "seats" array.
what i would like to have is 4 separate arrays, they should all have the same content but each of them needs to have 1 value of "seats".
I'm not sure I got exactly what you're looking to do, but this would result in an array of arrays where each has only one seat:
$seatArrays = array();
foreach ($addToCart as $arr)
{
foreach ($arr["seats"] as $seat)
{
$seatArr = $arr; // Copy the original array
$seatArr["seats"] = $seat; // Replace the "seats" subarray with the current seat
$seatArrays[] = $seatArr;
}
}