This is my array
Array (
[0] => "8266_hal_url"
[1] => "8266_hal_picture"
[2] => "8266_hal_status"
[3] => "8266_hal_qwert"
[4] => "4224423_hal_status"
[5] => "4223_hal_status"
)
How do I find all hal_status in an array and pass it to a query?
PHP >= 5.3.0
$testData = array ( '8266_hal_url',
'8266_hal_picture',
'8266_hal_status',
'8266_hal_qwert',
'4224423_hal_status',
'4223_hal_status',
);
$testNeedle = 'hal_status';
$result = array_filter($testData, function($arrayEntry) use ($testNeedle) {
return (strpos($arrayEntry,$testNeedle) !== false);
});
var_dump($result);
$result=array();
foreach ($myarray as $element)
if (strpos($element,'hal_status')===false) continue;
else $result[]=$element;
print_r($result);
Try this:
$result = array();
foreach($array as $item)
{
if(strpos($item, 'hal_status') !== false)
{
$result[] = $item;
}
}
print_r($result);
Related
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'm trying to group airlines with relations into single chains.
Array
(
[0] => Array
(
[0] => Aeroflot
[1] => S7
[2] => Transaero
)
[1] => Array
(
[0] => Alitalia
[1] => Lufthansa
)
[2] => Array
(
[0] => Transaero
[1] => United
)
[3] => Array
(
[0] => United
[1] => Alitalia
)
[4] => Array
(
[0] => Volotea
[1] => Iberia
)
[5] => Array
(
[0] => Transaero
[1] => Aeroflot
)
)
From that array I need to find connections between elements and combine it to groups. Expected results:
Array
(
[0] => Array
(
[0] => Aeroflot
[1] => S7
[2] => Transaero
[3] => United
[4] => Alitalia
[5] => Lufthansa
)
[1] => Array
(
[0] => Volotea
[1] => Iberia
)
)
Can anyone help with that? I've tried a dozen of ways but still get no success.
The most closest way I've tried which works but not in all cases:
function array_searchRecursive($needle,$haystack) {
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle===$value OR (is_array($value) && array_searchRecursive($needle,$value) !== false)) {
return $current_key;
}
}
return false;
}
foreach ($newarr as $key => $airlines)
{
foreach ($airlines as $lastkey => $airline)
{
$index = array_searchRecursive($airline,$newarr);
echo $airline.$index."\n";
if ($index !== false)
{
$newarr[$index] = array_merge($newarr[$index],$airlines);
$lastarr[] = $index;
}
}
}
But it doesn't match all values in array.
Recursive function will help you. You are welcome )
$arr = array(
array('Aeroflot','S7','Transaero'),
array('Alitalia','Lufthansa'),
array('Transaero','United'),
array('United','Alitalia'),
array('Volotea','Iberia'),
array('Transaero','Aeroflot')
);
function getConnections($arr,$curr_line_n=0,$num=0) {
for($i=0;$i<count($arr[$curr_line_n]);$i++) {
$cur_air_name = $arr[$curr_line_n][$i];
for($k=$curr_line_n+1; $k<count($arr); $k++) {
for($l=0;$l<count($arr[$k]);$l++) {
if ($arr[$k][$l]==$cur_air_name) {
$arr[$curr_line_n] = array_values(array_unique(array_merge($arr[$curr_line_n],$arr[$k])));
array_splice($arr,$k,1);
$num++;
$arr = getConnections($arr,$curr_line_n,$num);
}
}
}
}
$num++;
$curr_line_n++;
if ($curr_line_n!=count($arr)) {
$arr = getConnections($arr,$curr_line_n,$num);
}
return $arr;
}
print_r(getConnections($arr));
As per your example you are just grouping sub arrays by taking first sub array as reference. for example if you have any elements common in first sub array and in subsequent sub arrays then you combine them into one sub array.
<?php
$arr = array(
array('a', 'b', 'c', 'd'),
array('d', 't'),
array('t', 'f'),
array('k', 'o'),
array('p', 'z')
);
$arr_implode = array();
foreach ($arr as $key => $value) {
if (is_array($value)) {
$arr_implode[$key] = implode('', $value);
} else {
$arr_implode[$key] = $value;
}
}
$arr_key = array();
$result = array();
$count = count($arr_implode);
$tempj = 0;
for ($i = 0; $i <= $count; $i++) {
$flag = FALSE;
for ($j = ($i + 1); $j < $count; $j++) {
similar_text($arr_implode[$i], $arr_implode[$j], $percent);
if ($percent > 0) {
$result[] = array_merge($arr[$i],$arr[$j]);
break;
} else {
$result[] = $arr[$j];
break;
}
}
}
foreach($result as $key => $val){
$result[$key] = array_unique($val);
}
echo "<pre>";
print_r($result);
echo "</pre>";
?>
Try this code.
$arr = [
['Aeroflot', 'S7', 'Transaero'],
['Alitalia', 'Lufthansa'],
['Transaero', 'United'],
['United', 'Alitalia'],
['Volotea', 'Iberia'],
['Transaero', 'Aeroflot']
];
$hash = [];
$result = [];
foreach($arr as $set){
foreach($set as $el){
if(!$hash[$el]) $hash[$el] = [] ;
$hash[$el] = array_merge($hash[$el], $set);
}
}
function merge_connections(&$h, $key){
if(!$h[$key]) return [];
$data = [$key];
$rels = $h[$key];
unset($h[$key]);
foreach($rels as $rel){
if($rel==$key) continue;
$data = array_merge($data, merge_connections($h, $rel));
}
return $data;
}
foreach(array_keys($hash) as $company){
if(!$hash[$company]) continue;
array_push($result, merge_connections($hash, $company));
}
print_r($result);
Given the following input:
array('one/two/3',
'one/four/0/5',
'one/four/1/6',
'one/four/2/7',
'eight/nine/ten/11')
How can I convert it into this:
array(
'one': array(
'two': 3,
'four': array(5,6,7)
)
'eight': array(
'nine': (
'ten':11
)
}
)
$input = array ('one/two/3',
'one/four/0/5',
'one/four/1/6',
'one/four/2/7',
'eight/nine/ten/11');
$result = array ();
foreach ($input as $string) {
$data = array_reverse(explode('/', $string));
$tmp_array = array ();
foreach ($data as $val) {
if (empty($tmp_array)) {
$tmp_array = $val;
} else {
$tmp = $tmp_array;
$tmp_array = array ();
$tmp_array[$val] = $tmp;
}
}
$result = array_merge_recursive($result, $tmp_array);
}
echo "<pre>";
print_r($result);
echo "</pre>";
Output:
Array
(
[one] => Array
(
[two] => 3
[four] => Array
(
[0] => 5
[1] => 6
[2] => 7
)
)
[eight] => Array
(
[nine] => Array
(
[ten] => 11
)
)
)
It would be nice if we saw what you have tried.
$my_array = array('one/two/3',
'one/four/0/5',
'one/four/1/6',
'one/four/2/7',
'eight/nine/ten/11');
$result= array();
foreach ($my_array as $val) {
$ref = & $result;
foreach (explode("/", $val) as $val) {
if (!isset($ref[$val])) {
$ref[$val] = array();
}
$ref = & $ref[$val];
}
$ref = $val;
}
var_dump($result);
i have a problem with arrays, there are not my friends :)
i have a this array:
Array
(
[0] => 2012163115
[1] => 2012163115
[2] => 2012161817
[3] => 201214321971
[4] => 201214321971
[5] => 201214321971
)
and i need this with all the variables appear more than once
Array
(
[0] => 2012163115
[1] => 201214321971
)
i try this
foreach ($array as $val) {
if (!in_array($val, $array_temp)) {
$array_temp[] = $val;
} else {
array_push($duplis, $val);
}
}
but the result is
Array
(
[0] => 2012163115
[1] => 201214321971
[2] => 201214321971
)
where is my mistake? thanks for help!
array_unique() is there for you.
EDIT: ops I didn't notice the "more than once" clause, in that case:
$yourArray = array('a', 'a', 'b', 'c');
$occurrences = array();
array_walk($yourArray, function($item) use(&$occurrences){
$occurrences[$item]++;
});
$filtered = array();
foreach($occurrences as $key => $value){
$value > 1 && $filtered[] = $key;
}
var_dump($filtered);
$array = array(
'2012163115',
'2012163115',
'2012161817',
'201214321971',
'201214321971',
'201214321971',
);
$duplication = array_count_values($array);
$duplicates = array();
array_walk($duplication, function($key, $value) use (&$duplicates){
if ($key > 1)
$duplicates[] = $value;
});
var_dump($duplicates);
Please see http://php.net/manual/en/function.array-unique.php#81513
These are added characters to make SO accept the post!
$array_counting = array();
foreach ($array as $val)
if ( ! in_array($val, $array_counting))
{
$array_counting[$val] ++; // counting
}
$array_dups = array();
foreach ($array_counting as $key => $count)
{
if ($count > 1)
$array_dups[] = $key; // this is more than once
}
I have this array:
Array
(
[0] => CMS_ADMIN
[1] => CMS_ACADEMIC
[2] => CMS_FINANCE
[3] => HRM_HR
[4] => SRM_D
[5] => SRM_CNC
[6] => SRM_TM
[7] => SRM_DE
)
I would like to get the array by searching to the array value using the word provided. Let say I'm just provide the 'CMS' word, so how i'm gonna get the [0] => CMS_ADMIN, [1] =>CMS_ACADEMIC, [2] => CMS_FINANCE assign to the new array. Please help....
With a function that looks like this:
function array_filter_prefix($array, $prefix) {
$result = array();
foreach ($array as $value) {
if (strpos($value, $prefix) === 0) {
$result[] = $value;
}
}
return $result;
}
Given an input array, $test, you can then do this to get the result:
$result = array_filter_prefix($test, 'CMS');
print_r($result);
Codepad here.
batter style in PHP 5.3
$prefix = "CMS";
$new = array_filter($array, function ($str) use ($prefix) {
return (strpos($str, $prefix) === 0);
});