I have an array with just a single element. The key and value looks like this:
Array ( [test] => 12342 )
Result i want is this inside an variable:
test = '12342'
I tried following:
$test = "'" . implode(" ", $metric) . "'";
print_r($test);
But this gives only gives me: '12342', i wanted the '=' after key and '' around the value (this is used for SQL later on). So the result should be test = '12342'. Does not look like implode would work here. I tried looking at http_query_builder but failed.
If this array is only ever going to contain one single item, then you don’t need to loop through the data, but you can use key and current instead:
$data = ['test' => 12342];
echo key($data) . " = '" . current($data) . "'";
key gets you the key of the “current” item, and current gets its value.
By just replying your question as it is using that unique example you would do it this way assuming there is only 1 value inside your array :
$yourArray = array('test' => '12342');
foreach($yourArray as $key => $value) {
$test = $key . " = '" . $value . "'";
}
print_r($test);
If there are multiple you would do this, as each key is unique :
$yourArray = array('test' => '12342', 'testing' => '24321');
$test = array();
foreach($yourArray as $key => $value) {
$test[$key] = $key . " = '" . $value . "'";
}
var_dump($test);
Related
I have array :
$test = ['10' => 'a', '20' => 'b'];
I want get key and value for first and second element, e.g. something like
echo 'first array key is ' . array_key($test, 0);//10
echo 'second array key is ' . array_key($test, 1);//a
echo 'first array value is ' . array_value($test, 0);//20
echo 'second array value is ' . array_value($test, 1);//b
any ideas ?
PS. I don't want solution with
foreach ($test as $key => $value ){...
You can store all the keys and values in variables using array_keys and array_values i.e.,
$keys = array_keys($test);
$values = array_values($test);
And then, use them as follows:
echo 'first array key is ' . $keys[0]
echo 'first array value is ' . $values[0]
you can use this for get first:
$element = array_chunk($test, 1, 1);
var_dump( $element[0]);
or something like this for easy get all sub arrays:
list($first, $second) = $test;
print_r($first);
print_r($second);
I have an array I need formatted into a single array. The current function I have works well aside for I can't figure out how to set a custom key for the ID. The array is used to populate a select box, so I need that ID. I spent hours messing with it and I am just lost when it comes to formatting arrays.
I need the output to use a custom key (eg. $row->id ) which would output something like:
[40 =>'Florida', 50 =>'CA', 33 => 'NY']
Right now it outputting like..
[0 =>'Florida', 1 =>'CA', 2 => 'NY']
This is my current function:
protected function createRegionList($parent_id = '0', $spacing = '')
{
$arr = [];
foreach ($this->getByParentId($parent_id) as $row) {
$arr[] = $spacing . ' ' . $row->title;
$arr = array_merge($arr, $this->createRegionList($row->id, ' ' . $spacing . '—'));
}
return $arr;
}
Any help or suggestion would be much appreciated. I have found a bunch of examples, but they are more multidimensional arrays and I need the output as a single array.
You can set array item key by specifying the key in brackets. See PHP docs.
Try this
$arr[$row->id] = $spacing . ' ' . $row->title;
array_merge is not preserving keys. Use array_replace.
Or just union these together
$arr = [];
foreach ($this->getByParentId($parent_id) as $row) {
$arr[] = $spacing . ' ' . $row->title;
$childs = $this->createRegionList($row->id, ' ' . $spacing . '—');
$arr = $childs + $arr;
}
return $arr;
I have a array like that :
$myarray= 'zzz,aaa,bbb' ;
and want use it in mysql IN clause like that :
"... WHERE ids IN ($myarray) " // didnt work
"... WHERE ids IN ('$myarray') " // didnt work
the error im getting is that the first value in that array zzz says that zzz is not a column name . so i understand that i must separate the values with quotes to be like that :
$myarray= ' "zzz","aaa","bbb" ' ;
But i have no clue to do that . any help would be much appreciated.
$myarray = 'zzz,aaa,bbb';
$myarray = implode("','",explode(',',$myarray));
$query = "..... WHERE ids IN ('$myarray')";
You need to explode() your string and then implode() it -
$myarray = 'zzz,aaa,bbb';
$realArray = explode(',', $myarray);
$stringForIn = "'" . implode("','", $realArray) . "'";
echo "WHERE ids IN ($stringForIn)";
Try that :
$myarray= 'zzz,aaa,bbb' ;
echo implode '"' . ('","', explode(',', $myarray)) . '"';
I have listed an example below. What I need is for $key to return the actual index number (position) in the array during the loop, but instead it is returning Array. The same code works fine when given a single dimension array, but not in the example below.
GIVEN:
$screenshots would be similar to the following with only more entries.
Array
(
[0] => Array
(
[screenshot_id] => 871
[image_filename] => DSCF0124.JPG
)
)
PHP:
//build in clause & binding using selected array from above
$prefix = $in_clause = '';
$binding_clause = array();
foreach($screenshots as $key)
{
$in_clause .= $prefix.':selected_'.$key;
$prefix = ', ';
$binding_clause[':selected_'.$key] = $key['screenshot_id'];
}
RESULT:
$inclause = :selected_Array
$binding_clause =
Array
(
[:selected_Array] => 871
)
EXPECTED:
$inclause = :selected_0
$binding_clause =
Array
(
[:selected_0] => 871
)
Just because you call it $key doesn't make it a key. You need the key and the value (inner array):
foreach($screenshots as $key => $value)
{
$in_clause .= $prefix.':selected_'.$key;
$prefix = ', ';
$binding_clause[':selected_'.$key] = $value['screenshot_id'];
}
You need to tell it that you want the KEY and the VALUE.
like this:
foreach($screenshots as $key=>$screenShot)
That will get you the key and the value.
You just need to change your foreach to cast keys and values
foreach($screenshots as $key => $val)
Now the key is in your $key variable while you can access elements with $val array, for example $key['screenshot_id']
You can have a check to documentation examples here
Try:
foreach($screenshots as $key => $screenshot)
{
$in_clause .= $prefix.':selected_'.$key;
$prefix = ', ';
$binding_clause[':selected_'.$key] = $screenshot['screenshot_id'];
}
Read more about PHP foreach: http://php.net/manual/en/control-structures.foreach.php
Hi I have this array called $allextensionsforque. See print_r below.
Array ( [0] => Local/101#from-queue/n,0 [data] => Local/101#from-queue/n,0 )
I'm trying a foreach like below but its not showing any data on the echo. Can anyone help?
$allextensionsforqueu = mysqli_query($conqueue,"SELECT data FROM queues_details WHERE id = '1' AND keyword ='member'");
$allextensionsforque = mysqli_fetch_array($allextensionsforqueu);
$foo = "";
foreach ($allextensionsforque as $extensionrow)
{
$extensionrowstriped = substr($extensionrow['data'],6,3);
$foo = "' " . $extensionrowstriped . " ' ,";
}
echo $foo;
You don't have an array in $extensionrow, since it's only an one dimensional array. So you just need to replace $extensionrowstriped = substr($extensionrow['data'],6,3); with $extensionrowstriped = substr($extensionrow,6,3);.
Check for errors using mysqli_error(). Here is a simple approach, but you may want to improve it for production use:
$allextensionsforqueu = mysqli_query($conqueue,"...") or die(mysqli_error($conqueue));
Next, append your data to $foo instead of overwritting it in each loop:
$foo .= "' " . $extensionrowstriped . " ' ,";
Finally, verify that $extensionrow contains the data that you expect. substr() will return false if a match is not found.