Multi-dimensional array in php - php

I would like to create a multi-dimensional array with two variables but don't know how.
This is what i have so far;
$_SESSION['name'][] = $row_subject['name'];
$_SESSION['history'][]= $_SERVER['REQUEST_URI'];
I wanted to know if this is possible?
$_SESSION['name'][] = $row_subject['name'],$_SERVER['REQUEST_URI'];
i want to get the name of a programme which is generated via a data base and also to retrieve the url. What i am actually doing once the name is retrieve, i want to make that a link which the url would be necessary.
any help would be appreciated.
Thanks

I'm not sure what you want to do but the correct notation for your second example could be
$_SESSION['name'][] = array("name" => $row_subject['name'],
"history" => $_SERVER['REQUEST_URI']);
This pushes an associative array with the keys "name" and "history" to the array $_SESSION["name"].
You could then access the entries like so:
echo $_SESSION["name"][0]["name"];
echo $_SESSION["name"][0]["history"];
if you repeat the command with different data:
$_SESSION['name'][] = array("name" => $row_subject['name'],
"history" => $_SERVER['REQUEST_URI']);
the next entry would be addressed like so:
You could then access the entries like so:
echo $_SESSION["name"][1]["name"];
echo $_SESSION["name"][1]["history"];
and so on.

$_SESSION['name'][] = array($row_subject['name'], $_SERVER['REQUEST_URI']);

Related

Expand an string based on values in an array in PHP

I would like to expand a string with one or more values which come from an array.
Desired result:
http://example.com/search/key=["Start", "USA", "Minneapolis"]&shift=false
Array:
array(2) {
[0]=>
string(8) "USA"
[1]=>
string(4) "Minneapolis"
}
PHP Code:
$myArr = ("USA", "Minneapolis");
$string = 'http://example.com/search/key=["Start",' .$myArr[0]. ']&shift=false';
Gives me this result: http://example.com/search/key=["Start", "USA"]&shift=false
How can I make it more dynamic so that more than one value can be added? I know I somehow have to use a foreach($myArr as $i){...} and concatenate the values via $string += but because my variable is in the middle of the string I'm kinda stuck with this approach.
Try the following:
$myArr = array("USA", "Minneapolis");
$string = 'http://example.com/search/key=["Start", "' . implode('", "', $myArr) . '"]&shift=false';
This will provide the expected output using implode.
Something isn't right here. You are trying to pass array data through a querystring but not in a valid array structure. This means you are either not using it as an array on the next script and you are going to having to chop and hack at it when the data gets there.
I'll make the assumption that you would like to clean up your process...
Declare an array of all of the data that you want to pass through the url's query string, then merge the new key values into that subarray. Finally, use http_build_query() to do ALL of the work of formatting/urlencoding everything then append that generated string after the ? in your url. This is THE clean, stable way to do it.
Code: (Demo)
$myArr = ["USA", "Minneapolis", "monkey=wren[h"];
$data = [
'key' => [
'Start'
],
'shift' => 'false'
];
$data['key'] = array_merge($data['key'], $myArr);
$url = 'http://example.com/search/?' . http_build_query($data);
echo "$url\n---\n";
echo urldecode($url);
Output:
http://example.com/search/?key%5B0%5D=Start&key%5B1%5D=USA&key%5B2%5D=Minneapolis&key%5B3%5D=monkey%3Dwren%5Bh&shift=false
---
http://example.com/search/?key[0]=Start&key[1]=USA&key[2]=Minneapolis&key[3]=monkey=wren[h&shift=false
*the decoded string is just to help you to visualize the output.
Then on your receiving page, you can simply and professionally access the $_GET['key'] and $_GET['shift'] data.
If you have a legitimate reason to use your original malformed syntax, I'd love to hear it. Otherwise, please use my method for the sake of clean, valid web development.

PHP adressing the first key in an array that changes

For example, lets say I have an array that looks liked
$stuff = array('random_string_1' => 'random_value_1','random_string_2' => 'random_value_2');
and then I call the asort() function and it changes the array around. How do I get the new first string without knowing what it actually is?
If you want to get the first value of the array you can use reset
reset($stuff);
If you want to also get the key use key
key($stuff);
If you need to get the first value, do it like this:
$stuff = array('random_string_1' => 'random_value_1','random_string_2' => 'random_value_2');
$values = array_values($stuff); // this is a consequential array with values in the order of the original array
var_dump($values[0]); // get first value..
var_dump($values[1]); // get second value..

Add an array elements into PHP/ Insert variable with value with Array_push

I am able to parse data from youtube gdata. For example:
$json_output = json_decode($json,TRUE);
foreach ( $json_output['data']['items'] as $data ){
echo $data['content'][1]
. '</br>'
. $data['title'];
These give me rtsp url and title. Now suppose I want some other elements to add to the output (I don't possess knowledge of php, so I don't know the terms actually). What I want, that the output should have an another variable, let's take 'Boys'. This variable will have index key values same as youtube gdata and will be as follows:
Boys="You", "Me", "He", "Doggy",.....nth value.
My above code gives two values each time-url and title. Now I want the 3rd value which will be added to that from 'Boys'.
I tried Array_push as follows but it adds as an extra element and not as a variable like 'title' or 'content'.
$Boys= array('demi', 'ffe', 'erere');
array_push($json_output['data']['items'], $Boys );
How to properly insert Boys as variable? is there other methods like merge etc. to do it?
Again, since I am not a coder, please pardon my words!
Let's say you have 2 arrays with same keys, then you can do array_merge_recursive() to merge them together.
Here is a working example,
$a = [ 'boys' => ['play', 'sing']];
$b = ['boys' => 'fight'];
$merged = array_merge_recursive($a, $b);
print_r($merged);

Storing difficult data structure in php arrays

I need to store this data structure in php array:
Movie has id, name and show_date. Each of show_dates have show_times. I need to dynamically in cilcle fill this array with data from my data source. When i do like this:
$Movie = array();
$Movie[0]['id']=10;
$Movie[0]['name']='Some Name';
$Movie[0]['date'][0]='12.12.12';
$Movie[0]['date'][0]['time'][0]='12:23:00'; //there it throws error
$Movie[0]['date'][0]['time'][1]='15:23:00';
Could you help me with this issuse ?
You're trying to do array access on a string.
Change to:
$Movie[0]['date'] = array();
$Movie[0]['date'][] = array( // shorthand push notation
"date" => "12.12.12",
"times" => array("12:23:00", "15:23:00")
);
// .. etc

How to display something from array with another name in PHP?

I have one array that returns one element $name, and I want to display it with another name.
For example:
$name = 'PC' and I want to display it as 'PC COMPUTER', first array displays element which is true, how to compare this elements with another array to display it with that other names?
Can't you just make a second array and load it up with more descriptive names, then use the first as a key?
$name = 'PC';
$description = array('PC' => 'PC COMPUTER');
echo $descriptions[$name];
Is that what you are looking for?
$array[$name] = 'PC COMPUTER'
& Post ur array .give a eg:

Categories