This question already has answers here:
json_encode PHP array as JSON array not JSON object
(4 answers)
Closed 13 days ago.
I have an array with structure like - (checked with print_r statement)
Array
(
[0] => Array
(
[ads] => 343443543
[ad_name] => Cup
[cup_id] => 8869327
[web_url] => www.yahoo.com
[title] => Array
(
[0] => title1
[1] => title2
[2] => title3
[3] => title4
[4] => title5
)
[description] => Array
(
[0] => desc1
[1] => desc2
[2] => desc3
)
[new_title] => new_title1
)
[1] => Array
(
[ads] => 2325334
[ad_name] => Cup
[cup_id] => 8869327
[web_url] => www.google.com
[title] => Array
(
[0] => title1
[1] => title2
[2] => title3
[3] => title4
[4] => title5
)
[description] => Array
(
[0] => desc1
[1] => desc2
[2] => desc3
)
[new_title] => new_title1
)
)
When I use json_encode, it is returning with some object format like -
{"0":{"ads":"343443543","ad_name":"Cup","cup_id":"8869327","web_url":"www.yahoo.com","title":["title1","title2","title3","title4","title5"],"description":["desc1","desc2","desc3"],"new_title":"new_title1"},"1":{"ads":"2325334","ad_name":"Cup2","cup_id":"8869327","web_url":"www.google.com","title":["title1","title2","title3","title4","title5"],"description":["desc1","desc2","desc3"],"new_title":"new_title2"}
Instead, I want to have json string to be -
[{"ads":"343443543","ad_name":"Cup","cup_id":"8869327","web_url":"www.yahoo.com","title":["title1","title2","title3","title4","title5"],"description":["desc1","desc2","desc3"],"new_title":"new_title1"},{"ads":"2325334","ad_name":"Cup2","cup_id":"8869327","web_url":"www.google.com","title":["title1","title2","title3","title4","title5"],"description":["desc1","desc2","desc3"],"new_title":"new_title2"}]
I have another array variable for which it is showing correct json format. Now, sure what's wrong here.
Any help is appreciated.
Try json_encode(array_values($data)) ?
Related
I have an array items following.
Array
(
[0] => Array
(
[0] => 2
[field_id] => 2
[1] => Photometric Interpretation
[title] => Photometric Interpretation
[2] => text
[field_type] => text
)
[1] => Array
(
[0] => 3
[field_id] => 3
[1] => Make
[title] => Make
[2] => text
[field_type] => text
)
[2] => Array
(
[0] => 4
[field_id] => 4
[1] => Model
[title] => Model
[2] => text
[field_type] => text
)
[3] => Array
(
[0] => 5
[field_id] => 5
[1] => Strip Offsets
[title] => Strip Offsets
[2] => text
[field_type] => text
)
[4] => Array
(
[0] => 6
[field_id] => 6
[1] => Samples Per Pixel
[title] => Samples Per Pixel
[2] => text
[field_type] => text
)
[5] => Array
(
[0] => 7
[field_id] => 7
[1] => Rows Per Strip
[title] => Rows Per Strip
[2] => text
[field_type] => text
)
[6] => Array
(
[0] => 8
[field_id] => 8
[1] => Software
[title] => Software
[2] => text
[field_type] => text
)
[7] => Array
(
[0] => 9
[field_id] => 9
[1] => Exposure Time
[title] => Exposure Time
[2] => text
[field_type] => text
)
)
I need to sort above array's field_id INDEX according to this following array's VALUE
Array
(
[0] => 7
[1] => 3
[2] => 4
[3] => 5
[4] => 2
[5] => 6
)
OR to following string
7,3,4,5,2,6
I have tried to sort array using uksort() and uasort().
Usual foreach makes the desired result
$index = array_flip([7,3,4,5,2,6]);
foreach($arr as $item)
$res[$index[$item['field_id']]] = $item;
Is it possible to insert array data in textbox?
This is my array:
[MTS] => Array
(
[103] => Array
(
[0] => MTS
[1] => Lorvia Christine
[2] => 103
[3] => 6/16/2014 9:31
)
)
[SSS] => Array
(
[104] => Array
(
[0] => SSS
[1] => Jannah Curtis
[2] => 104
[3] => 6/28/2014 14:29
)
)
[ITM] => Array
(
[105] => Array
(
[0] => ITM
[1] => Sassy Mica
[2] => 105
[3] => 6/18/2014 9:12
)
)
And I want to make my form like this:
And if this way is impossible can someone recommend me an another way to output it on textbox.
<input type="text" value="<?=$arrayname['arraykey']?>" />
And that's all you need to do.
I have created a script that generates an array.
This array needs to be stored so that if a user wants to export its contents( eg print ) it may be possible.
This is my array ($doc)
Array
(
[0] => Array
(
[0] => ------
[1] => ---Start----
[2] => -------
[3] => Generated on Saturday, 3rd, August 2013
)
[1] => Array
(
[0] => ------
[1] => -------
[2] => -------
[3] => -----------------------
)
[2] => Array
(
[0] => Tid
[1] => Policy
[2] => Budget
[3] => User
[4] => Capturetime
[5] => Country
)
[3] => Array
(
[0] => 1
[1] => ask
[2] => das
[3] => carol
[4] => 2013-07-09
[5] => Ethiopia
)
[4] => Array
(
[0] => 2
[1] => das
[2] => adsasd
[3] => ck
[4] => 2007-07-13
[5] => Slovakia
)
[5] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[6] => Array
(
[0] => ------
[1] => ---End----
[2] => -------
)
)
Now I save the array to a file:
file_put_contents('array.txt', print_r($printdoc, true));
Then I would like to call back the contents of array.txt to the array $printdoc and use it as an array.
$printdoc = file_get_contents('array.txt', true);
I get $printdoc as a string instead of an array
Any help?
You can use serialize() to convert your array into a serializable string, then store it in a file. When you read the string from the file use unserialze() to convert it back into an array.
I am fetching some of the datas from my database and one of my column has multiple entries from one user say col(col1) values(a,b,c). Now when I am selecting data from this database, my array looks like this,
Array
(
[0] => srt
[1] => qwe
[2] => xyz
[3] => abc
[4] => 1
)
Array
(
[0] => srt
[1] => qwe
[2] => xyz
[3] => abc
[4] => 2
)
Array
(
[0] => srt
[1] => qwe
[2] => xyz
[3] => abc
[4] => 3
)
Now as my array shows only fourth element of array is different.
I want to insert this data into another table which has different columns for different values means only one row corresponding to one user. Thus
col(col1) turns in to col(cola,colb,colc).
Now I want to insert like
cola -> 1,colb -> 2,colc -> 3.
But I am not able to access these values. If I select array[4] then it selects all four elements if I try this $array=array($result[4]); then it create an array of result but with same index value
Array ( [0] => 1 )
Array ( [0] => 2 )
Array ( [0] => 3 )
Array ( [0] => 4 )
I hope I am able to clarify my question.
So please suggest me some way so that I can access these values in some way.
Thanks !
EDIT
my code
while($result= mysql_fetch_array($select))
{
echo "<pre>";
print_r($result);
$array=array($result[4]);
echo "</pre>";
print_r($array);
my result
Array
(
[0] => suresh.galaxy#gmail.com
[email] => suresh.galaxy#gmail.com
[1] => 2011-01-06 13:00:36
[date_joined] => 2011-01-06 13:00:36
[2] => 1
[is_active] => 1
[3] => 1
[attribute_id] => 1
[4] => suresh
[info] => suresh
)
Array ( [0] => suresh )
Array
(
[0] => suresh.galaxy#gmail.com
[email] => suresh.galaxy#gmail.com
[1] => 2011-01-06 13:00:36
[date_joined] => 2011-01-06 13:00:36
[2] => 1
[is_active] => 1
[3] => 2
[attribute_id] => 2
[4] => patidar
[info] => patidar
)
Array ( [0] => patidar )
Array
(
[0] => suresh.galaxy#gmail.com
[email] => suresh.galaxy#gmail.com
[1] => 2011-01-06 13:00:36
[date_joined] => 2011-01-06 13:00:36
[2] => 1
[is_active] => 1
[3] => 4
[attribute_id] => 4
[4] => e5c59af60000c8dff51e4e9a315ab152:vN
[info] => e5c59af60000c8dff51e4e9a315ab152:vN
)
Array ( [0] => e5c59af60000c8dff51e4e9a315ab152:vN )
You could make a loop first, then from there you could slice the array after the loop
<?php
$arr = array();
while($result= mysql_fetch_array($select))
{
$arr[] = $result;
}
print_r($arr);
echo $arr[0][4] ;
How do I rearrange the following array
Array
(
[0] => Array
(
[0] => 1
[1] => Electronic
[2] => 0
)
[1] => Array
(
[0] => 2
[1] => mobile
[2] => 1
)
[2] => Array
(
[0] => 3
[1] => Tv
[2] => 1
)
[3] => Array
(
[0] => 4
[1] => smartphone
[2] => 2
)
[4] => Array
(
[0] => 5
[1] => freeze
[2] => 1
)
[5] => Array
(
[0] => 6
[1] => lg
[2] => 5
)
[6] => Array
(
[0] => 7
[1] => philips
[2] => 3
)
[7] => Array
(
[0] => 8
[1] => Onida
[2] => 3
)
[8] => Array
(
[0] => 9
[1] => Samsung
[2] => 3
)
[9] => Array
(
[0] => 10
[1] => Apple
[2] => 4
)
[10] => Array
(
[0] => 11
[1] => Apple 1.0
[2] => 10
)
[11] => Array
(
[0] => 12
[1] => Sasmung flat
[2] => 9
)
to create a parent child relationship having Parent with their respective child and save them in database. I want to save them as Wordpress categories and Show the result in wp-admin categories as categories are shown. looking forward for help.
Why not just register a new taxonomy? Make sure hierarchical is set to true.