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'];
}
Related
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
Please help me to combine four arrays in one.
I have to compare main array keys and if the key of the secondaries arrays coincides, add them to the new array.
I tried to combine different php functions, but without the expected result and nothing relevant.
This is the main array.
[0] => Array
(
[1] => "Category1"
[2] => "Category2"
[3] => "Category3"
[4] => "Category4"
)
These are the secondaries arrays
[1] => Array
(
[1] "user_test_[1]" key 1 map to --> [1] Category1
[2] "user_test_[2]" key 2 map to --> [2] Category2
[3] "user_test_[3]" key 3 map to --> [3] Category3
[4] "user_test_[4]" key 4 map to --> [4] Category4
)
[2] => Array
(
2: "user_prod_[2]" key 2 map to --> [2] Category2
4: "user_prod_[4]" key 4 map to --> [4] Category4
)
[3] => Array
(
[3] "user_uat_[3]" key 3 map to --> [3] Category3
[4] "user_uat_[4]" key 4 map to --> [4] Category4
)
New array results in php format:
$array_result = array(
'Category1'=>array(
'user_test_[1]'
),
'Category2'=>array(
'user_test_[2]',
'user_prod_[2]'
),
'Category3'=>array(
'user_test_[3]',
'user_uat_[3]'
),
'Category4'=>array(
'user_test_[4]',
'user_prod_[4]',
'user_uat_[4]'
)
);
Main array in in php format:
$array_cat = array(1=>"Category1", 2=>"Category2", 3=>"Category3", 4=>"Category4");
These are the secondaries arrays in php fromat:
$array_usrtest = array(1=>"user_test_[1]", 2=>"user_test_[2]", 3=>"user_test_[3]", 4=>"user_test_[4]");
$array_usruat = array(3=>"user_uat_[3]", 4=>"user_uat_[4]");
$array_usrprod = array(2=>"user_prod_[2]", 4=>"user_prod_[4]");
Expected results
array{
["Category1"]=>array(1)
{
[0]=>"user_test_[1]"
}
["Category2"]=>array(2)
{
[0]=>"user_test_[2]"
[1]=>"user_prod_[2]"
}
["Category3"]=>array(3)
{
[0]=>"user_test_[3]"
[2]=>"user_uat_[3]"
}
["Category4"]=>array(3)
{
[0]=>"user_test_[4]"
[1]=>"user_prod_[4]"
[2]=>"user_uat_[4]"
}
}
Here is simple solution, which it's not very good, because it is solution for your specific values, but it is good start point and if you want, you can improve it:
<?php
$array_cat = array(1=>"Category1", 2=>"Category2", 3=>"Category3", 4=>"Category4");
$array_usrtest = array(1=>"user_test_[1]", 2=>"user_test_[2]", 3=>"user_test_[3]", 4=>"user_test_[4]");
$array_usruat = array(3=>"user_uat_[3]", 4=>"user_uat_[4]");
$array_usrprod = array(2=>"user_prod_[2]", 4=>"user_prod_[4]");
$result = [];
foreach ($array_cat as $k => $v) {
$result[$v] = []; // fill result array with categories
}
$values = array_merge($array_usrtest, $array_usruat, $array_usrprod); // merge arrays into one (all items)
foreach ($values as $k => $value) { // iterate over all values
preg_match('!\d+!', $value, $match); // get index in [index]
$result['Category' . $match[0]][] = $value; // append values to category
}
echo '<pre>';
var_dump($result);
Result:
array(4) {
["Category1"]=>
array(1) {
[0]=>
string(13) "user_test_[1]"
}
["Category2"]=>
array(2) {
[0]=>
string(13) "user_test_[2]"
[1]=>
string(13) "user_prod_[2]"
}
["Category3"]=>
array(2) {
[0]=>
string(13) "user_test_[3]"
[1]=>
string(12) "user_uat_[3]"
}
["Category4"]=>
array(3) {
[0]=>
string(13) "user_test_[4]"
[1]=>
string(12) "user_uat_[4]"
[2]=>
string(13) "user_prod_[4]"
}
}
I would say merge all array and then build the category array
$arrs = array_merge($array_usrtest, $array_usruat, $array_usrprod);
$categories = array_fill_keys($array_cat, []);
array_walk($arrs, function($item) use (& $categories){
preg_match('#(.*)\[(\d)\]#', $item, $matches);
$key = $matches[2];
$categories['Category' . $key][] = $item;
});
print_r($categories);
Based on yours answers:
<?php
$array_cat = array(1=>"Category1", 2=>"Category2", 3=>"Category3", 4=>"Category4");
$array_usrtest = array(1=>"user_test_[1]", 2=>"user_test_[2]", 3=>"user_test_[3]", 4=>"user_test_[4]");
$array_usruat = array(3=>"user_uat_[3]", 4=>"user_uat_[4]");
$array_usrprod = array(2=>"user_prod_[2]", 4=>"user_prod_[4]");
$values = array_merge($array_usrtest, $array_usruat, $array_usrprod);
$result = [];
foreach ($array_cat as $key => $cat) {
foreach ($values as $k => $value) { // iterate over all values
preg_match('!\d+!', $value, $match); // get index in [index]
if ($key == $match[0]) {
$result[$cat][] = $value; //append values to category
}
}
}
echo '<pre>';
var_dump($result);
?>
Result:
array(4) {
["Category1"]=>
array(1) {
[0]=>
string(13) "user_test_[1]"
}
["Category2"]=>
array(2) {
[0]=>
string(13) "user_test_[2]"
[1]=>
string(13) "user_prod_[2]"
}
["Category3"]=>
array(2) {
[0]=>
string(13) "user_test_[3]"
[1]=>
string(12) "user_uat_[3]"
}
["Category4"]=>
array(3) {
[0]=>
string(13) "user_test_[4]"
[1]=>
string(12) "user_uat_[4]"
[2]=>
string(13) "user_prod_[4]"
}
}
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
(
)
)
)
)
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 )
This question already has answers here:
PHP unique array by value?
(6 answers)
Closed 6 years ago.
I have a PHP array that looks like this,
[["a","b"],["e","j"],["a","s"]]
I need it to look like this,
[["a","b"],["e","j"]]
or this,
[["e","j"],["a","s"]]
I cannot have two inner arrays that contain the same "0" index. It does not matter which inner array is deleted as long as only one remains. How can I go through this array and remove inner arrays that contain the same "0" index?
Thanks!
You could simply loop through the array with two loops. Let's say this is your data:
$data = array(
array('a', 'b'),
array('e', 'j'),
array('a', 's'),
array('a', 't'),
array('c', 't'),
array('a', 'h'),
array('c', 'e'),
array('f', 'g')
);
Then you go ahead and loop through everything, and unset it if it's the same value.
$count = count($data);
foreach($data as $index => $array){
for($i=$index + 1; $i<$count; $i++)
if(isset($array[0]) && isset($data[$i][0]) && $array[0] == $data[$i][0])
unset($data[$i]);
}
The var_dump of $data after the loops would be:
array(4) {
[0]=>
array(2) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
}
[1]=>
array(2) {
[0]=>
string(1) "e"
[1]=>
string(1) "j"
}
[4]=>
array(2) {
[0]=>
string(1) "c"
[1]=>
string(1) "t"
}
[7]=>
array(2) {
[0]=>
string(1) "f"
[1]=>
string(1) "g"
}
}
<?php
$array = [["a","b"],["e","j"],["a","s"]];
$values = array();
foreach ($array as $key => $arrayChild) {
foreach ($arrayChild as $val) {
if (in_array($val, $values)) {
unset($array[$key]);
}
$values[] = $val;
}
}
print_r($array);
?>
Result:
Array (
[0] => Array ( [0] => a [1] => b )
[1] => Array ( [0] => e [1] => j )
)