nested json array in php (being coverted to string) [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
$arr = array (
array(
'type' => 'Tea', 'amount' => $expense_tea, 'desp' => $expense_tea_desc
),
array(
'type' => 'BusFare', 'amount' => $expense_bus, 'desp' => $expense_bus_desc
),
array(
'type' => 'Food', 'amount' => $expense_food, 'desp' => $expense_food_desc
)
);
$myObj1->expensedetails = json_encode($arr);
$json1 = json_encode($myObj1);
Description
I have tried to create a nested json array using php
The Output:
{
"expensedetails": {
"[{\"type\":\"Tea\",\"amount\":\"0\",\"desp\":\"0\"},{\"type\":\"BusFare\",\"amount\":\"0\",\"desp\":\"0\"},{\"type\":\"Food\",\"amount\":\"0\",\"desp\":\"0\"}]"
}
}
Explanation
The json has been converted to string
Expected Output
{
"expensedetails":
[
{"type":"Tea","amount":"0","desp":"0"},
{"type":"BusFare","amount":"0","desp":"0"},
{"type":"Food","amount":"0","desp":"0"},
{"type":"SalaryAdvance","amount":"0","desp":"0"},
{"type":"OT","amount":"0","desp":"0"},
{"type":"IceFlakes","amount":"0","desp":"0"}
]
}
Conclusion
I need a code like in the above-expected code output
But when I tried to do nested json array

Your problem is this:
$myObj1->expensedetails = json_encode($arr);
which sets expensedetails to a string that is already encoded as JSON.
If you want it to be a nested array in the JSON, it needs to be a nested array in the PHP - not an already-encoded string. Just do this:
$myObj1->expensedetails = $arr;

Related

PHP array_reduce(): getting an error 'invalid function name' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I got an error while using array_reduce(). I think syntax is correct.
This function returns sum of numbers in an array.
function sum($el1, $el2){
if(!isset($return_sum))
$return_sum = 0;
$return_sum = $return_sum + ($el1+$el2);
return $return_sum;
}
$sum = array_reduce($months_data, 'sum');
Error: array_reduce() expects parameter 2 to be a valid callback, function 'sum' not found or invalid function name.
$months_data:
Array
(
[201905] => 2
[201906] => 7
[201907] => 1
[201908] => 6
[201909] => 2
[201911] => 14
[201912] => 6
[202001] => 5
[202002] => 8
[202003] => 7
)
I'm assuming your function sum, is inside a class, i.e. its the class's method. You should try this:
$sum = array_reduce($months_data, array($this,"sum"));

convert array into two different string php [closed]

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 4 years ago.
Improve this question
I have an array with following elements:
$arr = array(
'nick' => "blabla",
'pass => "blabla2"'
);
I would like to convert it somehow to strings, the first string would be the value of nick - "blabla", the second string would be the value of pass - "blabla2"
Thank you.
If you want to convert elements of the array to separate string variables you can use extract function to import elements from an array to variables. For example:
$arr = array(
'nick' => "blabla",
'pass' => "blabla2"
);
extract($arr);
echo $nick, ' ', $pass;
$arr = [
'nick' => "blabla",
'pass' => "blabla2"
];
$string_one=$arr['nick'];
$string_two=$arr['pass'];

PHP inserting values bug [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I dont understand what is wrong with this array and why it does not work, when any other times it worked...
Its simple, i do a foreach loop and insert values to an array:
$insert = [];
foreach ($csv as $i=>$row) {
$insert[$i] = [
'id_customer' => $row[0],
'id_shop_group' => $row[1],
`id_shop` => $row[2],
];
}
The array generated is:
0 =>
array (size=3)
'id_customer' => string '14' (length=2)
'id_shop_group' => string '1' (length=1)
'' => string '1' (length=1)
I dont understand... I am creating my own keys, it should be added to the array, but it isnt... what is the problem?
Your mistake is in the use of the wrong '. In the third array key you used ` instead of '.

Needing PHP to show multiple rows, but it is only showing me the last row [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The output from the code below only shows me the last row of my data. Any ideas? I want it to show me all rows.
Here is the code:
$FinalSet = array();
while ($ResultSet = mysqli_fetch_array($Query)){
$FinalSet = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5],
);
}
echo json_encode($FinalSet);
You have to add them to your array. At the moment you are replacing the array itself.
You can do this by using
$FinalSet[] = array(
instead of
$FinalSet = array(
You can also use array_push, which does the same:
array_push($FinalSet, array(......));
You are already initialized $FinalSet = array(); and you are storing data single index so your data has been override and you found last row your data. Now you can use $FinalSet[] instead of $FinalSet. You will get all data.
Try this, You can use $FinalSet[] or array_push() to push the array content into the main array. Currently, you are jut replacing the same array respective of the loop counts. So you getting the last array value in the variable.
$FinalSet[] = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5]
);
instead of
$FinalSet = array(
'idExam' => $ResultSet[0],
'SubjectName' => $ResultSet[1],
'ExamTime' => $ResultSet[2],
'ExamDate' => $ResultSet[3],
'IntakeCode' => $ResultSet[4],
'Scope' => $ResultSet[5],
);
Try the following in the loop:
while (...) {
$FinalSet[] = array(...);
}

Sort PHP Numerically & Identify which is closest to date [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got an array for events on my site that looks like this:
array(
'title' => 'Name',
'link' => 'http://www.eventssite.com',
'image' => '_img/event_img.jpg',
'location' => 'Florida, US',
'year' => '2013',
'date' => 'Dec. 12-14',
'desc' => 'Description about the event.',
'dateid' => '1212013'
),
I'd like to sort the array before the foreach by the dateid so that they show in the proper Date order.
Additionally I'm trying to identify which one of the events is closest to the actual date, as I am using a carousel type system that needs to know which to display first.
I've researched usort and am not able to get it to go on my own, Thank you for any assistance on these!
Using this function: http://php.net/usort
An example would be something like:
<?php
//just an array of arrays with the date as one of the values of the array
$array = array(
array(
'date' => '05/02/1988',
'name' => 'Jacob'
),
array(
'date' => '12/12/1968',
'name' => 'Sherry'
),
array(
'date' => '05/15/1978',
'name' => 'Dave'
)
);
//usort is used for non conventional sorting.
//which could help in this case
//NOTICE - we are not setting a variable here!
//so dont call it like $array = usort(...) you will just be setting $array = true
usort($array,'sortFunction');
//display the results
var_dump($array);
//function called by usort
function sortFunction($a,$b){
//turn the dates into integers to compare them
//
$strA = strtotime($a['date']);
$strB = strtotime($b['date']);
//don't worry about sorting if they are equal
if($strA == $strB){
return 0;
}
else{
//if a is smaller than b, the move it up by one.
return $strA < $strB ? -1 : 1;
}
}
?>
(in case youre interested, line 40 is called a Ternary)
edited for clarity

Categories