Viewed   146 times

I'm trying to create an array inside an array, using a for loop - here's my code:

    array(
    'label' => 'Assign to user',
    'desc' => 'Choose a user',
    'id' => $prefix.'client',
    'type' => 'radio'
    'options' => array( 
        foreach ($clients as $user) {
         $user->user_login => array (  
            'label' => $user->user_login,  
            'value' => $user->user_login,
            ), 
        }
        )
    )

Unfortunately this gives me a

"Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')'"

For the line:

'options' => array( 

I'm at a bit of a loss as to what has gone wrong - any help is much appreciated. $clients is defined elsewhere, so that is not the problem.

 Answers

2

That's invalid syntax. You'd have to build the "parent" portions of the array first. THEN add in the sub-array stuff with the foreach loop:

$foo = array(
    'label' => 'Assign to user',
    'desc' => 'Choose a user',
    'id' => $prefix.'client',
    'type' => 'radio',
    'options' => array()
);

foreach ($clients as $user) {
    $foo['options'][] = array (  
        'label' => $user->user_login,  
        'value' => $user->user_login,
    );
}
Saturday, December 3, 2022
3
foreach( $codes as $code and $names as $name ) { }

That is not valid.

You probably want something like this...

foreach( $codes as $index => $code ) {
   echo '<option value="' . $code . '">' . $names[$index] . '</option>';
}

Alternatively, it'd be much easier to make the codes the key of your $names array...

$names = array(
   'tn' => 'Tunisia',
   'us' => 'United States',
   ...
);
Wednesday, August 31, 2022
5

If you now how deep your array is you can just simply add other foreach-loops within your main loop.

if (!empty($array)) 
{
    foreach ($array['categories'] as $category) 
        {
            echo $category['category_id'];
            echo "<br>";

            if(isset($category['categories'])){
            foreach($category['categories'] as $category2)
                {
                echo $category2['category_id'];
                echo "<br>";

                     if (isset($category2['categories'])){
                     foreach($category2['categories'] as $category3)
                       {
                         echo $category3['category_id'];
                         echo "<br>";

                         ...

                       } }

                }}
        }
}
Sunday, August 14, 2022
 
2

This should work for you:

Just array_reduce() your array into a one dimensional array.

$result = array_reduce($arr, "array_merge", [])

EDIT:

To get your result you have to rewrite your code a bit, so that you collect all values into one array by go throw all elements of $linkObjs with array_map().

$linkObjs = $html->find('h3.r a'); 
$i = count($linkObjs);
$result = array_map(function($v){
    return trim($v->plaintext);
}, $linkObjs);

print_r($result);
Thursday, September 1, 2022
 
1

unset it first in case it is already in the proper format, otherwise you will remove what you just defined:

foreach($array as $key => $value)
    {
        unset($array[$key]);
        $array[ucfirst($key)] = $value;
    }
Thursday, August 11, 2022
 
kchetan
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :