I'm working with the Youtube Data Api V3 and this array is beeing returned from my api call:
What i want:
I wanna access the red underlined Array in the Image above and respectively get the latest array inside of it. (In the example i wanna access the [maxres], but it's always different)
What i tried:
$var = end($array)['url']; // $array is my input array
But that doesn't work and so i hope someone can help me :)
You want to use array_pop. Link to PHP documentation.
<?pnp
$var = array_pop($array['modelData:protected']);
$object->modelData is a protected property, you cannot access it. Usually there is a function included in the object to retrieve it.
For example $array = $object->getModelData();
But if it was not protected, this would be a solution:
$array = $object->modelData;
end($array);
$key = key($array);
var_dump($object->modelData[$key];
Try this...
$var = $array['url'][ count($array['url']) - 1 ]
Related
I want to get the id from my url like below
http://localhost/cpanel-ar/stage_one/reports.php?temp=temp_21day
How can GET only the "temp"?
I assume that you mean this:
$getVars = array_keys($_GET);
print_r($getVars);
This will return an array with your get parameters.
for example:
http:example.com?getparameter=getvalue
returns:
Array ( [0] => getparameter )
why you dont try to use substr function?
$data = $_GET['temp'];
echo substr($data,0,4);
its will only catch temp
i dont know if i understood but you can use predefined variables using:
$temp = $_GET['temp'];
but if you are using a framework maybe they already is handle that.
if you want the 'temp' name and you know witch parameters are passed to URL you can use
array_keys($_GET)[i]
Where i i the index of parameter
I need to merge a php array INTO a php object.
I need my php object to look like this :
{"info1":"value","concurrents":[{"concurrent1":"value"},{"concurent2":"value"}],"info3":"value"};
, I'm actually getting my initial object like this :
$initial = pg_fetch_object($result);
After that, I'm getting the array with this command :
$concurrents = pg_fetch_all($result);
So please, how could i merge $concurrents INSIDE my $initial object, for executing this finally for destination to the front end:
print_r(json_encode($initial));
I've tried a foreach, but it doesn't work.
Have you tried the following?
$initial->concurrents = $concurrents;
Or even:
$initial->concurrents = pg_fetch_all($result);
Don't get your initial result as an object, get it as an array:
$initial = pg_fetch_assoc($result);
Then 'merging' is fairly easy (push the initial on to the beginning of your concurrents array):
$concurrents = pg_fetch_all($result);
array_unshift($concurrents, $initial);
Now you can print them:
print_r(json_encode($concurrents));
I have multidimensional array like this:
$comp = [{"d":1},{"v":9}];
I need to insert another array
$arr = array("s" => 5 );
I tried the following code
$comp [] = array_push($comp,$arr);
I get the output $comp = [{"d":1},{"v":9},{"s":5},3]
I dont need 3.
So I have tried
$comp [] = json_encode(array_push(json_decode($comp),$arr));
But now it shows error.
Plz help me. I am using angular js with php.
You don't need array_push here. Just assign $arr to $comp[]
$comp[] = $arr;
Actually what you did here, let me explain it to you:
$comp[] - means pushback element.
It works the same way here as array_push($comp,$arr), but that function also returns the position where that push happened.
So you are pushing result of pushing the element as well as element itself :)
what you need is
$comp[] = $arr;
OR
array_push($comp,$arr);
I want to do something like bellow using php:
$turl=array(trim($params->get('c2')));
$tname=array(trim($params->get('cn2')));
and want to display each $turl with each $tname.
I tried like this:
$result=array_combine($turl,$tname);
print_r($result);
but given result as:
Array ( [http://184.107.144.218:8282/,http://184.107.144.218:8082/] => ABC Radio,AHH Radio )
But I want like this:
Array ( [http://184.107.144.218:8282/=> ABC Radio,
http://184.107.144.218:8082/=> AHH Radio )
Thanks in advance
maybe you want to use a code similar to this:
$turl = explode(',',trim($params->get('c2')));
$tname = explode(',',trim($params->get('cn2')));
$result=array_combine($turl,$tname);
print_r($result);
The error seems to be into the $turl and $tname arrays creation as array combine should work as intended
Obviously I would add several checks, as, for example, Have the two arrays the same size?
Addendum
In your comment you give me a specimen of what $params->get returns if called with parameters 'c2' and 'cn2'.
$params->get('c2')="hoicoimasti.com,google.com"
$params->get('cn2')="hoicoi,google".
The example code I gave you do what required, or at leas what I was thinking you are trying to obtain. A different code with a different result is:
$turl = explode(',',trim($params->get('c2')));
$tname = explode(',',trim($params->get('cn2')));
$result=array_map(function($x,$y){return $x.','.$y;},$turl,$tname);
print_r($result);
or, if you want to obtains a single string:
$result=join(',',array_map(function($x,$y){return $x.'=>'.$y;},$turl,$tname));
print_r($result);
You can obtain any desired result modifying one of the previous example.
To encase the results in an option you need a slight variation of the array_map user function:
$result=join("\n",array_map(
function($x,$y){
return "<option>$x=>$y</option>";
},
$turl,
$tname
));
print_r($result);
PHP < 5.3 version
Put this function declaration somewhere in the global scope
function formatOption($x,$y){
return "<option>$x=>$y</option>";
};
and then your code will become:
$result=join("\n",array_map( 'formatOption', $turl, $tname));
print_r($result);
If possible I won't clutter the global namespace with function like formatOption,
but when anonymous function are not available
Reference:
array_map
join
This is a two part question. First how can I grab the last url value from a link when I dont know how deep the value is, for example how can I grab
the last value of sub_4 from the link example below using PHP? And second how can I grab the url value cat=3 and the last url value sub_4 using PHP?
I'm kind of new to PHP so a detailed step by step example would help me out a lot if its not too much trouble.
Thanks in advance!
Here is an example of a URL value.
http://www.example.com/categories/index.php?cat=3&sub_1=sub1&sub_2=sub2&sub_3=sub3&sub_4=sub4
All variables from the hook will be returned on $_GET. So if you want to get that value from the URL, just use:
$_GET['sub_4']
If you want to get a list of all of the possible values:
array_keys($_GET)
This will give you all of the variables.
UPDATE: I just reread your question. I think I better understand what you are looking for. Correct me if I am wrong, but you are not certain how to get the last element of the string? Is that right? So you could potentially have sub_5, sub_6, sub_7 etc. Here is how to get the last element from the string:
end($_GET);
$key = key($_GET);
$last_item = $_GET[$key];
$last_item will now have sub_4 (or what ever the last item is).
Extracting URL Parameters
Query parameters (anything in the URL with ?name or &name) are saved in the $_GET superglobal.
If sub is a hierarchy, you should probably just write it as such. For instance: ?sub=path/to/sub or ?sub=path:to:sub
From this you can explode() on your separator (/ or :) to get the different parts of the sub parameter. $sub_array = explode('/', $_GET['sub']);
You can then iterate over the array using a foreach or directly access the highest branch of the hierarchy using count():
$sub_array[count($sub_array-1)];
Building URL Parameters
If you have an array of subs that you want to use to generate a URL, you can use implode() to build your URL params. $sub_params = implode('/', $_GET['sub']);
You might construct that array by appending each sub to the $sub_array.
$sub_array[] = 'sub1';
$sub_array[] = 'sub2';
$sub_array[] = 'sub3';
etc.
Inspect the Data
If you get lost, use var_dump($_GET) or var_dump($variable) to see what's inside it.
Each cat=3 or sub_1 value can be retreived by $_GET['cat'] and $_GET['sub_1'] respectively. To elaborate, $_GET['NAME_OF_PROPERTY'] would look like php.net/index.php?NAME_OF_PROPERTY=whatever
It seems like both questions were on how to grab the value from the URL, so I hope that answers both.
In your example url http://www.example.com/categories/index.php?cat=3&sub_1=sub1&sub_2=sub2&sub_3=sub3&sub_4=sub4
echo $_GET['cat']; // 3
echo $_GET['sub_1']; // sub1
echo $_GET['sub_2']; // sub2
echo $_GET['sub_3']; // sub3
echo $_GET['sub_4']; // sub4
For the first part; you should use the $_GET[] array which contain every parameter passed in the url $_GET['cat'], $_GET['sub_1'].
For the second part, in your case you should try send an array in parameter like this :
http://www.example.com/categories/index.php?cat=3&sub[1]=sub1&sub[2]=sub2&sub[3]=sub3&sub[4]=sub4
And then use the $_GET['sub'][] array $_GET['sub'][1], $_GET['sub'][2], ...
Now you can determine the length of the array and know the last item in it.
This example is under the assumption you want to find the very first variable in the GET query, and the very last:
<?php
// Here, you separate all the variables
$parameters = explode("&", $_SERVER['QUERY_STRING']);
// Get the last key's index
$last_key_index = count($parameters)-1;
// Easy way for PHP 5.3
$first_key = strstr($parameters[0], "=", true);
$last_key = strstr($parameters[$last_key_index], "=", true);
// Bit longer otherwise
$first_key = substr($parameters[0], 0, strpos($parameters[0], "="));
$last_key = substr($parameters[$last_key_index], 0, strpos($parameters[$last_key_index], "="));
// Test it here
echo($first_key);
echo($last_key);
?>