I have data in double array in php. Now I need to store that array data into string. How can I do that in php? I am new to php. Please help me in this regard.
Array is as follows:
Array ( [0] => Array ( [id] => 100 [type] => test1 [count] => 8 [count2] => 8)
[1] => Array ( [id] => 103 [type2] => test2[count2] => 3 [count3] => 3 ))
You can use implode() to convert an array into a string: http://php.net/manual/en/function.implode.php
Related
I'm trying to get the value of a specific key in an array or string format, but I can't seem to figure out how to make it work.
So, this is an example of an array I'm working with, for example, I'm trying to get all the product_id's in a single array or string format.
Like output productIdArray[] = ['34441', '34442' , '34444'];
Tried achieving this using for each but, Is there a better way to do that?
Any help would be appreciated, Thanks!
(
[0] => ( Array
(
[id] => 1333708
[abc_id] => 429084
[test_id] => 58291
[order_id] => 2222
[product_id] => 34441
)
)
[1] => ( Array
(
[id] => 1333708
[abc_id] => 429084
[test_id] => 58291
[order_id] => 2222
[product_id] => 34442
)
)
[2] => ( Array
(
[id] => 1333708
[abc_id] => 429084
[test_id] => 58291
[order_id] => 2222
[product_id] => 34444
)
)
)
Simply use array_column
$res = array_column($arr,'product_id');
This question already has answers here:
How to Flatten a Multidimensional Array?
(31 answers)
PHP - Multidimensional array to CSV
(3 answers)
How to write a multidimensional array to a csv file. PHP
(2 answers)
multidimensional irregular array to CSV in PHP
(1 answer)
Closed 3 years ago.
I need to explode stdentid array in comma separated value.currently the studentid array is in multidimensional array i need all the value should be in comma separated
The result should be for studentid array is
[studentid] => 36399,96500,96503,96509,96512 and so on..
Array
(
[started] => 1
[studentid] => Array
(
[1] => Array
(
[0] => 36399
)
[2] => Array
(
[0] => 96500
[1] => 96503
[2] => 96506
[3] => 96509
[4] => 96512
[5] => 96515
)
[3] => Array
(
[0] => 96501
[1] => 96504
[2] => 96507
[3] => 96510
[4] => 96513
)
[4] => Array
(
[0] => 96502
[1] => 96505
[2] => 96508
[3] => 96511
[4] => 96514
)
)
[name] => Test
[name_or_email] =>
[submitbtn] => 1
)
assuming your variable is $studentsResult (which contains the whole array you have posted)
in that case:
foreach($studentsResult['studentid'] as $studentId => $studentValues){
$tempStudents[$studentId] = join(',',$studentValues);
}
and then $tempStudents is what you're probably looking for\
If, however all those IDs needs to be under that ONE student, then you haven't said where is the ID, but you can follow the answer on flattening the array in one of the comments or do:
$tempStudents = [];
foreach($studentsResult['studentid'] as $someId => $studentValues){
$tempStudents = array_merge($tempStudents,$studentValues);
}
echo join(',',$tempStudents);
following array is received at server side :
[{"id":"2","foo":"bar","children":[{"id":"4","foo":"baz","children":[{"id":"6"}]},{"id":"5"}]},{"id":"7"},{"id":"3"}]
is there any way to convert it to a nested array like this?
[
['id' => 2, 'foo' => 'bar', 'children' =>[
'id'=> 4, 'foo' => 'baz' ....
P.S: i find out that without using any function i will have desired code format on server side. maybe this is because of sending data as post request (using ajax) that convert data to an array then serialize it and send , then on server side i have a nice array the same as what i'm looking for.Im not sure it is ralated to php or laravel or jquery !?
Array
(
[0] => Array
(
[id] => 2
[children] => Array
(
[0] => Array
(
[id] => 4
[children] => Array
(
[0] => Array
(
[id] => 6
)
)
)
[1] => Array
(
[id] => 5
)
)
)
[1] => Array
(
[id] => 7
)
[2] => Array
(
[id] => 3
)
)
To get the array in PHP, Yes. Use json_decode().
To get the "nested array like this" in JSON, No. This is not how arrays in JSON are supposed to look. See the specification at json.org, it is really easy to understand.
When I have an array like this :
Array (
[0] => Array ( [0] => 1 [1] => 12 [2] => Essener [3] => 1 )
[1] => Array ( [0] => 2 [1] => 12 [2] => Dinkel Spezial [3] => 0.2 )
[2] => Array ( [0] => 1 [1] => 1 [2] => Essener [3] => 1 )
)
and I use json_encode and echo it, I get this:
[["1","12","Essener","1"],["2","12","Dinkel Spezial","0.2"],["1","1","Essener","1"]]
which is good for me.
Now I have an array with stdClass Objects, which I wasn't able to transform into JSON with json_encode. When I echo it, it just doesn't show anything.
Then I transformed this array with objects to an array like this (with get_object_vars()):
Array (
[0] => Array (
[item_id] => 1
[item_name] => Essener
)
[1] => Array (
[item_id] => 2
[item_name] => Dinkel Spezial
)
[2] => Array (
[item_id] => 3
[item_name] => Saatenbrot
)
)
and when I use json_encode and echo it still doesn't show anything. Can anyone tell me what I am doing wrong or what I need to do to get a JSON array?
I need this json array to send data to an IOS App.
From the documentation:
Note:
In the event of a failure to encode, json_last_error() can be used to determine the exact nature of the error.
So you can try to detect the exact error by yourself. From your information I can't see any error.
Furthermore I don't think, it doesn't return anything. Try to var_dump() the result of json_encode(). I assume it returns false, which means an error occured.
So if anybody wondered what was wrong,
the problem was that i had "ä,ü,ö,ß" in my array and i needed to convert these to unicode and then everything worked just fine.
I have an array which is very complicated to generate.
Hence, would like to generate it once and then store as a string in a database.
Then, whenever it is needed, it can be simply retrieved from the DB as a string and declared as an array. Is there an easy way to do such a thing on PHP without using the eval() function.
So the string in the db might be something like
Array
(
[0] => Array
(
['none'] => Array
(
['none'] => Array
(
['Page'] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
)
)
[1] => Array
(
['Volume 1'] => Array
(
[] => Array
(
['Page'] => Array
(
[27] => 18
[28] => 19
[29] => 20
)
)
)
['Volume 2'] => Array
(
[] => Array
(
['Page'] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
)
)
)
)
You can use the json_encode and json_decode functions. They will serialize an array into a JSON string or deserialize a valid JSON string to an array respectively.
Using serialize/unserialize functions might not always be a good idea, because they come with security issues. Code might get executed upon deserialization which you had not intended. If no outside users have write access to the serialized objects, it should be fine though.
Yup, use serialize and unserialize functions.