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;
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 this array $pages which spits out this data:
Array (
[Name] => Array (
[Subname] => Array (
[0] => 43.2057, -79.9632, 1, -70,-150
[1] => 140240757658.jpg
[2] => 5
[3] => 0
) )
[Name2] => Array (
[Subname2] => Array (
[0] => 43.1769, -79.4703, 5, -70,-150
[1] => 140267498933.png
[2] => 16
[3] => 0
) )
)
and I have this foreach setup:
foreach($pages as $row => $value) {
echo '<li>'.$row.'<ul>';
foreach($value as $x => $y) {
echo
'<li>
'.$x.'
</li></ul></li>';
}
}
What I am trying to do is if [3] in each of the Subname is equal to 0, then skip it from my foreach.
NOTE: Subname and Name are just examples, they will be different for each one.
This should work:
foreach ($pages as $page) {
foreach($page as $subname) {
if ($subname[3] != 0) {
/* Do whatever you want with the data of this subname */
}
}
}
Or this if you want to use the key names:
foreach ($pages as $pageKey => $page) {
foreach($page as $subnameKey => $subname) {
if ($subname[3] != 0) {
/* Do whatever you want with the data of this subname */
}
}
}
If you want to skip the sub-foreach then this is the solution
foreach($pages as $row => $value) {
if($value['Subname'][3] == 0)
continue;
echo '<li>' . $row . '<ul>';
foreach($value as $x => $y) {
echo '<li>' . $x . '</li></ul></li>';
}
}
Since this is an Associative Array you will not be able to to check
$pages['foo']['bar'][0] === 0
before the first foreach begins since we don't know the value of $pages['foo'] before hand. when we get to the check you have already output html at the point in the form of:
echo '<li>' . $row . '<ul>';
I want to group an associative array by fields. The array itself is originally from a mysql database query.
Below is an example of how I do it by hard coding it:
<?php
$fields = array("ID,subID");
$fieldCounts = count($fields);
$data = array(); //there is sql querieed data
$parsedData = array();
foreach ($data as $val)
{
if ($fieldCounts == 1)
{
$f0 = $fields[0];
$fv0 = $val[$f0];
$parsedData[$fv0][] = $val;
}
else if ($fieldCounts == 2)
{
$f0 = $fields[0];
$fv0 = $val[$f0];
$f1 = $fields[10];
$fv1 = $val[$f1];
$parsedData[$fv0][$f1][] = $val;
}
else
{
exit("Third field not implemented");
}
}
?>
But how can I do it dynamically with an arbitrary number of fields?
Am not sure how this code has worked for you but some things are that wrong and might not allow the code to function properly
Fields has only as one valued with ,
$fields = array("ID,subID");
^----------- between string
Instead of
$fields = array("ID","subID");
Notice: Undefined offset:
$f1 = $fields[10];
^----- your array is not up to 10
Since you did not put your generate data and desired output. I would assume your final output and generate some temporary data
$fields = array("ID","subID"); //You can Increase or decrease this Fields
$fieldCounts = count($fields);
$data = array(); // there is sql querieed data
for($i = 0; $i < 3; $i ++) {
$data[] = array("ID" => mt_rand(1, 1000),"subID" => "sub" . mt_rand(100, 900));
}
Ruining your code with the 2 corrections above
foreach ( $data as $val ) {
if ($fieldCounts == 1) {
$f0 = $fields[0];
$fv0 = $val[$f0];
$parsedData[$fv0][] = $val;
} else if ($fieldCounts == 2) {
$f0 = $fields[0];
$fv0 = $val[$f0];
$f1 = $fields[1];
$fv1 = $val[$f1];
$parsedData[$fv0][$f1][] = $val;
} else {
exit("Third field not implemented");
}
}
Output
Array
(
[159] => Array
(
[subID] => Array <----------- SubID is fixed in your can cause confict
(
[0] => Array
(
[ID] => 159
[subID] => sub589
)
)
)
[334] => Array
(
[subID] => Array
(
[0] => Array
(
[ID] => 334
[subID] => sub703
)
)
)
)
A better Alternative to yours
$parsedData = array();
foreach ( $data as $val ) {
$temp = &$parsedData;
foreach ( array_slice($val, 0, $fieldCounts) as $key ) {
$temp = &$temp[$key];
}
$temp[] = $val;
}
print_r($parsedData);
Output
Array
(
[159] => Array
(
[sub589] => Array <---------- Make Sub ID Dynamic
(
[0] => Array
(
[ID] => 159
[subID] => sub589
)
)
)
[334] => Array
(
[sub703] => Array
(
[0] => Array
(
[ID] => 334
[subID] => sub703
)
)
)
)
Recommended Version For easy array path
$parsedData = array();
foreach ( $data as $val ) {
$temp = &$parsedData;
foreach ( array_slice($val, 0, $fieldCounts) as $key ) {
$temp = &$temp[$key];
}
$temp = $val;
}
print_r($parsedData);
Output
Array
(
[159] => Array
(
[sub589] => Array <---- Easy to asses as $parsedData['159']['sub589']
(
[ID] => 159
[subID] => sub589
)
)
[334] => Array
(
[sub703] => Array
(
[ID] => 334
[subID] => sub703
)
)
)
Instead of doing if/elseif/else inside your $data foreach-loop (which is always limited to the number you "write" in there with that structure and a lot of code-duplicateion) you need to turn that if/elseif/else into a loop of it's own.
But first of all transform the existing code, I start in the first if body, it contains already all code necessary:
$f0 = $fields[0];
$fv0 = $val[$f0];
$parsedData[$fv0][] = $val;
The $val should be assigned to the array $parsedData which is keyed by $fields name $value. Let's compress this here, the number 0 in names is superfluous as we don't want it any longer (but maybe the first):
$field = $fields[0];
$value = $values[$field];
$parsedData[$value][] = $values;
(I changed $val into $values to improve naming). This is now more easy to read and understand. Also we spot the magic number 0 here more easily.
Now to the magic. We want to add to an array here (push):
$parsedData[$value][] = $values;
To make this more easy, let's turn it this way:
$array = &$parsedData[$value];
$array[] = $values;
This right now seems superfluous, but when this turns into a loop, it will become more clear:
$array = &$parsedData;
...
$array = &array[$value];
...
$array[] = $values;
Let's review the code in with the outer loop at this moment:
foreach ($data as $values)
{
$array = &$parsedData;
$field = $fields[0];
$value = $values[$field];
$array = &$array[$value];
$array[] = $values;
}
Obviously this code is yet not complete. The inner-loop is missing but it starts to get some kind of body. And actually the inner loop is pretty simple to achieve:
$array = &$parsedData;
foreach ($fields as $field)
{
$value = $values[$field];
$array = &$array[$value];
}
$array[] = $values;
And that's already it. The single field has been turned into an iteration over all fields. The aliasing/referencing of the sub-array per each step in the iteration allows to push the value to the appropriate array entry after the inner loop has finished.
The whole outer and inner loop:
foreach ($data as $values)
{
$array = &$parsedData; # set reference
foreach ($fields as $field)
{
$value = $values[$field];
$array = &$array[$value];
}
$array[] = $values;
unset($array); # remove reference
}
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
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
}
}