PHP - Echo an array data to form_input - php

How to echo an array to a form?
My array
Array
(
[0] => Array
(
[id] => item1
[price] => 300
[quantity] => 1
)
[1] => Array
(
[id] => item2
[price] => 400
[quantity] => 2
)
)
I tried like (codeigniter):
echo form_textarea('detail_prod',print_r($detail));
And I got like
what happen? why the result is 1 ?
It's possible to echo to a form in array format?

Just pass the 2nd parameter true like below:
echo form_textarea('detail_prod',print_r($detail, true));
For your help:
http://php.net/manual/en/function.print-r.php
Hope this will useful.

Related

Getting Value from Nested Array -- PHP Wordpress --

I am using get_post_meta like below:
$job_owner = get_post_meta($post->ID, 'assignedUsers', true);
That returns the following:
(
[total] => 1
[data] => Array
(
[0] => stdClass Object
(
[id] => 440968
[firstName] => John
[lastName] => Doe
[email] => john#website.com
)
)
)
I am trying to grab the values from the object but catch an error each time I simply try and use echo $job_owner. Error is - Object of class stdClass could not be converted to string
I have tried to use:
$array = json_decode(json_encode($job_owner), true);
Which returns the arrays:
Array
(
[total] => 1
[data] => Array
(
[0] => Array
(
[id] => 440968
[firstName] => Megan
[lastName] => Collins
[email] => megan#bridgeviewit.com
)
)
)
But I cannot seem to get anything to return using echo $array[0]->id etc...
My ideal scenario is to use the array values as variables to use throughout the theme.
So with echo $array[0]->id are trying to get the 0th element of that array which is total. So echo $array[0]->id should fail but echo $array[0] should return 1. If you change your request to $array[data][0]->id or $array[1][0]->id that should get you the value you are looking for - the id of the first element in the data bit.
$array[data][0]->id

PHP Get values from nested array

I am new to PHP and Arrays, I am trying to get the values from an array. But no matter how I'm trying to do it, I can't get the value. What am I doing wrong?
The Array:
Array ( [playerinfo] => Array ( [rank] => Godfather [cash] => € 8,520,530 [weapon] => M-16 (29000) [health] => Array ( [width] => 100 [color] => green ) [wealth] => Too rich to be true [protection] => Bulletproof Humvee [plf] => Huge [plane] => Concorde [crew] => None [pbf] => Large [ship] => None ) [character] => Array ( [crime] => Array ( [0] => 120 [1] => 69 ) [gta] => Array ( [0] => 400 [1] => 70 ) [drugs] => Array ( [0] => 120 [1] => 2528 ) [airport] => Array ( [0] => 2700 [1] => 2529 ) [oc] => Array ( [0] => 86400 [1] => 1442364 ) [tr] => Array ( [0] => 10800 [1] => 1640016011 ) [plf] => Array ( [0] => 7200 [1] => 6712 ) [kill] => Array ( [0] => 3600 [1] => 1640019611 ) ) )
The way I tried to get the info:
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
foreach ($AccData as $playerinfo) {
echo $playerinfo['playerinfo']['rank'].'<br/>';
echo $playerinfo['character']['gta'].'<br/>';
}
EDIT:
The json string
{"playerinfo":{"rank":"Boss","cash":"€ 5,923,712","weapon":"M-16 (4500)","health":{"width":"100","color":"green"},"wealth":"Too rich to be true","protection":"Bulletproof Humvee","plf":"Huge","plane":"Concorde","crew":"None","pbf":"Large","ship":"None"},"character":{"crime":[120,122],"gta":[400,369],"drugs":[120,2582],"airport":[2700,2582],"oc":[86400,1640020450],"tr":[10800,1640016850],"plf":[7200,3935],"kill":[3600,1640020450]}}
Anyone knows how to do this ? For example I need the Concorde from plane in a variable and the time values from gta in a variable. And some more from this string.
So your first Part is okay and you rank you can just display like the first part as well
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['plane'].'<br/>';
echo $AccData['character']['gta'][0].'<br/>';
but the character is on the same level as playerinfo so you need to access it from AccData. also gta is an array like health, so you have to specify which value you want to show, the first so 0 or second which is 1

Getting data out of array in php

below is array info, I just need to echo out what is for Array. So you will see that in data there is Example 1 and Example 3, Just need to echo that out.
This: $user->getFieldValue('AUTOCOMPLETE'); when I print_r, is the results below.
SocialFieldValue Object
(
[unique_key] => AUTOCOMPLETE
[element] => autocomplete
[field_id] => 161
[uid] => 84
[type] => user
[value] => Array
(
[Example 1] => Example 1
[Example 3] => Example 3
)
[raw] => ["Example 1","Example 3"]
[data] => Array
(
[0] => Example 1
[1] => Example 3
)
)
Did you test this code?
$user->getFieldValue('AUTOCOMPLETE')->value['Example 1'];
This is so simple you need to access prop using ->
$autoComplete = $user->getFieldValue('AUTOCOMPLETE');
$data = $autoComplete->data;
Now you can access its items with.
echo $data[0] // prints Example 1

PHP - How to upload image in a multidimensional array loop

I am not able to upload image, I tried using the below code to display. But nothing gets displayed.
PHP Code is as
$question_image = $allquestion["'ques_image'"];<br>
$imagetemp = $_FILES[$question_image]['name'];<br>
echo "IMAGE NAME".$imagetemp;
And Returned Array
Array
(
[1] => Array
(
['ques_title'] => Question 1
['ques_image'] => galaxy_s7_black.png
['choice'] => Array
(
[0] => Array
(
[0] => Question 1 - First Choice
[1] => 127
)
[1] => Array
(
[0] => Question 1 - second Choice
[1] => 128
)
)
)
[2] => Array
(
['ques_title'] => Question 2
['ques_image'] => iphone7_rosegold.png
['choice'] => Array
(
[0] => Array
(
[0] => Question 2 - First Choice
[1] => 131
)
[1] => Array
(
[0] => Question 2 - second Choice
[1] => 132
)
)
)
)
How to upload image in a multidimensional array loop.
How did you print the array?
You must call the array arguments in step-by-step
for example :
echo $_FILES[1]['ques_image']
or
echo $_FILES[2]['ques_image']

How to echo a specific value from an array in php using REST API , need to echo name from the given array

Array
(
[root] => Array
(
[status] => Array
(
[statusCode] => 200
[message] => OK
)
[response] => Array
(
[success] => 1
[result] => Array
(
[0] => Array
(
[id] => 1
[content] => 12
[enabled] => 1
[date_modified] => 2013-03-07 19:28:19
[name] => Home page
[category_id] => 1
)
)
[error_messages] =>
[debug] =>
Please help me to echo the name from the given array. i have tried many things but not able to find correct , please help me on it.
Chances are you're going to get more than one name, so:
foreach($yourArrayRef['root']['response']['result'] as $result) {
echo $result['name'];
}
Try this:
$arr['response']['result'][0]['name']
Try this
echo $response["result"][0]["name"];

Categories