get value of array of arrays in php - php

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++;
}
}

Related

Array key with the dash php 5

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.

php - Find array for which a key have a given value

I have an array of dictionnaries like:
$arr = array(
array(
'id' => '1',
'name' => 'machin',
),
array(
'id' => '2',
'name' => 'chouette',
),
);
How can I find the name of the array containing the id 2 (chouette) ?
Am I forced to reindex the array ?
Thank you all, aparently I'm forced to loop through the array (what I wanted to avoid), I thought that it were some lookup fonctions like Python. So I think I'll reindex with id.
Just find the index of array that contains the id you want to find.
SO has enough questions and answers on this topic available.
Assuming you have a big array with lots of data in your real application, it might be too slow (for your taste). In this case, you indeed need to modify the structure of your arrays, so you can look it up faster, e.g. by using the id as an index for the name (if you are only interested in the name).
As a for loop would be the best way to do this, I would suggest changing you array so that the id is the arrays index. For example:
$arr = array(
1 => 'machin',
2 => 'chouette',
);
This way you could just get the name for calling $arr[2]. No looping and keeping your program running in linear time.
$name;
foreach ($arr as $value){
if ( $value['id'] == 2 ){
$name = $value['name'];
break;
}
}
I would say that it might be very helpful to reindex the information. If the ID is unique try something like this:
$newarr = array();
for($i = 0;$i < count($arr);$i++){ $newarr[$arr[$i]['id']] = $arr[$i]['name']; }
The result would be:
$newarr = array('1'=>'machin','2'=>'chouette');
Then you can go trough the array with "foreach" like this:
foreach($newarr as $key => $value){
if($value == "machin"){
return $key;
}
}
But of course the same would work with your old array:
foreach($arr as $item){
if($item['name'] == "machin"){
return $item['id'];
}
}
It depends on what you are planning to do with the array ;-)
array_key_exist() is the function to check for keys. foreach will help you get down in the multidimensional array. This function will help you get the name element of an array and let you specify a different id value.
function findKey($bigArray, $idxVal) {
foreach($bigArray as $array) {
if(array_key_exists('id', $array) && $array['id'] == $idxVal) {
return $array['name'];
}
}
return false;
}
//Supply your array for $arr
print(findKey($arr, '2')); //"chouette"
It's a bit crude, but this would get you the name...
$name = false;
foreach($arr as $v) {
if($v['id'] == '2') {
$name = $v['name'];
break;
}
}
echo $name;
So no, you are not forced to reindex the array, but it would make things easier.

getting information out of an array with key/value pairs

I have the following snippet of code that is creating the following array...
while ($stmt->fetch()) {
foreach($row as $key => $val) {
$x[$key] = $val;
}
$results[] = $x;
}
Results in the follow array:
Array ( [0] => Array ( [cap_login] => master [cap_pword] => B-a411dc195b1f04e638565e5479b1880956011badb73361ca ) )
Basically I want to extract the cap_login and cap_pword values for testing. For some reason I can't get it!
I've tried this kind of thing:
echo $results[$cap_login];
but I get the error
Undefined variable: cap_login
Can someone put me right here?
Thanks.
cap_login is in an array within $results so you would have to do $results[0]['cap_login']
You would have to do the following:
echo $x[0]['cap_login'] . '<br />';
echo $x[0]['cap_pword'];
The reson $results[$cap_login] wont work is because there isn't a variable called $cap_login, there is a string called cap login. In addition, there isn't a key in $results called $cap_login. There is a value in $results called 'cap_login'

compare elements of array and based on comparison retrieve array data

I have two arrays, there is one element which is 'name' common in first and second array. Now, I want to retrieved values from second array if first array value match to second one.
code for first array:
$rs = array();
foreach ( $ex_array as $data ) {
$rs[] = array( 'name' => $data['name'] );
}
Second Array:
$entries_data = array();
foreach ( $array as $entry ) {
$name = $entry['name']['value'];
$email = $entry['email']['value'];
$entries_data[] = array(
'name' => $name,
'email' => $email
);
}
Problem is, there is only multiple names in first array, and then i have to compare first array names with the second one array, if there is match then whole data is retrieved from second array for specific name. I am trying to do this by using in_array function for search names in second array but can't fetch whole values. Any suggestions or help would be grateful for me.
is_array() is used for 1d arrays which isnt ur case use this function taken from the php documentation comments and edited by me to work for ur example
function in_multiarray($elem, $array)
{
$top = sizeof($array) - 1;
$bottom = 0;
while($bottom <= $top)
{
if($array[$bottom]['name'] == $elem)
return true;
else
if(is_array($array[$bottom]['name']))
if(in_multiarray($elem, ($array[$bottom]['name'])))
return true;
$bottom++;
}
return false;
}

While loop in foreach loop not looping correctly

I'm trying to make a very basic php ORM as for a school project. I have got almost everything working, but I'm trying to map results to an array. Here's a snippet of code to hopefully assist my explanation.
$results = array();
foreach($this->columns as $column){
$current = array();
while($row = mysql_fetch_array($this->results)){
$current[] = $row[$column];
print_r($current);
echo '<br><br>';
}
$results[$column] = $current;
}
print_r($results);
return mysql_fetch_array($this->results);
This works, but the while loop only works on the first column. The print_r($results); shows the following:
Array ( [testID] => Array ( [0] => 1 [1] => 2 ) [testName] => Array ( ) [testData] => Array ( ) )
Can anybody shed some light?
Thanks in advance!
It's because you already fetched every row, and the internal pointer is at the end.
The next while, mysql_fetch_array() will immediately return false.
You can reset the pointer to the first row:
mysql_data_seek($this->results, 0);
Put this just before
while($row = mysql_...
I'm not sure you can use the -> operator in a variable name. As you trying to get the key and value out of the array $columns? If so, you want something like this:
foreach($columns as $k => $v) {
//in here, $k is the name of the field, and $v is the associated value
}

Categories