convert array into two different string php [closed] - php

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'];

Related

nested json array in php (being coverted to string) [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 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;

Convert one dimensional key-value array into two dimensional kay-value array every nth key with new key names [closed]

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 2 years ago.
Improve this question
I have an array that looks like this:
And I need to translate it into this:
Some things to know about the $startArray is that it is dynamic based on the amount of people submitted on a form. Each person always has just those three fields though (custom_12, custom_13, custom_14 aka fName, lName, email). So if there were 5 members, the 5th members fName would be the key custom_12-5 in the start array.
I've been looking around on stackoverflow for a question just like this and I was not able to find one. What would be the steps taken and possible array functions or anything else for creating the $endArray ?
There's actually a builtin function for this: https://www.php.net/manual/en/function.array-chunk.php
For example, array_chunk($startArray, 3) will give you the base for your new array, you'll just need to then iterate through it and rename the keys.
Alternatively, just iterate through the array yourself and add the values to a new array depending on the index of the current iteration.
Thanks to Charlie's advice, I came up with this.
$startArray = array(
'custom_12' => 'john',
'custom_13' => 'johny',
'custom_14' => 'john#johny.com',
'custom_12-2' => 'bob',
'custom_13-2' => 'bobby',
'custom_14-2' => 'bob#bobby.com',
'custom_12-3' => 'don',
'custom_13-3' => 'donny',
'custom_14-3' => 'don#donny.com'
);
$middleArray = array_chunk($startArray, 3);
$endArray = array_map(function($val) {
return array(
'fName' => $val[0],
'lName' => $val[1],
'email' => $val[2]
);
}, $middleArray);
echo "<pre>";
print_r($endArray);
echo "</pre>";
And the output is exactly what I wanted:

Calling Array Function [closed]

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
How do I create an input argument and call the function defined below?
function insert_data ($var) {
$set_entry_array = array(
array('name' => 'name' ,'value' => $var['phone']),
array('name' => 'email' ,'value' => $var['circle']),
array('name' => 'assigned_user_id' ,'value' => 1),
);
}
I want to call this function to pass the actual value in the name and email parameter.
Your function accepts an array. You need to define the array, and pass it to the function. It appears the function expects two array keys to be set: phone and circle. We need to set them.
$myarray = array();
$myarray['phone'] = '555-1234';
$myarray['circle'] = 'foo#bar.com';
insert_data($myarray);

PHP slice associative array [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 8 years ago.
Improve this question
This is my array
$array = array(
"13111" => "2014-06-21 19:51:00.0000000",
"23111" => "2014-06-20 19:51:00.0000000",
"12111" => "2014-06-21 19:51:00.0000000",
"23311" => "2014-06-22 19:51:00.0000000",
"13114" => "2014-06-21 19:51:00.0000000",
"23711" => "2014-06-20 19:51:00.0000000",
);
How can i get first 3 elements of my array and how can i sort by datetime? thanks
What you want is:
sort($array);
$array = array_slice($array, 0, 3);
first, the sort function will sort them lexicographically (which in this case coincides with the date) and then you slice it to get the elements you want.
EDIT
If you want to preserve the keys just use
asort($array); // "asort" instead of simple "sort"
$array = array_slice($array, 0, 3, true); // note the final "true" parameter!

PHP generate json - list of categories [closed]

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 8 years ago.
Improve this question
Hi I need to generate json code with php like this http://api.androidhive.info/contacts/
You are looking for json_encode(). Passing an associative array to it will return a JSON representation.
http://us1.php.net/json_encode
You can use an multi-dimensional array with the json_encode function to achieve this structure.
$var = array(
"contacts" => array (
array(
"id" => 1,
"name" => "foo"
),
array(
"id" => 2,
"name" => "bar"
)
)
);
echo json_encode($var);

Categories