I must do foreach array:
Array
(
[0] => Array
(
[name] => news_number
[value] => 10
)
)
and get "value" to variable $value. How i can do ?
i have :
foreach($this->_config as $key){
foreach($key as $value['news_number'] => $key){
echo $key;
}
}
but is not good i have "news_number10".
try this:
foreach( $this->_config as $key => $data )
{
echo $data['name']." is ".$data['value']."\n";
}
If you're looking for a specific variable in your configuration data, you could simply do this:
foreach( $this->_config as $key => $data )
{
if ( $data['name'] == 'news_number' )
{
$myNewsNumber = $data['value'];
break;
}
}
Or try that:
foreach( $this->_config as $data )
{
extract($data);
printf("%s is %s\n", $name, $value);
}
Can't you do it assigning a numnber to the array?
foreach($this->_config as $key){
foreach($key as $value[0] => $key){
echo $key;
}
}
Are you intending to have just one value? then just change the array number to the array you want to use in the foreach.
try
$this->_config = Array (
0 => Array (
'name' => "news_number",
'value' => 10
)
);
foreach ( $this->_config as $value ) {
echo $value ['name'], " ", $value ['value'];
}
Output
news_number 10
Related
I have an generated array into this format and want to generated a second array to fit into a file that expects the specific format
This is the array i have :
(int) 0 => array(
[Service] => Array
(
[id] => 6948229
[document] => Array
(
[number] => 0003928425
)
)
This is the array i want to build from the previous array (will have many indexes)
verified[id]
verified[number]
So far i build this script:
foreach($data as $key=>$value )
{
echo '<br>key '.$key;
foreach($value as $k=>$v)
{
$Verified[$key]['id'] = $v["id"];
$Verified[$key]['number'] = $v['document']['number'];
But just get undefined index error message.
Which indexes i must use to get the flatten array ?
From what I can make from your question, you can do something like this to get the desired output,
$Verified = []; //use array() for versions below 5.5
foreach($data as $key=>$value )
{
echo '<br>key '.$key;
foreach($value as $k=>$v)
{
if(is_array($v)){
$Verified[$key]['number'] = $v['document']['number'];
}
$Verified[$key]['id'] = $v['id'];
Please pass your array to this function
function arrayconvert($arr) {
if (is_array($arr)) {
foreach($arr as $k => $v) {
if (is_array($v)) {
arrayconvert($v);
} else {
$newarr[$k] = $v;
}
}
}
return $newarr;
}
There is no need of second foreach and you are getting undefined index because you are using $v['id'] insted of $val['id'] in that line $Verified[$key]['id'] = $v["id"];
<?php
$data = array('Service' => array('id' => 6948229,'document' => array ('number' => '0003928425' )));
$verified = array();
foreach($data as $key => $val)
{
$verified[$key]['id'] = $val['id'];
$verified[$key]['number'] = $val['document']['number'];
}
echo "<pre>"; print_r($verified);
?>
output
Array
(
[Service] => Array
(
[id] => 6948229
[number] => 0003928425
)
)
I Have an array in PHP that looks like:
Array ( [2099:360] => 6-3.25 [2130:361] => 4-2.5 [2099:362] => 14-8.75 )
Notice there is Two Keys that are 2099 and one that is 2130. I Have a foreach to remove the everything after the colon. the $drop is my array
$a = array();
foreach ($drop as $part=>$drop_a){
$ex_part = explode(":", $part);
$a[$ex_part[0]] = $drop_a;
}
print_r($a);
but when I print $a it displays only the recent value of the 2099?
Array ( [2099] => 14-8.75 [2130] => 4-2.5 )
Any Successions? How can I get it to display all of the values?
Thank You for Your Help
One solution is to use a multi-dimensional array to store this strategy:
$a = array();
foreach ($drop as $part=>$drop_a){
$ex_part = explode(":", $part);
if (isset($a[$ex_part[0]])) {
$a[$ex_part[0]][] = $drop_a;
} else {
$a[$ex_part[0]] = array($drop_a);
}
}
Your resulting data-set will however be different:
Array ( [2099] => Array ( [0] => 6-3.25 [1] => 14-8.75) [2130] => Array ( [0] => 4-2.5 ) )
It may be beneficial to you to preserve the second portion after the colon :
$a = array();
foreach ($drop as $part=>$drop_a){
$ex_part = explode(":", $part);
if (isset($a[$ex_part[0]])) {
$a[$ex_part[0]][$ex_part[1]] = $drop_a;
} else {
$a[$ex_part[0]] = array($ex_part[1] => $drop_a);
}
}
Now your result is a little more meaningful:
Array ( [2099] => Array ( [360] => 6-3.25 [362] => 14-8.75) [2130] => Array ( [361] => 4-2.5 ) )
Finally you can use alternative key-naming strategy if one is already occupied:
$a = array();
foreach ($drop as $part=>$drop_a){
$ex_part = explode(":", $part);
if (isset($a[$ex_part[0]])) {
$a[altName($ex_part[0], $a)] = $drop_a;
} else {
$a[$ex_part[0]] = $drop_a;
}
}
function altName($key, $array) {
$key++; // Or however you want to do an alternative naming strategy
if (isset($array[$key])) {
return altName($key, $array); // This will eventually resolve - but be careful with the recursion
}
return $key;
}
Returns:
Array
(
[2099] => 6-3.25
[2130] => 4-2.5
[2100] => 14-8.75
)
You basically have a key and a sub key for each entry, so just put them in a multidimensional array:
$a = array();
foreach ($drop as $key => $val) {
list($key, $subKey) = explode(':', $key);
$a[$key][$subKey] = $val;
}
Gives you:
Array
(
[2099] => Array
(
[360] => 6-3.25
[362] => 14-8.75
)
[2130] => Array
(
[361] => 4-2.5
)
)
You can traverse multidimensional arrays by nesting loops:
foreach ($a as $key => $subKeys) {
foreach ($subKeys as $subKey => $val) {
echo "$key contains $subKey (value of $val) <br>";
}
}
I have array which show result like this :
Array ( [standards_id] => 1
[value] => sory
[order] => 10
)
Array ( [standards_id] => 1
[value] => javid
[order] => 3
)
Array ( [standards_id] => 1
[value] => saleem
[order] => 4
).
I want to check the array key ,if it is "value" then i want to concatenate its value.I try code like this but not successeded.
$row = array(.....);
$vali = '';
foreach ($row as $key => $value) {
if( $value[$key] == 'value'){
echo $vali .= $value['value'].",";
}
}
I want to do it in one loop.$row contains multiple arrays like above 3.$row contains all the records that are fetched from data base.hope you understand what $row is.
Use isset() to find out whether the key 'value' is present in each element of $row; you don't need a loop for that at all:
foreach ($row as $data) {
if (isset($data['value'])) {
$vali .= $data['value'] . ',';
}
}
Alternatively, you can build an array with the values:
$values = array();
foreach ($row as $data) {
if (isset($data['value'])) {
$values[] = $data['value'];
}
}
echo join(',', $values);
Your naming suggests that $row contains just:
Array ( [standards_id] => 1
[value] => sory
[order] => 10
)
There is no reason to loop over such a structure. You can test if this 'row' contains a value with
if( isset( $row['value'] ) ) {
$vali .= $row['value'] . ', ';
}
If you have a variable $myRows containing these arrays, you can loop over them using
foreach( $myRows as $k => $row ) { ... }
In this case $row contains the array and you can use the first code to append the value to $vali.
You need this:
foreach ($row as $key => $value) {
if( $key == 'value'){
$vali .= $value.",";
}
}
echo $vali;
$row = array('standards_id' => 1, 'value' => 'saleem', 'order' => 4);
$vali = '';
foreach ($row as $key => $value) {
if( $key == 'value'){
$vali .= $value . ",";
}
}
echo $vali;
I have an array in the format
Array
(
[/Callum/] => Array
(
[0] => ##chan1
)
[/Adam/] => Array
(
[0] => ##chan2
)
[/Chris)/] => Array
(
[0] => ##chan1
)
[/Mike*/] => Array
(
[0] => ##chan3
)
)
And from this I use the below code to try and get the id of the array that each channel features in.
foreach($array as $row)
{
if (in_array($buf['channel'],$row))
{
$return = $return." ".current(array_keys($array,$row));
}
}
My problem is that current() doesnt seem to work the way I am expecting it to. Currently if the $buf /Callum/ twice rather than /Callum/ and /Chris/
Why not:
foreach($array as $key => $row)
{
if (in_array($buf['channel'],$row))
{
$return = $return . " " . $key;
}
}
Try this instead
foreach($array is $id => $row){
$return .=" ".$id;
}
edit:
foreach($array is $id => $row){
if($row[0] == $buf['channel']){
echo $key; //This is your key
}
}
I want to implode this array to make a string with all keys = 'Palabra'. How can this be done? (the output should be: 'juana es')
Array
(
[0] => Array
(
[Palabra] => juana
)
[1] => Array
(
[Palabra] => es
[0] => Array
(
[Raiz] => ser
[Tipo] => verbo
[Tipo2] => verbo1
)
)
)
function foo( $needly, $array ) {
$results = array();
foreach ( $array as $key => $value ) {
if ( is_array( $value ) ) {
$results = array_merge($results, foo( $needly, $value ));
} else if ( $key == $needly ) {
$results[] = $value;
}
}
return $results;
}
echo implode( " ", foo( "Palabra", $your_array ) );
I ended using the foreach for lack of a better solution:
foreach ($array as $key => $palabra) {
$newArray[] = $array[$key]["Palabra"];
}
$string = implode(' ', $newArray);
I think the simplest solution is with array_walk_recursive.
<?php
$arr = array(
array(
'Palabra' => 'juana',
),
array(
'Palabra' => 'es',
array(
'Raiz' => 'ser',
'Tipo' => 'verbo',
'Tipo2' => 'verbo1',
),
),
);
$str = array();
array_walk_recursive($arr, function($value, $key) use(&$str) {
if ($key == 'Palabra') {
$str[] = $value;
}
});
$str = implode(' ', $str);
echo "$str\n";
The function passed in is called for each key-value pair in the array and any subarrays. Here we append any values with a matching key to an array and then implode the array to get a string.