In my code in php 5 I need to access array key which have a dash in its name(case-ins). Is there any way to do that?
My code looks like this:
$count = 0;
foreach($par as $key){
foreach($key as $case-ins)
$count = $count+1;
....
}
Basically I need to get array size. I know I can probably use the count function, but right now the biggest problem I am dealing with is the dash. I have found on the internet something like ${case-ins}. but it didn't work. I can't change name of array key because it is actually argument from command line which I got using getopt.
Could you please help me with this? Or is there any other way around to count how many times was same argument used?
Thank you for all the answers :)
$par = array(
array(
'0'=> 'dark-blue',
'1'=> 'yellow',
'2'=> 'high-color'
),
);
$count = 0;
foreach ($par as $key ) {
foreach ($key as ${'case-ins'} ) {
if (preg_match('#-#', ${'case-ins'} )==true) {
$count = $count+1;
}
}
}
echo $count; // count is 2 ..
More info: in some strict PHP systems you better use #(.+)?-(.+)?# instead of #-#
and instead of ${'case-ins'} you may use normal variables like $case_ins, not dash in PHP.
Related
So, let's assume I am not using php 5.4 because I tried
$results[0][0]
and it did not work. My array $results is an array of arrays and I want to return the first value of the first array. How would I do this without doing the above, because it doesn't work.
Let assume that your array looks like this
$results = array(
'k1'=>array(
'k1.1'=>'v1.1',
'k1.2'=>'v1.2',
'k1.3'=>'v1.3',
),
'k2'=>array(
'k2.1'=>'v2.1',
'k2.2'=>'v2.2',
'k2.3'=>'v2.3',
),
);
Then if you need to get first value of the first array this should help
$results[key($results)][key($results[key($results)])];
Looks weird :)
You can use foreach function
foreach($result as $resk => $resv ){
$i = 1;
foreach($resv as $rk => $rv ){
if($i == 1){
echo $rv;
}
$i++;
}
}
<?php
$interests[50] = array('fav_beverages' => "beer");
?>
now i need the index (i.e. 50 or whatever the index may be) from the value beer.
I tried array_search(), array_flip(), in_array(), extract(), list() to get the answer.
please do let me know if I have missed out any tricks for the above functions or any other function I`ve not listed. Answers will be greatly appreciated.
thanks for the replies. But for my disappointment it is still not working. btw I have a large pool of data like "beer");
$interests[50] = array('fav_cuisine' => "arabic");
$interests[50] = array('fav_food' => "hummus"); ?> . my approach was to get the other data like "arablic" and "hummus" from the user input "beer". So my only connection is via the index[50].Do let me know if my approach is wrong and I can access the data through other means.My senior just informed me that I`m not supposed to use loop.
This should work in your case.
$interests[50] = array('fav_beverages' => "beer");
function multi_array_search($needle, $interests){
foreach($interests as $interest){
if (array_search($needle, $interest)){
return array_search($interest, $interests);
break;
}
}
}
echo multi_array_search("beer", $interests);
If your array contains multiple sub-arrays and you don't know which one contains the value beer, then you can simply loop through the arrays, and then through the sub-arrays, to search for the value, and then return the index if it is found:
$needle = 'beer';
foreach ($interests as $index => $arr) {
foreach ($arr as $value) {
if ($value == $needle) {
echo $index;
break;
}
}
}
Demo
If i knew the correct terms to search these would be easy to google this but im not sure on the terminology.
I have an API that returns a big object. There is one particular one i access via:
$bug->fields->customfield_10205[0]->name;
//result is johndoe#gmail.com
There is numerous values and i can access them by changing it from 0 to 1 and so on
But i want to loop through the array (maybe thats not the correct term) and get all the emails in there and add it to a string like this:
implode(',', $array);
//This is private code so not worried too much about escaping
Would have thought i just do something like:
echo implode(',', $bug->fields->customfield_10205->name);
Also tried
echo implode(',', $bug->fields->customfield_10205);
And
echo implode(',', $bug->fields->customfield_10205[]->name);
The output im looking for is:
'johndoe#gmail.com,marydoe#gmail.com,patdoe#gmail.com'
Where am i going wrong and i apologize in advance for the silly question, this is probably so newbie
You need an iteration, such as
# an array to store all the name attribute
$names = array();
foreach ($bug->fields->customfield_10205 as $idx=>$obj)
{
$names[] = $obj->name;
}
# then format it to whatever format your like
$str_names = implode(',', $names);
PS: You should look for attribute email instead of name, however, I just follow your code
use this code ,and loop through the array.
$arr = array();
for($i = 0; $i < count($bug->fields->customfield_10205); $i++)
{
$arr[] = $bug->fields->customfield_10205[$i]->name;
}
$arr = implode(','$arr);
This is not possible in PHP without using an additional loop and a temporary list:
$names = array();
foreach($bug->fields->customfield_10205 as $v)
{
$names[] = $v->name;
}
implode(',', $names);
You can use array_map function like this
function map($item)
{
return $item->fields->customfield_10205[0]->name;
}
implode(',', array_map("map", $bugs)); // the $bugs is the original array
here's my issue:
I have a program in the command line that has access to tens of thousands of users. The idea is that you want to be able to get all the information about a user by simply inputting their username. So, because I want to work in php, I've done the following
$user_info = array();
exec('uwdir -v userid=nvidovic', $user_info);
To give you an oversimplified version of what a var_dump on $user_info would look like, it would be something like this:
array(2){
[0] => "first: N"
[1] => "last: Vidovic"
}
I'd like to be able to do this $user[first] => N
This is what I've come up with (not for the real data from command line):
$full_name = array("first: N", "last: Vidovic");
var_dump($full_name);
foreach ($full_name as $part_name) {
$exploded = explode(":", $part_name);
$make_array = array($exploded[0] => $exploded[1]);
echo $make_array["first"];
}
Clearly, this doesn't work. But my question is why? Does anyone know how I can do what I explained above?
I'm really, really, really...really stuck
THANKS to anyone in advance!!
EDIT:
Great answers, thank you. One last thing though, I keep getting the error messages Notice: Undefined offset: 1 and Notice: Undefined offset: 0 for the code below:
$user_info = array();
exec('uwdir -v userid=nvidovic', $user_info);
foreach ($user_info as $info) {
$exploded_info = explode(":", $info);
$info_array[$exploded_info[0]] = $exploded_info[1];
}
echo $info_array["displayName"];
Anyone know why this is happening? I thought the explode function would break up the single string into an array of two strings, right?
Instead of
$make_array = array($exploded[0] => $exploded[1]);
try
$make_array[$exploded[0]] = $exploded[1];
Make sure you put $make_array = array(); before this line, just to make sure it's an empty array.
$full_name = array("first: N", "last: Vidovic");
//var_dump($full_name);
foreach ($full_name as $part_name) {
$exploded = explode(":", $part_name);
$make_array[$exploded[0]] = $exploded[1];
// echo $make_array["first"];
}
print_r($make_array);
http://codepad.org/JvcPdBeq
Because the line
$make_array = array($exploded[0] => $exploded[1]);
overwrites the contents of the whole array. So on the last step you will have only one element there.
I have a large array.
In this array I have got (among many other things) a list of products:
$data['product_name_0'] = '';
$data['product_desc_0'] = '';
$data['product_name_1'] = '';
$data['product_desc_1'] = '';
This array is provided by a third party (so I have no control over this).
It is not known how many products there will be in the array.
What would be a clean way to loop though all the products?
I don't want to use a foreach loop since it will also go through all the other items in the (large) array.
I cannot use a for loop cause I don't know (yet) how many products the array contains.
I can do a while loop:
$i = 0;
while(true) { // doing this feels wrong, although it WILL end at some time (if there are no other products)
if (!array_key_exists('product_name_'.$i, $data)) {
break;
}
// do stuff with the current product
$i++;
}
Is there a cleaner way of doing the above?
Doing a while(true) looks stupid to me or is there nothing wrong with this approach.
Or perhaps there is another approach?
Your method works, as long as the numeric portions are guaranteed to be sequential. If there's gaps, it'll miss anything that comes after the first gap.
You could use something like:
$names = preg_grep('/^product_name_\d+$/', array_keys($data));
which'll return all of the 'name' keys from your array. You'd extract the digit portion from the key name, and then can use that to refer to the 'desc' section as well.
foreach($names as $name_field) {
$id = substr($names, 12);
$name_val = $data["product_name_{$id}"];
$desc_val = $data["product_desc_{$id}"];
}
How about this
$i = 0;
while(array_key_exists('product_name_'.$i, $data)) {
// loop body
$i++;
}
I think you're close. Just put the test in the while condition.
$i = 0;
while(array_key_exists('product_name_'.$i, $data)) {
// do stuff with the current product
$i++;
}
You might also consider:
$i = 0;
while(isset($data['product_name_'.$i])) {
// do stuff with the current product
$i++;
}
isset is slightly faster than array_key_exists but does behave a little different, so may or may not work for you:
What's quicker and better to determine if an array key exists in PHP?
Difference between isset and array_key_exists