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:
Related
I am beginner to laravel. I am trying to access array input from my API request.
For postman, I have array as key called file_name_list and its value like ["m_profile.png","aa_image.jpg","new_pan.jpg"]. I want to access that array in my controller. That values should go into 3 seperate variables like
$profile = m_profile.png
$aadhar = aa_image.jpg
$pan = new_pan.jpg
For that I am trying to use replace and explode functions in controller.
$filenamelist1 = Str::replaceArray(array(' ','"', '[',']'),[""], $request->file_name_list);
$filename_str = explode(",",$filenamelist1);
After this I want to store values from explode array to 3 variables as mentioned above using for loop
But I am facing problems like in Str::replaceArray 2 parameter should be array and for explode 2 parameter should be string.
How should I use replace and explode to get required result? please guide. Thanks in advance.
You can use list method like below:
list( $profile , $adhar, $pan) = $request->file_name_list;
Check the Docs
If Your array size fixed then you assign that by using list
list( $profile,$adhar,$pan) = $request->file_name_list;
<?php
$value = '["m_profile.png","aa_image.jpg","new_pan.jpg"]';
$value = str_replace("[","",$value); // clean [
$value = str_replace("]","",$value); // clean ]
$arrayValues = explode('","',$value); // explode with ","
print_r($arrayValues);
output
Array (
[0] => "m_profile.png
[1] => aa_image.jpg
[2] => new_pan.jpg"
)
Replace " when you access the values
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..
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);
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
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']);