How to get value of array inside another array in php? - php

I made a php page in which I got the array within array value.
Here is my array:
Array (
[events] => Array ( [0] => 5 [1] => 7 [2] => 8 )
[fromdate] =>
[todate] =>
[description] =>
[subject] =>
[fromname] =>
[replyto] =>
[senddatetime] =>
[timezone] => America/Los_Angeles
[message] =>
[submit_skip] => Continue
)
This array is the POST data from previous page. I want to get events value in a variable. How can I do this?
I use this for getting events value but it gives me the array but I want to get first event value
echo $eventid=$_POST['events'];

To echo:
echo $_POST['events'][0];
To assign it to a variable:
$eventid = $_POST['events'][0];
To assign the array to a variable and get the value from the new array:
$events = $_POST['events'];
$eventid = $events[0];
[0] represents the key of the first value since it is set to be 0.

Output:
echo $_POST['events'][0];
Assign to variable:
$eventid = $_POST['events'][0];

Related

Extract data from php array

I have the following array in my Moodle plugin:
Array (
[0] => stdClass Object (
[id] => 32
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1464071400
[timefinish] => 1464102000 )
[1] => stdClass Object (
[id] => 33
[sessionid] => 5
[sessiontimezone] => CET
[timestart] => 1465281000
[timefinish] => 1465311600 )
)
How to get the data. Right now, when I make:
$pluginsessiondates = $DB->get_record('plugin_sessions', array('id'=>$sessionid));
I get only data from the frist array [0]
How to get the data from every array key and then the single values? Thanks in advance.
The Moodle DB functions are for getting data out of the database, rather than from an array somewhere inside your plugin.
If you have an array somewhere, then you can get fields from it by writing:
echo $myarray[0]->id;
echo $myarray[1]->id;
etc.
If you are not trying to get data out of an existing array and want, instead, to get it out of the database, then $DB->get_record() will, as its name implies, get you only a single record, whereas $DB->get_records() will get you all the matching records:
$sessions = $DB->get_records('plugin_sessions', array('sessionid' => $sessionid));
foreach ($sessions as $session) {
echo $session->id;
}

show random values from array of array

Below is a array generated by a query builder.
$random_array = Array ( [0] => Array ( [text] => A great time was had by all! )
[1] => Array ( [text] => KILL SHOT )
[2] => Array ( [text] => How is it possible)
[3] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
[4] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g )
)
Currently i am doing like this to print the random value
print_r(array_rand($random_array,1));
This is printing the array key as 3 or 1 etc(random from above array).I want to print the value of the key and not the key.
e.g I want to print the random value like this "http://www.youtube.com/watch?v=KwGOZpbxU9g" or "A great time was had by all!" instead of 3 or 1 which is printing now.
Is it possible to do this.
You will have one more line of code as shown below:
$array_key = array_rand($random_array,1); //get the key
print_r( $random_array[$array_key] ); //use the key to print value
What about simply calling
$randNumber = rand(0,count($random_array))-1; //index in array starts with 0
print (string) $random_array[$randNumber];

PHP nested array yielding undefined index error - what am I doing wrong?

I'm sending a POST array to a controller. The array looks like such:
Array
(
[event] => Array
(
[0] => Array
(
['publishStart'] => 2013-12-10
['eventStart'] => 2014-05-05
['eventEnd'] => 2014-05-10
['timeStart'] =>
['timeEnd'] =>
['location_id'] => 1
['id'] => 65774
)
There are a few of these blocks, i.e. [event][1]. [event][2], etc. I'm attempting to run a foreach loop on $_POST['event'] and can confirm that on each iteration, $event contains the following:
Array
(
['publishStart'] => 2013-12-10
['eventStart'] => 2014-05-05
['eventEnd'] => 2014-05-10
['timeStart'] =>
['timeEnd'] =>
['location_id'] => 1
['id'] => 65774
)
Now, the problem. You can see in both above arrays there is a key called "id" with a corresponding value. Yet the following code returns a string of "undefined index" errors:
foreach ($_POST['event'] as $event)
{
echo $event['id'];
exit();
}
What on earth am I doing wrong?
Your array indices seem to include the single-quotes.
Try
echo $event["'id'"];

php Post arrays in Session array

Im trying to put multiple posts into one session. Heres the line of code that i use to do it.
$_SESSION['answers'][] = array_push($_SESSION['answers'], $_POST);
As i do so, the following array comes out once i try to add the second array to the session:
Array
(
[0] => Array
(
[checkbox] => Optie 2
[category] => Dieren en natuur
[subcategory] => Wilde dieren
[vraagid] => 116
[type] => 3afbeeldingen
[media] => image
[submit] =>
)
[1] => Array
(
[checkbox] => Optie 1
[category] => Dieren en natuur
[subcategory] => Wilde dieren
[vraagid] => 117
[type] => 3afbeeldingen
[media] => image
[submit] =>
)
[2] => 2
)
I am talking about the last array,
[2] => 2.
This is automattically added. Now when i try to get another array in to the session, it gives me the same issue, it adds the correct array, with checkbox and other vars, but also makes a
[4] => 4
Is this an issue with the array_push function? Because the array that i push is correct, i already checked that.
Either add the value to your array:
$_SESSION['answers'][] = $_POST;
or use array_push
array_push($_SESSION['answers'], $_POST);
You try to do to much at once :)
Won't it work, when you just do:
array_push($_SESSION['answers'], $_POST);
or
$_SESSION['answers'][] = $_POST;
array_push() is basically the same as $array[] = .... array_push() returns the new number of elements in the array, so basically you are adding the new element to your array and then you are adding the amount of elements in the array to the array again (hence the 2).

how to assign elements of one array to another array in php

i retrieved data from mysql table in php
$array2=array(); while($q1= mysql_fetch_assoc($result))
{ print_r($q1);
$array2[]= $q['user_id'];
}
the print_r($q1) will output this Array ( [user_id] => 1 ) Array ( [user_id] => 2 ) Array ( [user_id] => 4 ) .
Now i want to assign these values into another array i.e
$array2[]=$q['user_id']; but when i echo $array2 this gives me result otherthan $print_r($q) i.e
Array ( [0] => [1] => [2] => )
my question is how can i get the value of [user_id]=>4 from this array
$q is different from $q1..
You need:
$array2[]= $q1['user_id'];
It should actually be throwing a warning about $q['user_id'] not being defined. Do you have error reporting turned on?

Categories