Echo value from PHP array when brackets in key-value pairs - php

Quick question, how do you echo a variable when the array has key's that contain names with brackets etc. See array below:
[data] => Array
(
[0] => stdClass Object
(
[id] => 4
[contact_id] =>
[status] => Complete
[is_test_data] => 0
[datesubmitted] => 2014-04-16 22:18:39
[sResponseComment] =>
[responseID] => 4
[[question(1), option(0)]] =>
[[question(2)]] => John
[[question(3)]] => Wtf#wtf.com
[[question(5)]] => 0975735289
[[question(6)]] => 3010
[[question(9), option(0)]] =>
[[question(10)]] => Testing
[[question(11)]] => Later
[[question(12)]] => This year
)
Lets say i want to echo the answer to question 2, which is "John". How do i go about doing this?

Well you have an array conataining an array of objects:
echo $array['data'][0]->{'[question(2)]'};

Related

Returning multidimensional array value for PHP

I'm to return data from a provider and the array supplied looks like this :
Array
(
[result] => Array
(
[0] => Array
(
[problem_id.u_root_cause] =>
[number] => INC0000001
[short_description] => Unable to print
[u_alert_check] =>
[u_business_service] => Array
(
[display_value] => Printer
[link] => https://redacted/37bc8af837c3820ca7e3b15ec3990e1c
)
[assignment_group] => Array
(
[display_value] => Printer Support
[link] => https://redacted/b699595c37dac200a7e3b15ec3990eb1
)
[u_service_impact_duration] => 57 Minutes
[u_summary_notes] => Changed USB cable
[u_environment] => Production (PD)
[problem_id] =>
[priority] => 3 - P3
[u_business_services_impacted] =>
)
Now, I'm able to return most of the data like this,
foreach($data['result'] as $line){
echo $line['number'];
echo '<br>';
echo $line['short_description'];
}
The above returns the number and short_description from the array.
The trouble I have is how do I return u_business_service in the same line?
For acessing the multidimensional array you may use:
echo $line['u_business_service']['display_value'];
echo $line['u_business_service']['link'];

How to add one array to another array [duplicate]

This question already has answers here:
PHP append one array to another (not array_push or +)
(11 answers)
Closed 3 years ago.
I have two array, i want to convert it with one array with whole data.
My array format is here
$fromData=array('id' => '004','shapeid' => 'circle','x' =>'360','y' => '560', 'tooltext' => 'vivek','labelpos' => 'bottom');
$ToData=array('id' => '005','shapeid' => 'triangle','x' =>'480','y' => '980', 'tooltext' => 'kimi','labelpos' => 'top');
I wanna got whole data in one array.
Thanks
you can use array_merge_recursive
$fromData=array('id' => '004','shapeid' => 'circle','x' =>'360','y' => '560', 'tooltext' => 'vivek','labelpos' => 'bottom');
$ToData=array('id' => '005','shapeid' => 'triangle','x' =>'480','y' => '980', 'tooltext' => 'kimi','labelpos' => 'top');
$newdata= array_merge_recursive($fromData,$ToData);
output will be
Array
(
[id] => Array
(
[0] => 004
[1] => 005
)
[shapeid] => Array
(
[0] => circle
[1] => triangle
)
[x] => Array
(
[0] => 360
[1] => 480
)
[y] => Array
(
[0] => 560
[1] => 980
)
[tooltext] => Array
(
[0] => vivek
[1] => kimi
)
[labelpos] => Array
(
[0] => bottom
[1] => top
)
)
This can be done based on what #Ajith is suggesting in above comment
$result[] = $fromData;
$result[] = $ToData;
You can check this implementation here, https://3v4l.org/0QR56

echoing an array value giving a key from another array

I know this is simple, but cant get my head around it...
I have 2 arrays. Both populated from a database lookup.
Array 1
Array (
[sailID] => 7
[sailTag] => 100004
[assigneduser] => Jason Ellmers
[assigneddate] => 2018-05-30 17:48:57
[cutuser] => Jason Ellmers
[cutdate] => 2018-05-30 20:31:23
[stickuser] => Jason Ellmers
[stickdate] => 2018-05-30 20:38:24
[corneruser] => Jason Ellmers
[cornerdate] => 2018-05-30 20:38:54
[finishuser] => Jason Ellmers
[finishdate] => 2018-05-30 20:39:53
[checkuser] =>
[checkdate] => 0000-00-00 00:00:00
[DesignRef] => 420abcdefg
[OrderingLoft] => 1
[ClassRef] => 1
[ClothType] => Bainbridge
[ClothColour] => White
[ClothWeight] => 12oz
[SailNo] => GB342398 )
Array 2
Array (
[0] => Array (
[id] => 1
[name] => 420 )
[1] => Array (
[id] => 2
[name] => J24 ) )
What I am after doing is being able to echo to the screen $array1['Where the ClassRef is a lookup of the ID in Array2' and displays the Name from Array2]
So for the above example the Echo would be '420'
I think I could do it using a foreach or while loop but that seems a bit cumbersome???
I've had to put some test data together, but from the comment, the idea is to re-index the second array using array_column() with the id as the index, so the code (as you've worked out) is...
$array1 =[
"sailID" => 7,
"sailTag" => "100004",
"ClassRef" => 1 ];
$array2 = [["id" => 1, "name" => "420"],
["id" => 2, "name" => "J24"]];
$array2 = array_column($array2, "name", "id");
echo $array2[$array1["ClassRef"]];

Get JSON value from PHP

All, I have got a JSON response from NEO4J:
Array
(
[columns] => Array
(
[0] => n
)
[data] => Array
(
[0] => Array
(
[0] => Array
(
[outgoing_relationships] => http://localhost:7474/db/data/node/1/relationships/out
[labels] => http://localhost:7474/db/data/node/1/labels
[data] => Array
(
[position] => Developer
[awesome] => 1
[name] => Michael
[children] => 3
)
[traverse] => http://localhost:7474/db/data/node/1/traverse/{returnType}
[all_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/all/{-list|&|types}
[property] => http://localhost:7474/db/data/node/1/properties/{key}
[self] => http://localhost:7474/db/data/node/1
[properties] => http://localhost:7474/db/data/node/1/properties
[outgoing_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/out/{-list|&|types}
[incoming_relationships] => http://localhost:7474/db/data/node/1/relationships/in
[extensions] => Array
(
)
[create_relationship] => http://localhost:7474/db/data/node/1/relationships
[paged_traverse] => http://localhost:7474/db/data/node/1/paged/traverse/{returnType}{?pageSize,leaseTime}
[all_relationships] => http://localhost:7474/db/data/node/1/relationships/all
[incoming_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/in/{-list|&|types}
)
)
)
)
I do not know how to retrieve the value "position" under the data array, because the array is under another array.
Can you tell me how to do it with PHP?
Thx
Access them like this.
echo $yourarr['data'][0][0]['data']['position'];
Tips on how to locate :
Just locate where is the position keyword, Now look up to the array, As you can see the parent of position is data, think of how you reach from the start
(data)to the destination(position) (like a maze).
When you have more than 1 record try this
foreach($var['data'] as $inside){ //inner 1st stage
foreach($inside as $index => $main){ //inner 2nd stage
if($index == 'data'){ //check if index is data
echo $main['position']; //output position
}
}
}

How to print data from an array php?

I am having some troubles calling the correct values in the php array. Here is the array.
Array ( [count] => 1 [threads] => Array ( [13] => Array ( [thread_id] => 13 [node_id] => 4 [title] => Forum Integration nearly complete! [reply_count] => 0 [view_count] => 0 [user_id] => 59 [username] => Faeron [post_date] => 1369257302 [sticky] => 0 [discussion_state] => visible [discussion_open] => 1 [discussion_type] => [first_post_id] => 23 [first_post_likes] => 0 [last_post_date] => 1369257302 [last_post_id] => 23 [last_post_user_id] => 59 [last_post_username] => Faeron [prefix_id] => 1 [content] => Array ( [count] => 1 [content] => Array ( [23] => Array ( [post_id] => 23 [thread_id] => 13 [user_id] => 59 [username] => Faeron [post_date] => 1369257302 [message] => It's been quite a while since we began to integrate the phanime Forums with the main site. We have now finished the integration with the phanime Forums and the main site. You will no longer notice that there are two platforms running phanime, but instead only one. Our next step is to theme the forums to make it look like the main site! [ip_id] => 268 [message_state] => visible [attach_count] => 0 [position] => 0 [likes] => 0 [like_users] => a:0:{} [warning_id] => 0 [warning_message] => ) ) ) ) ) )
Now lets say this array was named $array then to get the first element's value "[count]" can't I just say the following: print $array["[count]"] <-- this returns an error.
What about the element that has a value as an array itself, which is the [threads] element. How do I get, perhaps the [thread_id] element's value?
Use it like this:
echo $array['count']; // would output '1'
echo $array['threads'][13]['thread_id']; // outputs '13'
echo $array['threads'][13]['content']['content'][23]['message']; // "It's been.."
Here is the (brief) documentation on multidimensional arrays: http://php.net/manual/en/language.types.array.php#language.types.array.syntax.accessing
Here's a good guide to them with examples: http://www.developerdrive.com/2012/01/php-arrays-array-functions-and-multidimensional-arrays/
Update: To get the value of 'message' without knowing the numbered array keys beforehand you could use:
reset($array);
$first = array_keys($array['threads']);
$first = $first[0];
$second = array_keys($array['threads'][$first]['content']['content']);
$second = $second[0];
echo $array['threads'][$first]['content']['content'][$second]['message'];
You can access a value in an array by using the following:
echo $array['count'];
You can also print the entire array like so:
print_r($array);
or
var_dump($array);
If you want to write a value from a multidimensional array then use the following:
echo $array[23]['post_id'];
So, to summarize, see the following:
$array = array(
'bar' => 'testing value',
'foo' => array(
'bar' => 'test'
)
);
print_r($array) // Will print whole array
echo $array['bar']; // Will print 'testing value'
print_r($array['foo']); // Will print the second level array
echo $array['foo']['bar']; // Will print 'test'

Categories