Creating an php array elements nested - php

I am trying to create an php array elements to be nested.
For example:
I have this array:
array(3) {
[0]=>
string(9) "Example 1"
[1]=>
string(9) "Example 2"
[2]=>
string(9) "Example 3"
}
and I want the output to be like
array(1) {
[0]=>
string(9) "Example 1"
array(1) {
[0]=>
string(9) "Example 2"
array(1) {
[0]=>
string(9) "Example 3"
}
I tried with foreach() but without success. Can someone help me?
Thanks.

$array = array("Example 1","Example 2","Example 3");
$x = count($array) - 1;
$temp = array();
for($i = $x; $i >= 0; $i--)
{
$temp = array($array[$i] => $temp);
}
echo'<pre>';
print_r($temp);
Array
(
[Example 1] => Array
(
[Example 2] => Array
(
[Example 3] => Array
(
)
)
)
)

Related

NOt getting the array - PHP JSON Explode

I am trying to explode the JSON file below with the 2 arrays but keep getting an output of..
Array
(
[0] =>
)
Array
(
[0] =>
)
What am I doing wrong?
[0]=>
array(2) {
["question_id"]=>
string(2) "88"
["weight"]=>
string(1) "5"
}
[1]=>
array(2) {
["question_id"]=>
string(2) "89"
["weight"]=>
string(1) "5"
$quest_id = $data["question_id"];
$quest_points = $data["weight"];
$quest_id_array = explode(" ,", $quest_id);
$quest_points_array = explode(" ,", $quest_points);
print_r($quest_id_array);
print_r($quest_points_array);
Assuming $data is the array you show at the top, it's not an array containing comma-separated strings. It's a 2-dimensional array, so you need to loop over it and push the values onto the new arrays.
$quest_id_array = [];
$quest_points_array = [];
foreach ($data as $val) {
$quest_id_array[] = $val['question_id'];
$quest_points_array[] = $val['weight'];
}

Create new Array under same key from multiple Array in PHP [duplicate]

This question already has answers here:
Transpose 2d array, join second level with commas, and join first level with pipes
(5 answers)
Closed 7 months ago.
I have the following array.
$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/02/2019");
$c = array("08/01/2020", "08/02/2019");
print_r($a);
print_r($b);
print_r($b);
and the output is
Array(
[0] => Algebra
[1] => Arithmetic
)
Array(
[0] => 08/01/2020
[1] => 08/01/2019
)
Array(
[0] => 08/02/2020
[1] => 08/02/2019
)
And I want Array in the following structure.
Array(
[0] => Algebra,08/01/2020,08/02/2020
[1] => Arithmetic,08/01/2019,08/02/2019
)
I have tried $results = array_merge_recursive($a, $b, $c); but its not giving desire output.
Thanks for help in advance.
A simple foreach loop will suffice
$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/02/2019");
$c = array("08/01/2020", "08/02/2019");
foreach( $a as $i=>$v ) {
$new[] = sprintf( '%s,%s,%s', $v, $b[$i], $c[$i] );
}
Firstly, when you find yourself using sequentially-named variables it almost always means that they should actually be an array:
$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/02/2019");
$c = array("08/01/2020", "08/02/2019");
$better_array = [$a, $b, $c];
var_dump($better_array);
Output:
array(3) {
[0]=>
array(2) {
[0]=>
string(7) "Algebra"
[1]=>
string(10) "Arithmetic"
}
[1]=>
array(2) {
[0]=>
string(10) "08/01/2020"
[1]=>
string(10) "08/02/2019"
}
[2]=>
array(2) {
[0]=>
string(10) "08/01/2020"
[1]=>
string(10) "08/02/2019"
}
}
Once they're in an proper array you can use array_column()
$out = [];
for($i=0, $c=count($better_array[0]); $i < $c; ++$i) {
$out[] = array_column($better_array, $i);
}
var_dump($out);
Output:
array(2) {
[0]=>
array(3) {
[0]=>
string(7) "Algebra"
[1]=>
string(10) "08/01/2020"
[2]=>
string(10) "08/01/2020"
}
[1]=>
array(3) {
[0]=>
string(10) "Arithmetic"
[1]=>
string(10) "08/02/2019"
[2]=>
string(10) "08/02/2019"
}
}
And if that comma-delimited string is what you actually want, then use implode():
$out = [];
for($i=0, $c=count($better_array[0]); $i < $c; ++$i) {
$out[] = implode(',', array_column($better_array, $i));
}
var_dump($out);
Output:
array(2) {
[0]=>
string(29) "Algebra,08/01/2020,08/01/2020"
[1]=>
string(32) "Arithmetic,08/02/2019,08/02/2019"
}
Lastly, you should avoid print_r() as it tends to produce misleading output. Eg: https://3v4l.org/ThSLb
You don't get anything built-in for this purpose. You need to build a custom function for this. You can try this-
<?php
$a = array("Algebra", "Arithmetic");
$b = array("08/01/2020", "08/01/2019");
$c = array("08/02/2020", "08/02/2019");
function mergeAssoc()
{
// You can get variable number of arguments|array by this.
$args = func_get_args();
$master = array();
foreach ($args as $arg)
{
foreach ($arg as $i => $v)
{
$master[$i][] = $v;
}
}
return $master;
}
$res = mergeAssoc($a, $b, $c);
print_r($res);
Note: It will return a multidimensional array. Not an array of comma-separated values.
Output:
Array
(
[0] => Array
(
[0] => Algebra
[1] => 08/01/2020
[2] => 08/02/2020
)
[1] => Array
(
[0] => Arithmetic
[1] => 08/01/2019
[2] => 08/02/2019
)
)
and if we use foreach then our desire output will be there with array separated by comma.
foreach ($res as $key => $value) {
$result[] = implode(',', $value);
}
and output of print_r($result); is
Array
(
[0] => Algebra,08/01/2020,08/02/2020
[1] => Arithmetic,08/01/2019,08/02/2019
)

How to fill a empty value in KEY=> VALUE for array_combine in php

I wish to make Key and Value combining with 2 arrays, but both arrays are not equal.
$array1 = array("1","2","3","4","5");
$array2 = array("apple","banana","","dog","");
$key_value = array_combine($array1,$array2);
The output is:
array_combine(): Both parameters should have an equal number of elements
But I need to below output be like
print_r($key_value);
array(5) {
[1]=> string(5) "apple"
[2]=> string(6) "banana"
[3]=> string(8) "No Value"
[4]=> string(3) "dog"
[5]=> string(8) "No Value"
}
How can do this if null, insert "no value" text.
You can do it via foreach loop:
$res = [];
foreach($array1 as $ind=>$num){
$res[$num] = $array2[$ind] === "" ? "No Value" : $array2[$ind];
}
print_r($res);
Output:
Array
(
[1] => apple
[2] => banana
[3] => No Value
[4] => dog
[5] => No Value
)
Demo
use array_map() and array_combine()
<?php
$array1 = array("1","2","3","4","5");
$array2 = array("apple","banana","","dog","");
$array2 = array_map(function($v){
return (empty($v)) ? "No Value" : $v;
},$array2);
$key_value = array_combine($array1,$array2);
print_r($key_value);
https://3v4l.org/CY4ku

Combine multiple arrays with identica keys and different values

I have this two arrays that are generated in two foreach loops and I want to set the first array as keys and the second one as values.
after I use this code
foreach ($difference AS $j) {
$fv = $cate->getFilterValueByFeatureID($j);
foreach ($fv AS $z) {
$array = array(
$j => $z
);
var_dump($array);
}
}
this is what I get
array(1) {
[6]=>
int(15)
}
array(1) {
[6]=>
int(20)
}
array(1) {
[8]=>
int(26)
}
array(1) {
[8]=>
int(27)
}
array(1) {
[8]=>
int(33)
}
and I want this result
array(1){
[6] => array(
[0] => 15
[1] => 20
)
array(1){
[8] => array(
[0] => 26
[1] => 27
[2] => 33
)
Like this (untested)
$result = [];
foreach ($difference AS $j) {
$fv = $cate->getFilterValueByFeatureID($j);
foreach ($fv AS $z) {
if(!isset($result[$j])) $result[$j] = [];
$result[$j][] = $z;
}
}
var_dump($result);

need to join arrays in to single one [duplicate]

This question already has answers here:
Combine two arrays
(11 answers)
Closed 5 months ago.
how can we join this two arrays into one array
for this, I had done my code like this and got the output as shown below
$groups_array = array_map('trim',explode(',', $tt));
$tt looks like this string(5) "11:00" string(5) "10:00"
array(1) { [0]=> string(5) "11:00" } array(1) { [0]=> string(5) "10:00" }
need desired output to look like
array(1) { [0]=> string(5) "11:00",[1]=> string(5) "10:00" }
My code is here please have a look
<?php $time_booked=$this->Hospital_model->get_already_booked_time($d,$timeslot->doctor_id);
foreach($time_booked as $index1=> $t) {
$tt=$t->time;
$groups_array = array_merge(array_map('trim',explode(',', $ttt)));
} ?>
my var_dump($time_booked) looks like this
array(2) { [0]=> object(stdClass)#40 (1) { ["time"]=> string(5) "11:00" } [1]=> object(stdClass)#41 (1) { ["time"]=> string(5) "10:00" } }
What about array_merge() with array_map()
$groups_array = array_merge(array_map('trim',explode(',', $tt)));
Output:-https://eval.in/1012484
By looking your edit in your question no need to do any extra stuff, just create an array and add values to it
<?php
$groups_array = []; //create array
$time_booked=$this->Hospital_model->get_already_booked_time($d,$timeslot->doctor_id);
foreach($time_booked as $index1=> $t) {
$groups_array[] =$t->time; //add values to array
}
var_dump($groups_array);
?>
What about array_merge ? That should give you the result.
http://php.net/manual/de/function.array-merge.php
EDIT:
$tt = ['11:00'];
$tt2 = ['10:00'];
$result = array_merge($tt,$tt2);
var_dump($result);
Result is
array(2) {
[0]=>
string(5) "11:00"
[1]=>
string(5) "10:00"
}
Is that not what you meant ?
Suppose you have two array which look like
$array1 = array(0 => "10:00 am");
$array2 = array(0 => "11:00 am");
and you want to join and want output like: Array ( [0] => 10:00 am [1] => 11:00 am )
then you can use array_merge option
$array3 = array_merge($array1, $array2);
If you print print_r($array3);
output will be
Array ( [0] => 10:00 am [1] => 11:00 am )

Categories