使用implode()从另一个数组创建一个数组

I have an array.

my array:

while($rowop = mysql_fetch_array($resultop))
{
    $info[] = array(
        'status'=>$rowop[2],
        'day' => $rowop[3],
        'from'=>$rowop[4],
        'to' => $rowop[5],
        'mon'=>$rowop[6],
        'tue' => $rowop[7],
        'wed'=>$rowop[8],
        'thu' => $rowop[9],
        'fri'=>$rowop[10],
        'sat' => $rowop[11],
        'sun'=>$rowop[12]
    );
}

value of $rowop[6],$rowop[7],$rowop[8],$rowop[9],$rowop[10],$rowop[11],$rowop[12] can be 'on' or 'off'. I want to show only those rows which contain 'off' value. array will be same as:

'status'=>'ok',
'day' =>'all',
'from'=>'12pm',
'to' => '8am',
'off day'=>'tue,wed,thu,sat,sun'

Any one please give me Idea.if possible give example

Try this, ( assuming your array name is $info ) :

$result = array();
foreach($info as $key=>$value){
    $result[$key]['status'] = $value['status'];
    $result[$key]['day'] = $value['day'];
    $result[$key]['from'] = $value['from'];
    $result[$key]['to'] = $value['to'];
    $off=array();
    foreach ($value as $k => $v) {
        if($v=="off") $off[]=$k;
    }
    if($off) $off_days = implode (", ", $off);
    $result[$key]["off day"] = $off_days;
}
print_r($result);

What about this?

$days = ['mon','tue','wed','thu','fri','sat','sun'];
for($offs=[],$i=6,$i<=12,$i++) if($rowtop[$i]=='off') $offs[]= $days[$i-6];

$info[] = array(
     'status'=>$rowop[2],
     'day' => $rowop[3],
     'from'=>$rowop[4],
     'to' => $rowop[5],
     'off day' => implode(',', $offs)
);

How about following code

I didn't test it. Fix if there are any errors

$days = array('mon','tue', 'wed', 'thu', 'fri', 'sat', 'sun');
$selected = array();
$final = array();
foreach($days as $day)
{
    if(isset($info[$day]))
        if($info[$day] == 'off')
            $selected[] = $info[$day];
}

$all_days = implode(',', $selected);
$final['status'] = $info['status'];
$final['day'] = $info['day'];
$final['from'] = $info['from'];
$final['to'] = $info['to'];
$final['off day'] = $all_days;

An example using array_filter

$days = array('mon','tue', 'wed', 'thu', 'fri', 'sat', 'sun');

while($rowp ....) { //

$offs = implode (
            ',',
            array_keys(
                array_filter(
                    array_intersect_key($oneRow, array_flip($days)),
                    function($i) {return $i == 'off'}
                )
            )
);

//...

}

where $oneRow is what you are filling the $info array with.

Hope it helps

This can be achieved in multiple steps:

  • Find all keys with off status using array_filter() and store it in array $result
  • Remove all the "week keys", i.e. mon, tue, wed etc. using array_diff_key()
  • Implode $result array to get a comma-separated string, and use that to create a new array element

Code:

// find all keys with 'off' status
$status = 'off';
$result = array_keys(array_filter($info,
    function($element) use ($status) {
        return $element == $status;
    }
));

// remove all week keys
$keys = array('mon','tue','wed','thu','fri','sat','sun');
$info = array_diff_key($info, array_flip($keys));

// create a new array item with the comma-separated key string
$info['off_days'] = implode(',', $result);

print_r($info);

Output:

Array
(
    [status] => ok
    [day] => all
    [from] => 12pm
    [to] => 8am
    [off_days] => tue,wed,thu,sat,sun
)

Demo.