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
}
}
Related
In a foreach loop i would like to compare [name] value beetween different arrays but they have not the same levels.
Array(
[array1] => Array
(
[0] => WP_Term Object
(
[name] => Plafond
)
)
[array2] => WP_Term Object
(
[name] => Chaudière
)
[array3] => Array
(
[0] => WP_Term Object
(
[name] => Pla
)
[1] => WP_Term Object
(
[name] => Toc
)
)
)
I don't know how could i get the [name] in the same loop whereas levels are different.
I have tried to make :
foreach( $fields as $name => $value )
{
echo $value->name; }
Should i add another loop in the first loop ?
thanks
So your data looks like this:
$json = '{"array1":[{"name":"Plafond"}],"array2":{"name":"Chaudière"},"array3":[{"name":"Pla"},{"name":"Toc"}]}';
$array = json_decode($json);
If you don't know how deep it will go, a simple recursive function should work. Perhaps something like this:
function get_name($o, &$output) {
if (is_array($o)) {
foreach($o as $v) {
get_name($v, $output);
}
} elseif (property_exists($o, "name")) {
$output[] = $o->name;
}
}
$output = [];
foreach ($array as $v) {
get_name($v, $output);
}
If you data is going to look like the sample you provided (i.e. it will always be first or second level) then you don't need to worry about recursion.
$output = [];
foreach ($array as $k=>$v) {
if (is_array($v)) {
foreach ($v as $k2=>$v2) {
$output[] = $v2->name;
}
} else {
$output[] = $v->name;
}
}
Either way, your output values are all in the $output array:
print_r($output);
Output:
Array
(
[0] => Plafond
[1] => Chaudière
[2] => Pla
[3] => Toc
)
You can use array_map, array_key_exists to retrive the name index from the array
$jsonFormat = '{"array1":[{"name":"Plafond"}],"array2":{"name":"Chaudière"},"array3":[{"name":"Pla"},{"name":"Toc"}]}';
$jsonArray = json_decode($jsonFormat,true);
$res = [];
array_map(function($v) use (&$res){
if(array_key_exists('name', $v)){
$res[] = $v['name'];
}else{
foreach($v as $_key => $_value){
$res[] = $_value['name'];
}
}
}, $jsonArray);
echo '<pre>';
print_r($res);
Result:-
Array
(
[0] => Plafond
[1] => Chaudière
[2] => Pla
[3] => Toc
)
You can use $res to compare the names.
I have this stdclass object in array
Array ( [0] => stdClass Object ( [file_id] => 6 [file_name] => 1ofdays.wav ) [1] => stdClass Object ( [file_id] => 7 [file_name] => abcd.mp3 ) )
I want to convert this stdclass object array into associative arrays such as
$name_array=([text]=>1ofdays.wav,[text]=>abcd.mp3);
$id_array=([value]=>6,[value]=>7);
I tried achieving it by first flattening the array using this function
public function array_flatten($mArray) {
$sArray = array();
foreach ($mArray as $row) {
if ( !(is_array($row)) ) {
if($sArray[] = $row){
}
} else {
$sArray = array_merge($sArray,$this->array_flatten($row));
}
}
return $sArray;
}
This function gave me result
Array ( [0] => 6 [1] => 1ofdays.wav [2] => 7 [3] => abcd.mp3 )
then I created two arrays even() and odd()
and took elements from odd and even indexes of the and pushed them in their respected arrays
which resulted in
even( [0] => 6 [1] => 7 )
odd( [0] => 1ofdays.wav [1] => abcd.mp3 )
now i want to put elements of even array into
Id_array =('value'=>6,'value'=>7)
and
name_array=('text'=>1ofdays.wav,'text'=>'abcd.mp3')
Hope It is same as you want
<?php
class stdClass1 {
public $file_id;
public $file_name;
public function setName($n, $sn) {
$this->file_id = $n;
$this->file_name = $sn;
}
}
$exaObj = new stdClass1;
$exaObj2 = new stdClass1;
$exaObj->setName('6', '1ofdays.wav');
$exaObj2->setName('7', 'abcd.mp3');
$arr = array($exaObj, $exaObj2);
foreach ($arr as $k => $v) {
print_r($v);
echo "<br>";
foreach ($v as $key => $value) {
$$key[] = $value;
}
}
echo "<pre>";
print_r($file_id);
echo "</pre>";
echo "<pre>";
print_r($file_name);
echo "</pre>";
echo "Second METHOD<br><br>";
foreach ($arr as $k => $v) {
$nameArr[] = $v->file_name;
$idArr[] = $v->file_id;
}
echo "<pre>";
print_r($idArr);
echo "</pre>";
echo "<pre>";
print_r($nameArr);
echo "</pre>";
?>
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 a problem with my array, So my array is :
Array
(
[0] => Array
(
[0] => Array
(
[sValue] => 1
)
[1] => Array
(
[sValue] => 2
)
)
)
I want to get this array :
Array
(
[0]=>1
[1]=>2
)
I tried like this, but not work, it's get only the sValue = 1:
for($i=0;$i<count($aExpectedAnswers);$i++){
foreach($aExpectedAnswers as $answer){
$aFormatedAnswers[] = '\''.$answer[$i]['sValue'].'\'';
}
}
Help me please, Thx in advance
$aFormatedAnswers = [];
foreach ($aExpectedAnswers as $answer) {
if (is_array($answer)) {
foreach ($answer as $item) {
$aFormatedAnswers[] = $item;
}
} else {
$aFormatedAnswers[] = $answer;
}
$result = array();
foreach($initial as $subArray){
foreach($subArrray as $value){
$result[] = $value;
}
}
print_r($result);
try this code:
$aExpectedAnswers = array(
array(
0 => array('sValue'=>1),
1 => array('sValue'=>2),
)
);
$result = array();
foreach($aExpectedAnswers as $aea){
foreach($aea as $ae){
$result[] = $ae['sValue'];
}
}
print_r($result);
hopefully helping.
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;